0

I have a lightning component that shows as button on the record page like below

enter image description here

The lightning component Controller are like below

<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 action = c.get("c.updateCheck");
        action.setParams({caseId: c.get('v.recordId')});
         $A.enqueueAction(action);
         $A.get('e.force:refreshView').fire();
    }
  })

Apex Class

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

On click of the button it doesnt refresh the page and shows the updated Status. I have to manually refresh the page. I did try to add the $A.get('e.force:refreshView').fire(); in the helper it doesnt work

2
  • Can you show us how you used $A.get('e.force:refreshView').fire()? Please edit your question to show us how you attempted to use it.
    – sfdcfox
    Commented Apr 3, 2020 at 17:36
  • @sfdcfox I updated the question. I am not sure how to use the setCallback too so I can add toast to the screen
    – user81642
    Commented Apr 3, 2020 at 17:41

1 Answer 1

3

For force:refreshView to work, you need to wait until your apex class returns; before this, the changes haven't been committed to the database yet. We use setCallback to accomplish this task.

({
  updateCaseClose_helper: function(c, e, h) {
    var action = c.get("c.updateCheck");
    action.setParams({ caseId: c.get("v.recordId") });
    action.setCallback(this, result => {
      switch (result.getState()) {
        case "SUCCESS":
        case "DRAFT":
          $A.get("e.force:showToast")
            .setParams({
              title: "Success",
              type: "success",
              message: "This case is now closed."
            })
            .fire();
          break;
        default:
          $A.get("e.force:showToast")
            .setParams({
              title: "Error",
              type: "error",
              message: "An error occurred while saving changes."
            })
            .fire();
      }

      $A.get("e.force:refreshView").fire();
    });
    $A.enqueueAction(action);
  }
})
7
  • Thanks. This works great. Is there way I can check the response from setCallback if it is a SUCCESS or not so I can fire Toast
    – user81642
    Commented Apr 3, 2020 at 17:50
  • @user81642 Yes, you can do that. I'll edit.
    – sfdcfox
    Commented Apr 3, 2020 at 17:53
  • +1 Never knew DRAFT was a state was following this documentation: developer.salesforce.com/docs/atlas.en-us.lightning.meta/…
    – Abhijith E
    Commented Apr 3, 2020 at 18:07
  • @user81642 Maybe you can use this little framework to develop things little fast and without errors, it's all upto you, AuraBase Commented Apr 3, 2020 at 18:37
  • @AbhijithE It may have changed, I've just been including it out of habit, lol.
    – sfdcfox
    Commented Apr 3, 2020 at 18:46

You must log in to answer this question.

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