Changing Ownership of Multiple Related Records concurrently
Business scenario:
Each of us have our strengths and weaknesses. Someone skilled in sales might not necessarily be better in management. It goes to show that in order to successfully run an enterprise, you need an amalgamation of various different talents that work together. Marketing, Sales, Negotiation, Support, Development, etc. All of them have their roles cut out clearly.
As such, lets say there's a guy from the Sales team who takes care of the leads and converting them into deals. Now, that guy has a lot of work to do in converting the lead. Assigning tasks, events, scheduling calls, emails, cases, quotes, purchase orders, etc. He might have to lay the groundwork for the guy who takes care of the negotiation part after the lead is converted. The initial owner of the deal and all the related records with the deal is the Sales guy, but when converted into a deal, shouldn't the ownership change for all of the records? But guess what, changing the deal ownership doesn't change the rest.
But we do have a workaround as you've guessed, since we explained things this far. You can use a function to transfer the ownership for all of the records simultaneously. And this works best when the function is created within a Workflow rule, which can be set to trigger "This rule will be executed when the field Leads Owner is updated to meet the condition(if any)." And there you have it.
Getting started with the function:
- Go to Setup > Developer Space > Functions > + Create New Function.
- Select Automation as Function type and click Next.
- Provide a name for the function. Add a description(optional).
- Copy the code given below.
- Click Edit Arguments.
- Enter the name as dealId and select the type as Int.
- Enter the name as dealownerid and select the type as Int.
- Click Save&Execute Script.
- Click Save.
The Code:
relatedcases = zoho.crm.getRelatedRecords("Cases", "Deals", input.dealId.toLong());
//info relatedcases;
for each ele in relatedcases
{
update = zoho.crm.update("Cases",ele.get("id"),{"Owner":input.dealownerid});
}
Adding to the workflow
- Go to Setup > Automation > Workflow Rules.
- Click '+ Create Rule'.
- Select the Module for which this custom function has to be added and give it a name and a description(optional).
- Select "On a record action" in the "When do you want to execute this rule?".
- Select "Field Update" and click "Repeat this workflow whenever a record is edited".
- Select "Deal Owner" and click Next.
- Select the Condition as "All Records".
- Select the checkbox "Repeat this workflow whenever a record is edited." and Click Next.
- Choose "Function" from Instant Actions.
- Select the option "Function" (Created by users from your organization).
- Select the required function and click Configure.
- Click Save and Associate.
- Save the workflow.
Note:
- Change the module name in the code from "Cases" to any other module you want.
- The above code works only for API V2.0 and not the previous version.
Found this useful? Try it out and let us know how it works! If you have questions, do not hesitate to ask! Share this with your team if you find it useful!