1

I created a lightning component button I added them in to the page layout. I tried to do with the Lightning App Builder the button show up when I click nothing happens. The lightning component button is like below Component

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

Controller

({
    updateCaseClose : function(c, e, h) {
        h.updateCaseClose_helper(c,e,h);
    },
})

Helper

({
    updateCaseClose_helper : function(c,e,h) {
        var save_action = c.get("c.updateCheck");
        save_action.setParams({
            });
        $A.enqueueAction(save_action);
    }
})

Apex Class

public class setCaseClose {
    @AuraEnabled
    public static void updateCheck(){
        list<Case> Case_list = new list<Case>();
        Case_list = [select id,Status from Case Limit 1];
        Case  p = new Case();
        p.id = Case_list[0].id;
        p.Status = 'Closed';
        update p;
    }
}

The button shows up in the page enter image description here

Clicking on it nothing happens, not what I am missing here.

5
  • This code is working fine, I guess you want to update the current Case but you are just querying a case and updating that, not sure which one. Apr 3, 2020 at 15:35
  • To update the current Case pass the current case id to the Apex method and use that in the SOQL to retrieve that and update the same. Apr 3, 2020 at 15:36
  • Can you help me how I can pass the CaseId from the component to ApexClass?
    – user81642
    Apr 3, 2020 at 15:43
  • And also check the debug logs as you are not using any callback to check the response. Apr 3, 2020 at 15:49
  • @itzmukeshy7 Even though the field is updated now the page is not refreshed even after trying to add $A.get("e.force:refreshView").fire()
    – user81642
    Apr 3, 2020 at 16:09

1 Answer 1

1

Modify the helper to pass the parameter from JS to Apex.

Helper:

({
  updateCaseClose_helper : function(c,e,h) {
    var save_action = c.get("c.updateCheck");
    save_action.setParams({
      caseId: c.get('v.recordId')
    });
    save_action.setCallback(this, function(response) {
      $A.get("e.force:refreshView").fire();
    });

    $A.enqueueAction(save_action);
  }
})

Apex Controller:

public class setCaseClose {
  @AuraEnabled
  public static void updateCheck(String caseId){
    List<Case> cases = new List<Case>();
    for(Case case:[select id,Status from Case WHERE Id = :caseId]){
      case.Status = 'Closed';
      cases.add(case)
    }

    if(!cases.isEmpty()){
      update cases;
    }
  }
}
4
  • 1
    Thank you it works now the records get the field updated but the page is not being refreshed Iadded $A.get("e.force:refreshView").fire(); in the helper after the $A.enqueueAction(save_action);
    – user81642
    Apr 3, 2020 at 16:08
  • 1
    Or can you suggest me instead of using $A.enqueueAction(save_action); how I can use $A.getCallback()?
    – user81642
    Apr 3, 2020 at 16:15
  • Try this, also updated in the answer above: save_action.setCallback(this, function(response) { $A.get("e.force:refreshView").fire(); }); Apr 3, 2020 at 18:30
  • I am having issue when I have two buttons, can you please help with this salesforce.stackexchange.com/questions/301425/…
    – user81642
    Apr 6, 2020 at 9:54

You must log in to answer this question.

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