Welcome back everyone!
Business scenario
Envision a scenario of you running a real estate business. The Accounts module contains records of all the partners associated with your company. For instance, Construction companies. However, you deal two types of real estates that you need a Custom Module for each. Let's say "For Rent" and "For Sale".
The three modules "Accounts", "For Rent" and "For Sale" are interlinked closely. A Construction company can build properties for rent and for sale. Similarly, properties for rent can be built by a lot of different companies. Hence, sync of data between the two makes life easier for all. Have the function executable by setting up a button in the "For Rent" and "For Sale" module records details page.
Through this function, you can update the records in Accounts module with information from records in a custom module. For example: A construction company "XYZ Builders" has 2 properties for rent and 2 for sale. The properties that are for rent and sale are sold 1 each. This particular information would be updated in the custom modules, but shouldn't it also be updated in the Accounts module? After all, it adds to the precision and effectiveness of running your business.
Getting started with the custom function:
- Go to Setup > Customization > Modules > Deals > Links and buttons > Create new button.
- Provide a name for the button. For example: "Update Account details". Add a description(optional).
- Select the placement of the button as View page.
- Select the action to be performed as "Writing function".
- Copy the code given below.
- Click “Edit arguments”.
- Enter the name as “acctId” and select the value as “Account Id”.
- Click Save&Execute Script.
- Save the script.
- Select the profiles who can view this button.
- Click Save.
The Code:
The following code is to roll up the rent amount for a particular apartment(record in 'For Rent' module).
----------------------------------------------------------------------------------------------------------------------------------
resp = zoho.crm.getRelatedRecords("Rented", "Accounts", input.acctId);
//info resp;
totalsb = 0.0 ;
totalpn = 0.0;
count = 1 ;
firstcount = 1;
pnvalue = 0.0;
sbvalue = 0.0;
datelist = List();
for each order in resp
{
totalsb = totalsb + ifnull(order.get("SB"),"0.0").toDecimal();
totalpn = totalpn + ifnull(order.get("PN"),"0.0").toDecimal();
if( firstcount == 1)
{
datelist.add(order.get("Created_Time").toDate());
}
if( resp.size() == firstcount)
{
datelist.add(order.get("Created_Time").toDate());
}
if( count != 1)
{
sbvalue = sbvalue + ifnull(order.get("SB"),"0.0").toDecimal();
pnvalue = pnvalue + ifnull(order.get("PN"),"0.0").toDecimal();
}
count = count + 1;
firstcount = firstcount + 1 ;
}
diff = (days360(datelist.get(1),datelist.get(0))/7).toLong();
lastsn = sbvalue / diff ;
lastpn = pnvalue / diff ;
mp=map();
mp.put("SB_Total",totalsb);
mp.put("PN_Total",totalpn);
mp.put("SB_Sold_week_number",lastsn);
mp.put("PN_Sold_week_number",lastpn);
update = zoho.crm.update("Accounts", acctId, mp);
info mp;
info update;
return "Success";
----------------------------------------------------------------------------------------------------------------------------------
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.
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!