One of my client has more than 15 Module and he wants to move Convert Data from almost Every Module to all other Modules. To avoid all these tedious tasks, I created a generalized Code to Convert from One Module to Another Module.
All you need is to change source_module and dest_module names and create a parameter of module_id.
Also need to add some fields if there is a change in Field Name and what you dont want to copy information. (upto Line 24)
I added as many pieces of information I can to describe the logic.
Note:
It uses API V2. Using API V2, we need API name instead of field name, however, this code grab those API names. Only thing is if Field Name is different between modules, you need to add it after line 16.
Many may think why I do the simple job in more complex way, but this generalized code saves a lot of time, while I need to make convert buttons more than 50.
===================================================
// This is the Only Parameter Required
// module_id
// The Module API NAME from where the data will be coppied
source_module = "Leads";
// The Module API NAME to where the data will be coppied
// It is a custom module
dest_module = "Applicants";
// If there is a change of Field name in Source -> Dest Module,
// FORMAT
// field_maps.put(Source Module Field name, Destination Module Field name);
// field_maps.put("Lead Owner","Applicant Owner");
// If you don not want to copy some fields, just mention Destination Module Field name as ""
// field_maps.put("Modified Time","");
field_maps = Map:String();
field_maps.put("Modified Time","");
field_maps.put("Created Time","");
field_maps.put("Last Activity Time","");
field_maps.put("Modified By","");
field_maps.put("Created By","");
// If want to copy Owner Data, change these as required
// field_maps.put("Lead Owner","Applicant Owner");
//
// DO NOT TOUCH THESE LINES
//
// Retreiving Source Field Values of Specific ID
src_resp = zoho.crm.getRecordById(source_module,module_id.toLong());
// Generating Source Fields Map
m = Map();
m.put("module",source_module);
source_modulemeta = zoho.crm.invokeConnector("crm.modulemeta",m);
source_module_name = source_modulemeta.get("response").get("modules").get(0).get("module_name");
source_fields = source_modulemeta.get("response").get("modules").get(0).get("fields");
source_field_maps = Map();
source_field_names = list();
for each source_field in source_fields
{
source_field_maps.put(source_field.get("field_label"),source_field.get("api_name"));
source_field_names.add(source_field.get("field_label"));
}
// Generation Destination Fields Map
m = Map();
m.put("module",dest_module);
dest_modulemeta = zoho.crm.invokeConnector("crm.modulemeta",m);
dest_module_name = dest_modulemeta.get("response").get("modules").get(0).get("module_name");
dest_fields = dest_modulemeta.get("response").get("modules").get(0).get("fields");
dest_field_maps = Map();
dest_field_names = list();
for each dest_field in dest_fields
{
dest_field_maps.put(dest_field.get("field_label"),dest_field.get("api_name"));
dest_field_names.add(dest_field.get("field_label"));
}
// Coping the Data from Source Module to Destination Module
dest_map = Map();
for each source_field_name in source_field_names
{
dest_name = ifnull(field_maps.get(source_field_name),source_field_name);
src_field_api = source_field_maps.get(source_field_name);
dst_field_api = dest_field_maps.get(dest_name);
if(dst_field_api != null && src_field_api != null)
{
dest_map.put(dst_field_api,src_resp.get(src_field_api));
}
}
// Creating New Module Entry in Destination Module
dest_resp = zoho.crm.create(dest_module,dest_map);
dest_id = dest_resp.get("id");
//
// Copying Related Notes from Source Module to Destination Module
//
src_notes = zoho.crm.getRelatedRecords("Notes",source_module,module_id.toLong());
data_list = list();
for each src_note in src_notes
{
note_map = Map();
note_map.put("Note_Title",src_note.get("Note_Title"));
note_map.put("Note_Content",src_note.get("Note_Content"));
note_map.put("Owner",src_note.get("Owner"));
note_map.put("Parent_Id",dest_id);
note_map.put("se_module",dest_module);
zoho.crm.create("Notes",note_map);
}
//
// Delete Source Module Entry,
// If you want to delete, UNCOMMENT the following lines
//
// del_m = {"module":source_module,"id":module_id.toLong()};
// response = zoho.crm.invokeConnector("crm.delete",del_m);
//
// Open in New Window with New Entry ID
// Uncomment the following line if needed
// openUrl(url,"same window");
// Return Value if used in Buttons
// return "The Lead converted SUCCESSFULLY...";