0

I have an aura component with a lightning card and lightning record form inside the lightnign card. I have the init handler which calls the doinit controller which calls the helper:

doinit : function(component, event, helper) {
        helper.checkToDisplayComponentClient(component);
        helper.getAllProductFieldsClient(component);
    }

getAllProductFieldsClient : function(component) {
        this.getProductSpecificFieldsClient(component);
    }

Initially in getProductSpecificFieldsClient I was calling getProductCommonFieldsClient:

getProductSpecificFieldsClient : function(component) {
        var action = component.get("c.getProductSpecificFields");
        action.setParams({oppId : component.get("v.recordId")});
        action.setCallback(this, function(response) {
            var state = response.getState();
            if(state === "SUCCESS") {
                component.set("v.productFields", response.getReturnValue());
                this.getProductCommonFieldsClient(component);
            } else if(state === "ERROR") {
                component.set("v.productFields", "");
                component.set("v.commonProductFields", "");
            }
        });
        
        $A.enqueueAction(action);
    }

It was working for a month until now. Now, when I open an opportunity it was lightning cards under which common fields and specific fields should be displayed doesn't show anything. If I move getProductCommonFieldsClient out of getProductSpecificFieldsClient and place it in getAllProductFieldsClient it works (see below).

getAllProductFieldsClient : function(component) {
        this.getProductCommonFieldsClient(component);
        this.getProductSpecificFieldsClient(component);
}
  1. Why I do not understand.

If I modify getProductSpecificFieldsClient including the lines var errors = response.getError(); console.log('state: products specific ' + errors[0].message);, like below:

getProductSpecificFieldsClient : function(component) {
        var action = component.get("c.getProductSpecificFields");
        action.setParams({oppId : component.get("v.recordId")});
        action.setCallback(this, function(response) {
            var state = response.getState();
            console.log('state products specific ' + state);
            var errors = response.getError();
            console.log('state: products specific errors ' + errors[0].message);
            if(state === "SUCCESS") {
                console.log('state: products specific ' + response.getReturnValue());
                component.set("v.productFields", response.getReturnValue());
            } else if(state === "ERROR") {
                component.set("v.productFields", "");
                component.set("v.commonProductFields", "");
            }
        });
        
        $A.enqueueAction(action);
    }

I am unable to display the specific fields for some reason.

  1. I would expect a null pointer exception on console log because var errors are empty, but I am new to JS, so I am guessing thats just how JS works?

  2. I had to Enable Profile Access for Apex Class for various profiles the controller class of this component, otherwise I was getting You do not have access to the Apex class named..., which is strange because I never did it before and it was working fine until now. Why now this error is occuring?

1
  • 1
    Point 3 is because of a critical update activated by Salesforce. Moving forward you will need to provide access of classes to profiles.
    – manjit5190
    Commented Oct 19, 2020 at 15:38

1 Answer 1

1

this is a very powerful and tricky thing to master in JS. In your code this holds the context which calls different methods in the aura component framework.

Using this in different loops changes the context values which it stores. So , while calling another helper method outside of a loop or callback function may or may not work at all times.

To overcome this challenge, a simple way is to store the value of this in another variable and use that new variable to call other helper methods. Something like this

let self= this; // should be first line inside a helper method
// some business logic
// some loop
self.getProductCommonFieldsClient(component);

On point 3 this is a critical update which was recently enforced by salesforce to enable more security on AuraEnabled Apex methods. You can read details here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .