Skip to content

Instantly share code, notes, and snippets.

@CubeYogi
Last active February 27, 2020 06:50
Show Gist options
  • Save CubeYogi/159bde7d23a55ce89b522a378a0fe82b to your computer and use it in GitHub Desktop.
Save CubeYogi/159bde7d23a55ce89b522a378a0fe82b to your computer and use it in GitHub Desktop.
//Replace contact module API name here
contact_module_api_name = "contacts";
//Replace lead module API name here
leads_module_api_name = "leads";
//Replace newly created module API name here
duplicate_module_api_name = "duplicates_within_contacts_and_leads";
//Replace contact module email field API name here
contact_email_field_api_name = "email";
//Replace contact module id field API name here
contact_id_field_api_name = "id";
//Replace contact module name field API name here
contact_name_field_api_name = "last_name";
//Replace lead module email field API name here
lead_email_field_api_name = "email";
//Replace lead module id field API name here
lead_id_field_api_name = "id";
//Replace lead module name field API name here
lead_name_field_api_name = "last_name";
//Replace duplicate module email field API name here
duplicate_email_field_api_name = "email";
//Replace duplicate module id field API name here
duplicate_id_field_api_name = "id1";
//Replace duplicate module name field API name here
duplicate_name_field_api_name = "name";
//Replace duplicate module module name field API name here
duplicate_module_name_field_api_name = "module_name";
//Replace lead module first name field API name here
lead_first_name_field_api_name = "First_Name";
//Replace contact module first name field API name here
contact_first_name_field_api_name = "First_Name";
//Add module name as leads for picklist value
lead_module_name = "Leads";
//Add the Module name as Contacts for picklist value
contact_module_name = "Contacts";
//fetch all contact records from contact module
contact_entries = zoho.crm.getRecords(contact_module_api_name);
// info contact_records;
//fetch all leads records from leads module
lead_entries = zoho.crm.getRecords(leads_module_api_name);
 //Executes only when the lead_records is not empty
 if(lead_entries != null && lead_entries.size() > 0)
 {
  //List stores Email of leads which are not duplicated
  original_lead_records_email_list = List();
  //List stores duplicated leads records
  duplicated_lead_records_list = List();
  //List stores duplicated contacts records which is present in leads module
  contact_duplicate_list = List();
  
  for each lead in lead_entries
  {
   //Executes only when contact email is not empty
   if(lead.get(lead_email_field_api_name) != null && lead.get(lead_email_field_api_name) != "")
   {
    //Get the Email from Contact module
    lead_email = lead.get(lead_email_field_api_name);
   }
   //Checks whether the lead email is present in the original_lead_records_email_list list
   if(original_lead_records_email_list.contains(lead_email))
   {
    //Add duplicated record from leads module
    duplicated_lead_records_list.add(lead);
    //Get necessary details you want from lead record
    if(lead.get(lead_name_field_api_name) != null && lead.get(lead_name_field_api_name) != "")
    {
     lead_last_name = lead.get(lead_name_field_api_name);
    }
if(lead.get(lead_first_name_field_api_name) != null && lead.get(lead_first_name_field_api_name) != "")
    {
     lead_first_name = lead.get(lead_first_name_field_api_name);
    }
    if(lead.get(lead_id_field_api_name) != null && lead.get(lead_id_field_api_name) != "")
    {
     lead_id = lead.get(lead_id_field_api_name);
    }
    if(lead.get(lead_email_field_api_name) != null && lead.get(lead_email_field_api_name) != "")
    {
     lead_email = lead.get(lead_email_field_api_name);
    }
lead_name=lead_first_name +" "+ lead_last_name;
    //Add the details you want to add into duplicated leads and contacts module
    update_lead_map = Map();
    update_lead_map.put(duplicate_email_field_api_name,lead_email);
    update_lead_map.put(duplicate_id_field_api_name,lead_id);
    update_lead_map.put(duplicate_module_name_field_api_name,lead_module_name);
    update_lead_map.put(duplicate_name_field_api_name,lead_name);
    //Create new record in newly created module
    create_duplicate_lead = zoho.crm.createRecord(duplicate_module_api_name,update_lead_map);
   }
   else
   {
    //Add Email which are not present in the record original_lead_records_email_list list
    original_lead_records_email_list.add(lead_email);
   }
  }
}
//Executes only when the contact_records is not empty
if(contact_entries != null && contact_entries.size() > 0)
{
  for each contact in contact_entries
  {
   //Executes only when lead email is not empty
   if(contact.get(contact_email_field_api_name) != null && contact.get(contact_email_field_api_name) != "")
   {
    //Get the Email from Leads module
    contact_email = contact.get(contact_email_field_api_name);
   }
   if(original_lead_records_email_list.contains(contact_email))
   {
    //Add duplicated record from contact module
    contact_duplicate_list.add(contact);
    //get the related details from Contact record
    if(contact.get(contact_name_field_api_name) != null && contact.get(contact_name_field_api_name) != "")
    {
     contact_last_name = contact.get(contact_name_field_api_name);
    }
if(contact.get(contact_first_name_field_api_name) != null && contact.get(contact_first_name_field_api_name) != "")
    {
     contact_first_name = contact.get(contact_first_name_field_api_name);
    }
    if(contact.get(contact_id_field_api_name) != null && contact.get(contact_id_field_api_name) != "")
    {
     contact_id = contact.get(contact_id_field_api_name);
    }
    if(contact.get(contact_email_field_api_name) != null && contact.get(contact_email_field_api_name) != "")
    {
     contact_email = contact.get(contact_email_field_api_name);
    }
contact_name=contact_first_name +" "+ contact_last_name;
    // Add the data you need to add in to duplicated leads and contact module
    update_contact_map = Map();
    update_contact_map.put(duplicate_email_field_api_name,contact_email);
    update_contact_map.put(duplicate_id_field_api_name,contact_id);
    update_contact_map.put(duplicate_module_name_field_api_name,contact_module_name);
    update_contact_map.put(duplicate_name_field_api_name,contact_name);
    create_duplicated_contact = zoho.crm.createRecord(duplicate_module_api_name,update_contact_map);
   }
  }
 }
info "/**** List of Leads module records which are duplicated in Contacts module****/";
info contact_duplicate_list;
info "/**** Duplicated records from Leads module****/";
info duplicated_lead_records;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment