Search code examples
salesforcees6-promiseapexsoqllwc

Passing in an Apex method parameter from Promise correctly?


I keep getting an error from the response of my promise, if I give it a parameter. I can't tell if I am suppose to pass in "columndefinitions" parameter to the apex method or not. I have included picture to help. PS: If I leave the parameter out, it returns an empty array but no errors.

Thoughts: Am I passing in the paramater incorrectly? Did I format my column defenitions wrong? Is it a syntax issue?

JS FILE

//before render, call an apex method query...
connectedCallback() {
   getValidFields(this.getColumnDefinitions)
             .then((response) => {
                console.log('getvalidfields', response)
               }).catch((error) => {
                console.error(error);
});

getColumnDefinitions() {
        return [
            {
                object: "Contact", apiName: "Name", label: 'Name', fieldName: 'contactURL', type: 'url', iconName: 'standard:contact', sortable: 'true',
                filterable: true, typeAttributes: { label: { fieldName: 'contactName' }, target: '_self', tooltip: 'Click to view Contact details' }
            },
            { object: "Contact", apiName: "Phone", label: 'Phone', fieldName: 'contactPhone', type: 'phone', sortable: false, filterable: true },
            { object: "Contact", apiName: "Email", label: 'Email', fieldName: 'contactEmail', type: 'email', sortable: false, filterable: true },
            { object: "Contact", apiName: "MailingAddress", label: "Address", fieldName: 'contactAddress', type: 'text', filterable: false },
            { object: "Contact", apiName: "Group_Rank__c", label: 'Group Rank', fieldName: 'contactGroupRank', type: 'text', sortable: true, filterable: true },
            { object: "Contact", apiName: "Role_Function__c", label: 'Role Function', fieldName: 'contactRoleFunction', type: 'text', sortable: true, filterable: false },
            { object: "ContactTerritoryRelationship__c", apiName: "TerritoryManagerRole__c", label: 'Territory Manager Role', fieldName: 'contactTerritoryRole', type: 'text', sortable: true, filterable: false },
            {
                object: "DistributionTerritory__c", apiName: "Name", label: 'Distribution Territory', fieldName: 'contactTerritoryURL', type: 'url', sortable: true, filterable: true, iconName: 'custom:custom78',
                typeAttributes: { label: { fieldName: 'contactTerritoryName' }, target: '_self', tooltip: 'Click to view Distribution Territory details' }
            },
            { object: "ContactTerritoryRelationship__c", apiName: "DistributionTerritory__c", type: "action", filterable: false, typeAttributes: { rowActions: this.rowActions } }
        ];
    }

CONTROLLER.CLS

 @AuraEnabled
    public static List<Map<String, Object>> getValidFields(List<Map<String, Object>> columnDefinitions) {
        for (Integer i = 0; i < columnDefinitions.size(); i++) {
            Map<String, Object> column = columnDefinitions.get(i);
            if (column.containsKey('object') && !userHasAccess((String) column.get('object'), (String) column.get('apiName'))) {
                columnDefinitions.remove(i);
            }
        }     
        return columnDefinitions;
    }

Promise.then

enter image description here

Console Errors


Solution

  • You should pass parameters as object if you call apex action imperatively from LWC. Object properties should be name like your Apex method parameters.

    In your case it should be:

    getValidFields({ columnDefinitions: this.getColumnDefinitions()} ).then(...)
    

    Please refer the following link for more details: https://developer.salesforce.com/docs/platform/lwc/guide/apex-call-imperative.html#call-an-apex-method-with-parameters