1

I have a Lightning cmp with some logic, and I need to get Id to continue work with it in Lightning. But I get null. How can I fix it?

public static Id toolId;

@AuraEnabled
public static void createRecord(Tool__c tool){ //I get my tool from Lightning cmp      
    if(tool != null){
        upsert tool Email__c;
        toolId = tool.Id;
        System.debug(tool.Id + ' ' + toolId); //All is ok here: I can see needed Id
    }
}

public static void checkMyId() {
    System.debug(toolId); //But here I get null. Why? And how can I get here Id?
}

3 Answers 3

1

Lightning server-side controllers are stateless. That means that they do not retain any knowledge of variables (even static variables) from one action to the next. You need to return the value from the first method, then pass it in as a parameter to the second:

@AuraEnabled
public static Id createRecord(Tool__c tool){
    if(tool == null) {
      throw new AuraHandledException('Received a null tool record.');
    }
    upsert tool Email__c;
    toolId = tool.Id;
    System.debug(tool.Id + ' ' + toolId); //All is ok here: I can see needed Id
    return tool.Id;
}
public static void checkMyId(Id toolId) {
    System.debug(toolId);
}
1
  • Thank you! It works :) Commented Mar 11, 2019 at 15:56
0

You won't be able to do what you're attempting with a static variable. You can only have instances of a static variable.

You could for example use the this keyword for an instance of toolId, but not for the static toolId you've declared.

public static Id toolId;

@AuraEnabled
public static void createRecord(Tool__c tool){ //I get my tool from Lightning cmp 
    this.toolId=new toolId;  
    if(tool != null){
        upsert tool Email__c;
        this = tool.Id;
        System.debug(tool.Id + ' ' + this); //All is ok here: I can see needed Id
    }
}

This still doesn't help you with your other method except that you could test to see if this is an instance of the static toolId. If so, you'd likely want to call if from within the method I've outlined above.

2
  • Thank you, but it doesn't work. Can I send Id to the lightning controller somehow? Commented Mar 10, 2019 at 17:36
  • Sure, just remove the void and add a return statement to your createRecord method. You'll need to refactor a bit and do something like public static string createRecord(Tool___c tool) { .... then after your first } add a line like return this; and it should return the Id as a string. I'm writing this on the fly in a hurry, so my syntax may be a bit off. Be sure to double check it.
    – crmprogdev
    Commented Mar 11, 2019 at 16:19
-1

Depends how you want to process, or execute business logic on Id. As @sfdcfox code snippet stated, you can have a promise call executed in js controller and chain the methods. Return the id from the first call and pass it to the second. Or execute future call in the first method and pass Id to it.

You must log in to answer this question.

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