0

From One lightning component I should be calling two Apex Class methods. My tow Apex Classes are like below

Class1

public class updateCaseRecord {    
 @AuraEnabled
public static void updateCheck(String caseId, String Status){
    List<Case> records = [select id,Status from Case WHERE Id = :caseId];
    for (Case record : records)
    {
            record.Status = 'Closed';
    }
    update records; 
    }   }

Class 2

public class updateRecordOwner {    
 @AuraEnabled
public static void updateOwner(String caseId, String Status){
    List<Case> records = [select id,Status from Case WHERE Id = :caseId];
    for (Case record : records)
    {
            record.OwnerId = userInfo.getUserId();
    }
    update records; 
    }   }

Below is my lightning component

<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId" access="global"  controller="updateCaseRecord">
    <aura:attribute name="updateCase" type="Case" default="{'sobjectType':'Case'}"/>
      <lightning:button variant="brand" label="Close Case" onclick="{!c.updateCaseClose}"  />
      <lightning:button variant="brand" label="Take Ownership" onclick="{!c.updateOwnership}"  />
</aura:component>

On the first button Controller updateCaseClose I am to call the Class1 method updateCheck like below

({
    updateCaseClose : function(c,e,h) {
        var action = c.get("c.updateCheck");
        action.setParams({caseId: c.get('v.recordId'), Status: StatusSel});
        action.setCallback(this, result => {
        ......

I am not sure how to handle with the second button component to call the method from Apex Class 2. Can someone help how to refer both Apex Class from 1 lightning component

2
  • Why not create a second method in updateCaseRecord that simply calls the method in updateRecordOwner ? – Drew Kennedy Apr 16 '20 at 17:59
  • Because I will have 10 buttons on the same component and it is hard to maintain the Apex class with so many methods – user81642 Apr 16 '20 at 18:06
3

So you can't really do this with 1 Aura Component. You will need to create a second component and extend it with the first component as detailed in this answer.

Alternatively, you could try using Lightning Web Components instead. With LWCs, you can import individual apex methods from as many different classes as you want. Here is some information about Lightning Web Components

Personally, I'd put both of those methods in a single Controller class for this Aura component. I believe it's best practice to keep components as isolated as possible. Makes it far easier to move things between orgs/environments

2

There is another answer in this post, but it is missing a couple of snippets of code so I wanted to offer an alternative here.

As @stackasaur said, you cannot point one aura component to two different controllers. If you created these two classes specifically for this case, then put them in one (both together) and voila. For example, like this:

public class CaseRecordOperations {    
  @AuraEnabled
  public static void updateCheck(String caseId, String Status){
    List<Case> records = [select id,Status from Case WHERE Id = :caseId];
    for (Case record : records)
    {
        record.Status = 'Closed';
    }
    update records; 
  }  

  @AuraEnabled
  public static void updateOwner(String caseId, String Status){
    List<Case> records = [select id,Status from Case WHERE Id = :caseId];
    for (Case record : records)
    {
        record.OwnerId = userInfo.getUserId();
    }
    update records; 
  }   
}

However, I recognize that sometimes these classes might already exist, or may be part of another, larger, architecture. If that your case, maybe you can do it this way:

public class CaseRecordOperations {    
  @AuraEnabled
  public static void updateCheck(String caseId, String Status){
     updateCaseRecord.updateCheck(caseId, Status);
  }  

  @AuraEnabled
  public static void updateOwner(String caseId, String Status){
    updateRecordOwner.updateOwner(caseId, Status);
  }   
}

Either way, you may only use one controller per each of your aura components.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.