Welcome back everyone!
Recently we had made an announcement regarding the renaming of Custom Functions to Functions. Check this
link
to know the implications regarding the change.
Last week, we learnt how to create Purchase Orders for Individual Products instead of a Contact or Account, from a Sales Order. This week, let's look at a custom function that lets you update your Account and Contact records on the products involved in the transaction, for each related Invoice generated.
Business scenario:
Keeping a check on the working capital lets you have an overview of your inventory and you can make plans to maintain your stocks. The vendor management side of CRM helps you with that. However, moving further on your business, you would have to keep regular contact with some of your patrons. Not just patrons, for each deal made with a company(Account) or a contact, there is a need to keep records of the products/services involved in the transaction. Although there is a related list that lets you do that, all of the information are Inventory-centric and not Contact or Account-centric. In other words, some additional information about the product, the exact products involved in the deal cannot be captured perfectly.
This procedure is vital in a business as it helps in gathering data for analyzing and forming better long-term business strategies. Assume there are 2 Customers, A and B, who buy 3 products each. You create an 2 Invoices, one for each. Your inventory gets updated and shows the remaining stock, but it doesn't show to whom the products were delivered. Through this function, you can use the information from the Invoice to place the product details of the transaction within the Contact or Account(Company) record to which the Invoice was sent to.
Setting this function as a button is not a practical, so we suggest placing this function in a Workflow rule to incorporate this process into your business process.
Getting started with the custom function:
- Go to Setup > Automation > Functions > Configure Function.
- Select "Write your own".
- Provide a name for the function. For example: “Product details from Invoice to Contact/Account”. Add a description(optional).
- Select the module as "Invoice". Add a description (optional).
- Copy the code given below.
- Click “Edit arguments”.
- Enter the name as “invoiceId” and select the value as “Invoice Id”.
- Enter the name as “acctId” and select the value as “Account Id”.
- Enter the name as “contId” and select the value as “Contact Id”.
- Click Save&Execute Script.
- Click Save.
The script:
Code for Version 2.0 API:
respMap = zoho.crm.getRecordById("Invoices", input.invoiceId.toLong());
accountId = ifnull(respMap.get("Account_Name"),"").get("id");
contactId = ifnull(respMap.get("Contact_Name"),"").get("id");
productDet=ifnull(respMap.get("Product_Details"),"");
pdlist=List();
for each eachProd in productDet
{
productvalue = eachProd.get("product");
proid = productvalue.get("id");
mp=map();
mp.put("PRODUCTID",input.proid);
resp = zoho.crm.updateRelatedRecord("Products",proid,"Accounts",accountId.toLong(),mp);
info resp;
resp1 = zoho.crm.updateRelatedRecord("Products",proid,"Contacts",contactId.toLong(),mp);
info resp1;
}
Code for Version 1.0 API:
authtoken = "xxxxxxxxxxxxxxxxxxx";
productDetails = respMap.executeXPath("/response/result/Invoices/row/FL[@val ='Product Details']/product").toList("-|-");
countVal = 0;
paramsmap = map();
paramsmap.put("authtoken", authtoken);
paramsmap.put("scope", "crmapi");
paramsmap.put("id", input.acctId);
paramsmap.put("relatedModule", "Products");
test = "<Products>";
for each item in productDetails
{
countVal = (countVal + 1);
productIdStr = item.executeXPath("/product/FL[@val ='Product Id']/text()");
info productIdStr;
productId = productIdStr.toLong();
test = (test + "<row no=\"" + (countVal)) + "\">";
test = test + "<FL val=\"PRODUCTID\">" + productId + "</FL>";
test = test + "</row>";
}
test = test + "</Products>";
paramsmap.put("xmlData", test);
info url;
paramsmap1 = map();
paramsmap1.put("authtoken", authtoken);
paramsmap1.put("scope", "crmapi");
paramsmap1.put("id", input.contId);
paramsmap1.put("relatedModule", "Products");
test1 = "<Products>";
for each item in productDetails
{
countVal = (countVal + 1);
productIdStr = item.executeXPath("/product/FL[@val ='Product Id']/text()");
info productIdStr;
productId = productIdStr.toLong();
test1 = (test1 + "<row no=\"" + (countVal)) + "\">";
test1 = test1 + "<FL val=\"PRODUCTID\">" + productId + "</FL>";
test1 = test1 + "</row>";
}
test1 = test1 + "</Products>";
paramsmap1.put("xmlData", test1);
info url1;
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!
Update: As you must be aware, API V1.0 will be deprecated and support for version 1.0 API will be available only till Dec 31, 2018. Version 1.0 compatible Functions will continue to work until Dec 31, 2019. You're advised to migrated to API Version 2.0 at the earliest. Check this
announcement
for more. We've updated the post to include the Version 2.0 compatible Function.