0

I am trying to have two buttons on my lightning component 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}"  />
    <lightning:button variant="brand" label="Take Ownership" onclick="{!c.updateOwnership}"  />
</aura:component>

Controller

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

    updateOwnership : function(c, e, h) {
        h.updateOwnership_helper(c,e,h);
    }
})

Helper

   ({
    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":
            $A.get("e.force:showToast")
            .setParams({
            type: 'success',
            message: 'Case is closed!',
            mode: 'pester'
            })
        .fire();
        break;
        default:
        $A.get("e.force:showToast")
        .setParams({
            type: 'error',
            mode: 'pester',
            message: 'Case Cant be Closed!'
        })
        .fire();
    }
    $A.get("e.force:refreshView").fire();
});
$A.enqueueAction(action);       
   },

   updateOwnership_helper: function(c,e,h) {
        var set_action = c.get("c.updateOwner");
        set_action.setParams({caseId: c.get('v.recordId')});
        set_action.setCallback(this, result => {
            switch (result.getState()) {
            case "SUCCESS":
            case "DRAFT":
            $A.get("e.force:showToast")
            .setParams({
            type: 'success',
            message: 'Case is Assigned to You!',
            mode: 'pester'
            })
        .fire();
        break;
        default:
        $A.get("e.force:showToast")
        .setParams({
            type: 'error',
            mode: 'pester',
            message: 'Case Cant be Assigned to You!'
        })
        .fire();
    }
    $A.get("e.force:refreshView").fire();
});
$A.enqueueAction(set_action);       
   }
  })

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;     
    }

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

Button shows up in the page like this

enter image description here

When I click the Close case button changes the owner and set the case status to closed and shows the toast as Case is closed!. Both the functions are executed when I click Close Case. I was expecting it to just to close the case and the Owner should remain as is. I am not sure what I am missing here and why both the functions are executed on click of a button.

3
  • onclick="{!comp.updateOwnership}" the second button should be onclick="{!c.updateOwnership}"?
    – Raul
    Apr 6, 2020 at 9:55
  • I changed them comp to test as I had the same behavior with c.
    – user81642
    Apr 6, 2020 at 9:58
  • @Raul I updated my code to 'c' for component and updated the question
    – user81642
    Apr 6, 2020 at 12:37

2 Answers 2

1

Controller methods are always referred to with c, not any other value.

<lightning:button variant="brand" label="Take Ownership" onclick="{!cf.updateOwnership}"  />

...

var set_action = comp.get("c.updateOwner");

If this doesn't fix it, there's something else wrong with your code, but I don't see it. You may have a copy-paste error or something else.

3
  • I updated the code to just use component 'c' and 'h' for helper and refreshed by screen and updated the question as well. Can you please help me figure out what I am missing here
    – user81642
    Apr 6, 2020 at 12:36
  • Found the issue it is the workflow rule that was causing the change
    – user81642
    Apr 6, 2020 at 12:54
  • Can you please help me with this question salesforce.stackexchange.com/questions/308776/… the auto poultae works one time doesnt work other time
    – user81642
    Jun 9, 2020 at 12:25
1

Why are you referencing your updateOwnership js method like this ?

<lightning:button variant="brand" label="Take Ownership" onclick="{!comp.updateOwnership}"  />

normally js method should be referenced c.updateOwnership.

Therefore, I think this is not the origin of your issue. don't you have any other workflow, process builder or trigger firing when you change the owner ? I suggest you place console.log on your Component controller and system.debugs on your apex controller to see what is exactly being executed.

2
  • I updated my question with the updated code and the issue I am seeing
    – user81642
    Apr 6, 2020 at 12:36
  • As you said it was workflow rule causing the issue. Thanks
    – user81642
    Apr 6, 2020 at 12:54

You must log in to answer this question.

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