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.

Update record lightning button
Standard Change Owner in Lightning
In classic saleforce we used custom javascript buttons to execute the functionalites but lighting not support the javascript so here we created custom component with Quick action and added in the pagelayout so we can see it in lighting.and add the button in "Salesforce Mobile and Lightning Experience Actions" section.
Add caption
Lightning Button Update record Pagelayout
Lightning Button Pagelayout



Lightning Button Update Owner 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;
    }
}




Comments

  1. Thanks for the post. How you created quick action. I have same requirement in lead object.

    Can you add step by step process. It could be easy to understand

    ReplyDelete
  2. How to achieve this from List view?

    ReplyDelete
  3. Using Bulk Object Field Creator, a Salesforce app admins/dev can easily Update Bulk Fields in few clicks

    ReplyDelete

Post a Comment

Popular posts from this blog

Community/Experience Cloud Interview Questions

Script to delete multiple records using Apex salesforce