Function #41: Sync Associated Subforms!

Function #41: Sync Associated Subforms!



Welcome back everyone!

The last custom function showed how to update a Contact with Product details from it's Related list in Deals. This week, let's look at a function that lets you update subform records in two modules simultaneously when one of them is updated.

Business scenario

Let's look at how subform helps in an education institution that has deployed Zoho CRM. National Public School, Austin (made up, of course!) has set up Zoho CRM and it follows the same relationship pattern of "Students", "Teachers", "Courses", "Timings"(assume that the said names are Modules in the CRM). Let's say there are 10 students, 3 teachers, 6 classes and 8 hours per day. All these are all interlinked.

Sales data is no different. They are often updated in different modules, such as "Contacts", "Deals", "Products", etc. Hence, it becomes necessary to have many-to-many record associations.Subforms in Zoho CRM helps enable association of multiple data records across modules.

The function for this week lets users sync two subforms in the Accounts module, such that if one subform is updated, the other one gets updated too. Let's say there are two branches for a particular organization. Instead of having the entire organization's info in a single Account record, it's smarter to create multiple Accounts for branches. Hence, you need to associate the Main branch with a sub-branch as well as the sub-branches themselves.

In this function, assume 2 Accounts records, A and B, have subforms each. Every time a row (Account B) is added to subform on account A, the function adds a new subform row (Account A) to the subform on Account B. In other words, when a new branch is opened, the records of the main branch and the other branches are updated. In addition, the information about the rest of the branches are updated in the subform under the new branch.

Getting started with the custom function

  1. Go to Setup > Automation > Actions > Functions > Configure Function > Write your own.
  2. Provide a name for the function. For example: "Auto-Subform updates". Add a description (optional).
  3. Copy the code given below.
  4. Click “Edit arguments”.
  5. Enter the name as “acctId” and select the value as “Account Id”.
  6. Click Save&Execute Script to check the code.
  7. Save the function.

The Code:

-----------------------------------------------------------------------------------------------------------------------

acctDetails = zoho.crm.getRecordById("Accounts", input.acctId.toLong());
subformDetails = ifnull(acctDetails.get("Related_Accounts"),"");
count = 1;
sub_forms = List();
for each form in subformDetails
{
if ( count == 1)
{
formacctname = form.get("Related_Account").get("name");
formacctId = form.get("Related_Account").get("id");
info "formacctId:" +formacctId ;
formaccttype = ifnull(form.get("Related_Account_Type"),"");
formacctdesc = ifnull(form.get("Description"),"");
subform = Map();
subform.put("Related_Account",input.acctId);
sub_forms.add(subform);
}
count = count + 1;
}
formacctDetails = zoho.crm.getRecordById("Accounts", formacctId.toLong());
formsubformDetails = ifnull(formacctDetails.get("Related_Accounts"),"");
for each forms in formsubformDetails
{
info "formsId:" + forms.get("Related_Account").get("id");
subform1 = map();
subform1.put("Related_Account",forms.get("Related_Account").get("id"));
subform1.put("Related_Account_Type", ifnull(forms.get("Related_Account_Type"),""));
subform1.put("Description",ifnull(forms.get("Description"),""));
sub_forms.add(subform1);
}
parammap = Map();
parammap.put("Related_Accounts",sub_forms);
update = zoho.crm.update("Accounts",formacctId.toLong(),parammap);
info parammap;
info update;

-----------------------------------------------------------------------------------------------------------------------

Note:

  • The code given above works only for V2 version of Zoho APIs. Please note that the code WILL NOT work for Version 1.0 APIs.
  • Use the function in a Workflow Rule, to automate the process of update.

Found this useful? Try it out and let me know how it works! If you have questions, do not hesitate to ask! Share this with your team if you find it useful. Do check out other custom functions shared in this series here.

See you all next week with another interesting custom function. Ciao!


    • Sticky Posts

    • Function #41: Sync Associated Subforms!

      Welcome back everyone! The last custom function showed how to update a Contact with Product details from it's Related list in Deals. This week, let's look at a function that lets you update subform records in two modules simultaneously when one of them is updated. Business scenario Let's look at how subform helps in an education institution that has deployed Zoho CRM. National Public School, Austin (made up, of course!) has set up Zoho CRM and it follows the same relationship pattern of "Students",