Business Requirement
Change owner is a standard functionality in Salesforce, which brings a flyout as shown below.
This functionality doesn’t allow us to change a reason (or any other fields) while changing the owner for a record.
In order to overcome this limitation, we can create a custom action button to change the owner for the record and make the user enter a reason for the owner change on Screen Flow.
STEPS TO ACHIEVE THE REQUIREMENT :
- Create two Custom Fields (lookup and formula fields)
- Create Custom Notification (for in-app notification)
- Create an Email Template
- Create an Email Alert
- Create Screen Flow
- Create Custom action button
Create two Custom Fields
-
Click on Gear Icon at the top. Go to Setup.
-
Select Object Manager → In the Quick find search and Select Lead.
-
Select Fields and Relationships → Click New.
-
Select Lookup Relationship → Click Next.
-
For Related to option Select User as the Object → Click Next.
-
Label the field as Lead Owner → Click Next.
-
Check the Visibility for the required Profiles. For the Profiles which need only read access check both Visibility and Read Only.
-
This field is created only to use in the screen flow and do not want to add to the page layout → Uncheck the checkbox and Click Save and New.
-
For the second custom field, select Formula as Data Type → Click Next.
-
Select Text as Data Type and Enter Field Label as Lead Owner and Field Name(API Name) as Lead Owner Formula → Click Next.
-
The Owner can be a Queue or an User. The below formula returns the non blank value of either of the Queue Name or the Owner's full name (concatenating first and last names).
BLANKVALUE( Owner:Queue.QueueName , Owner:User.FirstName + " " + Owner:User.LastName)
-
Click Next.
-
Select the Profiles to which you want to grant the access.
-
Add the field to the Page Layout → Click Save.
CREATE CUSTOM NOTIFICATION
-
In the Quick find search and Select Custom Notification → Click New.
-
You will see a popup. Enter Custom Notification Name as Change Lead Owner Notification → Under Supported channels Check Desktop and Mobile → Click Save.
CREATE EMAIL TEMPLATE
-
From the Quick find search and Select Classic Email Template → Click New Template.
-
Select Custom (Without Using Classic Letterhead) radio option → click Next.
-
Check on Available for Use Option → Enter Email Template Name as Lead Owner Change Template → Template Unique name (API Name) will populate automatically.
-
Add Subject as Lead Assigned, Click Next.
-
Copy Paste the below content in HTML Template
The Lead '<a href={!Lead.Name}' has been assigned to you.
-
For Text-Only Email Content click Copy text from HTML Version Button.
-
It will show a warning message → Click Ok.
-
Click Save.
CREATE AN EMAIL ALERT
-
From the Quick find search and Select Email Alerts → Click New Email Alert.
-
Add Description as Email Alert for Lead Owner Change. Unique Name will be populated automatically.
-
Select Object as Lead, For Email Template click the search icon and choose Lead Owner Change Template.
-
For Recipient Type Select Owner.
-
Under available recipients select Lead Owner and Click Add button.
-
Click Save.
-
Now The Email Alert is ready
CREATE SCREEN FLOW
-
From the Quick find search and Select Flows → Click New Flow.
-
Select Screen flow → Click Create.
-
By default the layout is set to Auto Layout.
-
Click New Resource on the left side.
-
Select Resource Type as Variable → Enter API name as recordId → Select Data Type as Record → Select Object as Lead → Check on Available for Input → Click Done.
-
Select (+) icon → Select Screen Element.
-
Name the Screen Element as Change Owner Screen → Check off Show Header. If needed, we can enter the header too.
-
Under Configure footer Choose Use a Custom Label in Next (or) Finish Button → Enter Change Owner.
-
Hide Previous Button and Pause button in footer.
-
Drag and Drop Display Text Component onto the screen.
-
Enter API name as CaptureLeadID → Insert {!recordId} in Resource.
-
Click Set Component Visibility for the Display Text component→ Choose All Conditions Are Met (AND).
-
Enter recordId>Lead ID Is Null True → Click Done.
-
Drag and Drop Lookup component below to the Display Text Component.
-
Enter LeadOwner as API name → Enter Lead_Owner__c as field API name (which is the API name of the custom lookup field we created) → Enter Owner as Label → Enter Lead as Object API name →Select {!$GlobalConstant.True} in Required.
-
Click Advanced → Check Manually assign variables. Select Record Id → Click New Resource.
-
Select Variable as Resource Type → Select Text as Data Type → Enter OwnerId as API name → Click Done.
-
For the Record Name → Click New Resource.
-
Select Variable as Resource Type → Select Text as Data Type as → Enter OwnerName as API name → Click Done.
-
Drag and Drop Text component below to the lookup component.
-
Enter Label as Reason → Check on Required.
-
Drag and Drop checkbox component below to the Reason Component.
-
Enter Label as Send Email Notification.
-
Click Done.
-
Click (+) icon → Select Update Records Element.
-
Enter Update Lead as Label → Select Specify Conditions to identify records and set fields individually.
-
Select Lead as Object → Select All Conditions Are Met (AND) → Enter filter condition as ID Equals to recordID > Lead ID.
-
Under Set Values for the Lead Records, Select Reason_for_Owner_Change_c as the field → Select Reason as Value.
-
Click Add Field.
-
Select Owner ID as the field → Select Owner ID variable as Value.
-
Click Done.
-
Click (+) icon → Select Get Records.
-
Enter Label as Get Notification Type → Select Custom Notification Type as Object → Choose condition requirements as All Conditions Are Met (AND) → Choose Developer Name as Field, Equals as Operator and Change_Lead_Owner_Notification(API name of the custom notification we created) as Value.
-
Choose Ascending as sort order → sort by Id → Choose only the First Record in How many records to Store → Choose Automatically store all fields in How to store Record Data.
-
Click Done.
-
Click (+) icon → Select Assignment Element.
-
Enter Assign Lead Owner as Label. Under Set Variable Values, Click on Variable and Click New Resource.
-
Select Variable as Data Type → Enter LeadOwnerIds as Label → Select Text as Data Type → Check on Allow Multiple Values → Click Done.
Note: This is a collection variable. Recipients for Custom Notifications expect collection.
-
Choose the Add as Operator → Select Owner Id variable as Value.
-
Click Done.
-
Click (+) icon → Select Action Element.
-
Enter Send in Search Actions → Choose Send Custom Notification.
-
Enter Label as Notification for Lead Owner Change → Select Custom Notification Type ID From Get_Notoification_Type > Custom Notification Type Id.
-
For the Notification Body, Click on New Resource.
-
Select Text Template as Resource Type → Enter Notification Body as API name → By using insert Resource use lead field values and create Email Body → Click Done.
-
For the Notification Title, Select New Resource.
-
Select Resource type as Text Template → Enter API name as Notification_Title → Enter Lead Re-Assigned. In Body → Click Done.
-
For the Recipient IDs → Select LeadOwnerIds collection variable.
-
Check the Include button for Target ID and Add Lead Id.
-
Click Done.
-
Click (+) icon → Select Screen Component.
-
Enter Label as End Screen → Check off Show Header.
-
Expand Configure Footer → Choose Use a Custom Label in Next or Finish Button → Enter Done → Hide Previous and Pause buttons.
-
Drag and Drop Display Text component onto the screen → Enter API name as EndScreen → Enter Lead Owner has been changed to {!OwnerName} as text value. (Select Owner name by inserting the resource).
-
Click Done.
-
Click (+) icon → Select Decision Element.
-
Enter Label as Email Notification → Enter Outcomes detail Label as Send Email → Choose Send Email Notification Equals to True → Click Done.
-
Click (+) icon under the Send Email → Click Action Element.
-
Select Type for Filter by → Select Email Alert.
-
In search email alerts, Select Email Alert for Lead Owner Change → Click Done.
-
Enter Label as Email Alert → API Name will Auto populate.
-
Select Lead ID in Record ID.
-
Click Done.
-
When a flow is launched from a custom button, the flow runs in User Context by default. Check this link for more information on which context a flow runs depending on the method of launch.
-
The User who runs the screen flow should have access to the Custom Notification Type in order to send a custom In-app notification. But Profiles cannot be given access to Custom Notifications as of now.
-
To overcome this issue we will run the flow in System context - with Sharing-Enforces Record Level Access.
-
When the flow runs in system context with sharing, the flow respects org-wide default settings, role hierarchies, sharing rules, manual sharing, teams, and territories. But it doesn’t respect object permissions, field-level access, or other permissions of the running user.
-
Click Gear Icon at the top left corner.
-
Click Show Advanced.
-
Select System context - with Sharing-Enforces Record Level Access in How to Run the Flow.
-
Click the Save button at the top right corner.
-
Enter Flow Name as Lead Owner Change Screen Flow → Click Save.
-
Click Activate.
CREATE CUSTOM ACTION BUTTON
-
Go to Object Manager → In the Quick find search and Select Lead.
-
Select Buttons, Links and Actions→ Click New Action.
-
Select Action Type as Flow.
-
Select Lead Owner Change Screen Flow → Enter Label as Change Owner.
-
Go to any Lead record page → Click Gear icon → Click Edit Page.
-
Select the Highlight Panel → Click Add Actions.
-
Select Change Owner under Quick Actions → Click Done.
-
Move the Change Owner button to the top.
-
Click Save.
-
Go to the previous Lead Record Page → Change Owner Button will be displayed on the page.
-
Remove Standard Owner Field from the Lead Page Layout → Click Gear Icon → Click Edit Object.
-
Select Page Layout → Select Lead Layout.
-
Click (-) icon on the Standard Lead Owner field.
-
Add Lead Owner Formula field in the place of Standard Lead Owner field in the Page Layout.
-
Click Save.
Testing Flow :
-
Select any Lead Record → Currently this record owner is Saichand Ballagiri → In the Lead Record Page Click Change Owner Button
-
Select the new Owner → Add Reason for Changing Owner (Enter Lead is based on North Side) → Check on Send Email Notification (If it is checked it will send Mail to the new owner) → Also It sent In app notification to the new owner.
-
Click Change Owner.
-
The screen displays that Lead Owner has been changed to Periakaruppan Ganesan → Click Done.
-
The new Lead Owner will receive an In App notification mentioning a Lead has been Re-Assigned to him.
-
You will receive Email Notification like below.
Note
- The Standard OwnerId field cannot be used in a Lookup Component in Screen flow. So a Custom lookup field was created and used in the screen flow.
WRAPPING IT UP
In this Blog we have covered how to add a reason when changing the owner of a lead record and send an In app Notification and Email to the New owner.
Leave a Comment