[SalesForce] Wired Apex method is not getting called Particular Condition LWC

I have an apex method which basically checks if user has create access on Custom Object or not. Based on that it just returns true or false.
If user has create permission in that case it's returning the true value but if I remove the create permission its not returning any value. In debug log I can see the Apex Method is called but noting in the lwc side.

As of now I'm calling my method on connectedCallback() where its working.

But I'm not able to understand why it's not returning any value when user doesn't have Object create permission.

Lwc.js

@wire(isObjectCreateable)
    isObjectCreate({ error, data }) {
        if (data) {
            this.isObjectCreatePermission = data;
            this.error = undefined;
            console.log(">>> isProposalCreatePermission : " + this.isObjectCreatePermission );

        } else if (error) {
            this.error = error;
            this.isObjectCreatePermission = false;
            console.log(">>> isProposalCreatePermission : " +error);
        }
    }

Apex:

@AuraEnabled(cacheable=true)
    public static Boolean isProposalCreateable(){
        SObjectType schemaType = Schema.getGlobalDescribe().get('Object__c');
        system.debug('>>> isProposal Createable '+schemaType.getDescribe().isCreateable());
        return schemaType.getDescribe().isCreateable();
    }

Debug Log:

  1. User Has Permission:

enter image description here
a. apex:

b. Console log:

enter image description here

  1. User Does not have permission:
    a. apex:
    b. Nothing in console log
    enter image description here

Best Answer

So let's consider your code

isObjectCreate({ error, data }) {
    if (data) {    
        //stuff
    } else if (error) {
        //other stuff
    }
}

If your Apex returns false, data= false and thus if(data) isn't true. But the method didn't fail, so error isn't defined either. Since you're returning a boolean I'd change it to

if (data != null && data != undefined) {

See if that works!

It's important to note also that returning a 0 value also compares as false, so if you know your return value can be 0 or false or an empty string, you should check for not null or not undefined instead.

Related Topic