This is an article with the snippet of code required to duplicate or clone a record in Zoho CRM using Zoho Deluge.
Why?
The aim here is to have a scheduled task that on the 1st of every month, takes the invoices for the month before, and duplicates each one for the new month.
How?
First you have to get all the applicable fields from the settings API. In API v2, you have to do it this way apparently. So setup a Zoho Oauth connector (see the end of this article if you don't know how), I'm going to call mine myZohoConnector, with the scope: Then check your URL is correct. The format for API v2 is:
https://www.zohoapis.{eu or com}/crm/v2/settings/fields?module={module-name}
- https://www.zohoapis.{eu or com}/crm/v2/settings/fields?module={module-name}
/* Function: fn_Button_CloneInvoice Purpose: This function duplicates a CRM invoice Parameters: p_InvoiceID (CRM ID of the invoice) Returns: Nothing Author: Joel Lipman Date Created: 2020-03-17 */ // get all field names for the Invoices module via API l_FieldApiNames = List(); r_Fields = invokeurl [ url :"https://www.zohoapis.eu/crm/v2/settings/fields?module=Invoices" type :GET connection:"myzohoconnector" ]; if(!isnull(r_Fields.get("fields"))) { for each r_Field in r_Fields.get("fields") { l_FieldApiNames.add(r_Field.get("api_name")); } } // get details of original invoice r_Invoice = zoho.crm.getRecordById("Invoices", p_InvoiceID); // start map m_Clone = Map(); // loop through field names for each v_FieldName in l_FieldApiNames { if(!isNull(r_Invoice.get(v_FieldName))) { m_Clone.put(v_FieldName,r_Invoice.get(v_FieldName)); } } // now override fields specific to the newly cloned/duplicated record m_Clone.put("Invoice_Status","Repeated Invoice"); m_Clone.put("Invoice_Date",zoho.currentdate.toString("yyyy-MM-dd")); m_Clone.put("Due_Date",zoho.currentdate.addMonth(1).toStartOfMonth().subDay(1).toString("yyyy-MM-dd")); // create the record r_Clone = zoho.crm.createRecord("Invoices",m_Clone); // get cloned invoice ID if(!isNull(r_Clone.get("id"))) { v_Duplicate_InvoiceID = r_Clone.get("id"); }
- /*
- Function: fn_Button_CloneInvoice
- Purpose: This function duplicates a CRM invoice
- parameters: p_InvoiceID (CRM ID of the invoice)
- Returns: Nothing
- Author: Joel Lipman
- Date Created: 2020-03-17
- */
- // get all field names for the Invoices module via API
- l_FieldApiNames = List();
- r_Fields = invokeUrl
- [
- url :"https://www.zohoapis.eu/crm/v2/settings/fields?module=Invoices"
- type :GET
- connection:"myzohoconnector"
- ];
- if(!isnull(r_Fields.get("fields")))
- {
- for each r_Field in r_Fields.get("fields")
- {
- l_FieldApiNames.add(r_Field.get("api_name"));
- }
- }
- // get details of original invoice
- r_Invoice = zoho.crm.getRecordById("Invoices", p_InvoiceID);
- // start map
- m_Clone = Map();
- // loop through field names
- for each v_FieldName in l_FieldApiNames
- {
- if(!isNull(r_Invoice.get(v_FieldName)))
- {
- m_Clone.put(v_FieldName,r_Invoice.get(v_FieldName));
- }
- }
- // now override fields specific to the newly cloned/duplicated record
- m_Clone.put("Invoice_Status","Repeated Invoice");
- m_Clone.put("Invoice_Date",zoho.currentdate.toString("yyyy-MM-dd"));
- m_Clone.put("Due_Date",zoho.currentdate.addMonth(1).toStartOfMonth().subDay(1).toString("yyyy-MM-dd"));
- // create the record
- r_Clone = zoho.crm.createRecord("Invoices",m_Clone);
- // get cloned invoice ID
- if(!isNull(r_Clone.get("id")))
- {
- v_Duplicate_InvoiceID = r_Clone.get("id");
- }
Additional Note(s):
- Invoice_Date in this example is the current date in format yyyy-MM-dd
- Due_Date in this example is at the end of the current month: so add 1 month, go to the start of that month, subtract 1 day. .toEndOfMonth() used to work but appears to be deprecated.
- Invoice_Ref_ID is excluded from being copied as it was an autonumber in our example.
- zohoapis.eu is used in the above example for a client on the EU datacenters. If your datacenter is in the US then this should be zohoapis.com.
Additional Additional(s):
I haven't done any benchmark tests on this to compare but looking at different ways to exclude elements from a list:
info l_FieldApiNames; l_FieldApiNames = {"a","b","c","d","e"}; l_ExcludeFields = {"b","d"}; for each v_FieldName in l_ExcludeFields { l_FieldApiNames.removeElement(v_FieldName); } info l_FieldApiNames; // yields: // a,b,c,d,e // a,c,e // compared to: for each v_FieldName in l_FieldApiNames { if(!l_ExcludeFields.contains(v_FieldName)) { ... do stuff here ... } } // notes: // no need to check if element exists on "removeElement"
- info l_FieldApiNames;
- l_FieldApiNames = {"a","b","c","d","e"};
- l_ExcludeFields = {"b","d"};
- for each v_FieldName in l_ExcludeFields
- {
- l_FieldApiNames.removeElement(v_FieldName);
- }
- info l_FieldApiNames;
- // yields:
- // a,b,c,d,e
- // a,c,e
- // compared to:
- for each v_FieldName in l_FieldApiNames
- {
- if(!l_ExcludeFields.contains(v_FieldName))
- {
- ... do stuff here ...
- }
- }
- // notes:
- // no need to check if element exists on "removeElement"
Zoho OAuth Connector
Just in case you don't know how to setup a Zoho OAuth Connector, here's how you do it in CRM:
- Login to Zoho CRM as an administrator and go to Setup
- Under Developer Space click on "Connections"
- Under Pick your Service click on "Zoho OAuth" (NOT "Zoho" - there are 2 Zoho options)
- Give the connection a name such as "myZohoConnector"
- Tick the applicable Scope(s): for example: "ZohoCRM.settings.fields.READ"
- Click on "Create" and then on "Accept" in the popup.
- You should be redirected to a page which lists the "status" as connected as well as a snippet of Deluge for the usage
Source(s):