Change Owner / Update record using Lightning Custom Button Salesforce
This post is related to changes the Owner or update the record from detail page in lightning. Actually we have standard button to change the owner of the record but when we click on the lighting button it will populate the popup and ask for user to change.
But here the requirement is change the logged in user direcly when we click on the button. So here we created Custom lightning component with custom Quick action to complete this task.
Standard Change Owner in Lightning |
Lightning Button Pagelayout |
Change owner Lightning Button |
<!--change owner lighting Component -->
Component Name : changeowner.cmp
<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction" controller="ChangeOwnerClass" access="global" >
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
</aura:component>
force:hasRecordId : It is used to directly get Record it into the controller.
force:lightningQuickAction : it is used to create QuickAction and use it in Salesforce Lightning.
File Name : Changeownercontroller.js
({
doInit : function(component, event, helper) {
var caseId = component.get("v.recordId");
var action = component.get("c.changeOwnerMethod");
action.setParams({
caseId : caseId
});
action.setCallback(this, function(response) {
if(response.getState() === "SUCCESS") {
console.log("Case Owner Changed To Current login User");
var rec = response.getReturnValue();
console.log(rec.OwnerId);
}
});
$A.enqueueAction(action);
$A.get('e.force:refreshView').fire();
}
})
FileName : ChangeOwnerClass
public class ChangeOwnerClass {
@AuraEnabled
public static Case changeOwnerMethod(Id caseId) {
if(caseId != null) {
Case c = [SELECT OwnerId FROM Case WHERE Id = :caseId];
c.OwnerId = UserInfo.getUserId();
//update case Ownerid with loggedin userid.
update c;
return c;
}
return null;
}
}
Thanks for the post. How you created quick action. I have same requirement in lead object.
ReplyDeleteCan you add step by step process. It could be easy to understand
How to achieve this from List view?
ReplyDeleteUsing Bulk Object Field Creator, a Salesforce app admins/dev can easily Update Bulk Fields in few clicks
ReplyDeleteThanks for the post.
ReplyDeleteSalesforce CPQ Training
Salesforce CPQ Online Training