Search In This Blog

2024-04-24

Jest issue: Fix TypeError getAccountInfo(...).then is not a function (test apex class inside @wire method)

To mock an Apex class
import getAccountInfo from "@salesforce/apex/CustomCtrl.getAccountInfo"

@wire(getRecord,[...])
wireRecord() {
    getAccountInfo(accountId).then(do somethings)
}

Create the jest mock.
jest.mock(
    "@salesforce/apex/CustomCtrl.getAccountInfo",
    () => { return { default: jest.fn(), } }, { virtual: true }
)
 
But mocked apex is called inside of @wire method, then getAccountInfo(...).then is not a function error is happened. Need to fix the mock as below:
jest.mock(
    "@salesforce/apex/CustomCtrl.getAccountInfo",
    () => {
        return {
            default: jest.fn((data) => {
                let mockData;
                if (data.accountId == "0000000000") {
                    mockData = [{
                    "Id": "0011700000xxxxx000",
                    "Name": "test"
                    }];
                } else if (data.accountId == "0000000001") {
                    mockData = [{
                    "Id": "0011700000xxxxx001",
                    "Name": "test1"
                    }];
                }
                // use Promise to mock getAccountInfo(...).then
                return Promise.resolve(mockData);
            }),
        };
    }, { virtual: true }
);


2023-12-27

2023-08-08

Not Admin User, How to delete/insert FinServ__FinancialAccount by Apex

Apex should be "without sharing".

Remove sObject.SObjectType.getDescribe().isDeletable() or sObject.SObjectType.getDescribe().isUpdateable() or sObject.SObjectType.getDescribe().isCreateable()

Inactive record in FinServ__RollupByLookupConfig__c, if need.


To Delete

Before delete, update "OwnerId" to current user. Like: FinServ__FinancialAccount.OwnerId = UserInfo.getUserId(). Then user can delete FinServ__FinancialAccount record.


To insert

If you want the special user can edit this FinServ__FinancialAccount record, don't forget to update "OwnerId" to the special user's id after insert.

2022-12-23

Catch System.LimitException in LWC

System.LimitException cannot catch by [try...catch...] in Apex.

When call Apex method in lwc and LimitException occurred in Apex source, [try...catch...] also not working in js.

But LimitException can be catched by [Promise.catch]

Sample
Promise.all([
    promise1,
    promise2,
    promise3
]).then((values) => {
    console.log(values);
}).catch((error) => {
    // LimitException can be catched here
    console.error(error);
}).finally(() => {
    console.log('Experiment completed');
});

2022-11-30

Custom auto open utility bar without popout button by aura

cmp
<aura:compomemt implements="lightning:utilityItem">
    <!-- define popout to false -->
    <aura:attribute name="supportsPopOut" type="Boolean" default="false" />

    <!-- auto open utility bar when init -->
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

    <!-- use lightning:utilityBarAPI -->
    <lightning:utilityBarAPI aura:id="utilitybar" />

    someting else
</aura:compomemt>

controller.js
doInit: function(component) {
    var utilityApi = component.find("utilitybar");
    utilityApi.getAllUtilityInfo().then(fuction (response) {
        if (typeof response != "undefined") {
            utilityApi.openUtility();
        }
    });
}