Converting Non-Lead Records In Zoho CRM

Cross-module record conversion can be mind-numbing if done manually, especially when you’re dealing with a list of records. Don’t you wish that you can convert records from any one module to another in Zoho CRM in a single click of a button like the “Convert” button in Leads? Well, we’ll show you how you can build one that even works for custom modules!

Some of you may think “I don’t need to know this cause I’ll never need to do record conversions”. Before you hit the back button, it might interest you to know that some parts of this article can be useful to you, like how to convert related lists, activities, move attachment, notes and more. While I still have you here, let’s dive in!

Cool Ideas to use the Code

Here are some cool ideas on ways you can use the code in Zoho CRM:

  • Convert a Record from ANY module to ANY module (including custom modules).
  • “Undo Convert” of Contacts (converting Contacts back to Leads). Click here to jump straight to that.
  • For Deduplication Activities: You can use this code to copy over fields, convert Related Tasks / Meetings / Calls, Move Attachments, Notes and other Related Lists to a record before deleting the duplicate.

Checklist & Configuration

Create New Record

Get the necessary fields of the record in the original module (oldModule), then create a record in the destination module (newModule) with a map containing the fields. Get the ID of the new record you have just created for later use. You now have the new record in the new module with all the fields you need copied over.
				
					//Change below to the relevant old & new modules
oldModule = "Households";
newModule = "Businesses";

oldRecord = zoho.crm.getRecordById(oldModule,recordId);

//Get all Necessary Fields - add/modify as needed
recordName = oldRecord.get("Name");
email = oldRecord.get("Primary_Email");

//Create Record
rMap = Map();
rMap.put("Name",recordName);
rMap.put("Primary_Email",email);
newRecord = zoho.crm.createRecord(newModule, rMap);
newRecordId = newRecord.get("id");
info newRecordId;
				
			

The Great Migration

A conversion is not complete without migrating the related lists. The common related lists on a record are Contacts, Deals, Notes and Activities (Tasks, Events, Calls). This may differ on a case-to-case basis depending on your modules, but here, we will focus on the four.

Related Contacts & Deals

We migrate related Contacts by getting all related Contacts on the old module. If the related Contact list is not empty, we iterate through each Contact, remove its connection from the old module, then link to the new.
				
					allContacts = zoho.crm.getRelatedRecords("Contacts", oldModule, recordId);
for each c in allContacts
{
	delete = zoho.crm.updateRecord("Contacts", c.get("id"), {"Household" : ""});
	update = zoho.crm.updateRecord("Contacts", c.get("id"), {"Business" : newRecordId});
	info delete;
	info update;
}
				
			
The same is done for related Deals
				
					allDeals = zoho.crm.getRelatedRecords("Deals", oldModule, recordId);
if (allDeals.size() > 0)
{
  for each d in allDeals
  {
	  delete = zoho.crm.updateRecord("Deals", d.get("id"), {"Household_Name" : ""});
	  update = zoho.crm.updateRecord("Deals", d.get("id"), {"Business_Name" : newRecordId});
	  info delete;
	  info update;
  }
}
				
			

Related Notes

We don’t wanna lose any notes about how Joe’s favourite food is sushi now do we? When copying notes, we wanna make sure to copy both the Note Title and the Note Content. Then, we re-create the notes in the new record by sticking the new record ID in the “Parent_Id” and setting the “$se_module” to the new module.
				
					allNotes = zoho.crm.getRelatedRecords("Notes", oldModule, recordId);
if(allNotes.size() > 0)
{
	for each  n in allNotes
	{
		notesMap = {"Parent_Id":newRecordId,"Note_Title":n.get("Note_Title"),"Note_Content":n.get("Note_Content"),"$se_module":newModule};
		create = zoho.crm.createRecord("Notes",notesMap);
		info create;
	}
}
				
			

Activities

Let’s not lose track of what we are in the midst of doing, going to do, or have already done in the past. “Activities” consists of Tasks, Events and Calls. All Open Activities (current & future) are stored in the “Activities” module. To migrate them to the new record, we need to update certain fields on the related activity records.
  • “$se_module” is updated with the new module
  • “What_Id” is updated with a map of the new record ID
				
					allAct = zoho.crm.getRelatedRecords("Activities", oldModule, recordId);
if (allAct.size() > 0)
{
for each a in allAct
	{
		idMap = Map();
		idMap.put("id",newRecordId);
		aMap = Map();
		aMap.put("$se_module",newModule);
		aMap.put("What_Id",idMap);
		updateAct = zoho.crm.updateRecord(a.get("Activity_Type"), a.get("id"), aMap);
		info updateAct;
	}
}
				
			
Past activities are stored in another module called “Activities History”.
				
					allPastAct = zoho.crm.getRelatedRecords("Activities_History", oldModule, recordId);
if (allPastAct.size() > 0)
{
	for each ap in allPastAct
	{
		idMap = Map();
		idMap.put("id",newRecordId);
		aMap = Map();
		aMap.put("$se_module",newModule);
		aMap.put("What_Id",idMap);
		updatePastAct = zoho.crm.updateRecord(ap.get("Activity_Type"), ap.get("id"), aMap);
		info updatePastAct;
	}
}
				
			

Attachments

Attachments can be migrated by first downloading them into the function, then uploading to the new record. For both download and upload functions, Zoho CRM API call is needed (that’s where your connection is needed)
				
					attachment = invokeurl
[
	url: "https://www.zohoapis.com/crm/v2/"+oldModule+"/"+recordId+"/Attachments"
	type: GET
	connection: "zohocrm" //Change this to your connection name
];
if (attachment.get("data").size() > 0)
{
	for each atm in attachment.get("data")
	{
		download = invokeurl
		[
			url: "https://www.zohoapis.com/crm/v2/"+oldModule+"/"+recordId+"/Attachments/"+atm.get("id")
			type: GET
			connection: "zohocrm" //Change this to your connection name
		];
		
		upload = zoho.crm.attachFile(newModule,newRecordId,download);
		info upload;
	}
}
				
			

Farewell Old Record

Once everything is fully migrated, it’s time to bid farewell to the old record with the delete record API call.
				
					delete = invokeurl
[
	url: "https://www.zohoapis.com/crm/v2/"+oldModule+"?ids="+recordId+"&wf_trigger=true"
	type: DELETE
	connection: "zohocrm" //Change this to your connection name
];
info delete;
				
			
Click here to copy these scripts. For more Zoho-wizardry, check out our GitHub page.

Convert Contacts back to Leads

The ability to convert Contacts back to Leads is sought after by many. Since Zoho does not have that functionality yet, we’re gonna build it using the scripts above. What happens during a Lead Conversion?
  • When a Lead is converted into a Contact, a few other things happen: An Account is created
  • A new Deal is created (optional) To undo a conversion, we need to reverse engineer the process.
In order to convert a Contact back to a Lead, here’s what we need the script to do:
  • Get all necessary fields and create the Lead record (make sure all mandatory fields are mapped).
  • Copy over all Related Notes
  • Reassign all related Activties (past and present)
    • “Who_Id” has to be set to “” to remove the connection with the Contact (this is necessary to prevent the activities from being deleted when the Contact is deleted later).
  • Copy over all Attachments (make sure that the attachments are moved to the Contact instead of Deals during the Zoho Lead conversion. If it was moved to the Deal, you will need to reconfigure the script).
  • Delete all Related Deals
  • Delete the Related Account
  • Delete the Contact Record
Create a Button for the Function For best user experience, create a custom button that says “Undo Convert” on the “View Page” which will execute the script.

  • Add a note in the button description to warn users that the related Account and Deals will be deleted.
  • Add a success message with the URL of the Lead at the end of the script to notify users that the “Undo Conversion” is complete.
				
					//Change below to the relevant old & new modules
oldModule = "Contacts";
newModule = "Leads";
oldRecord = zoho.crm.getRecordById(oldModule,recordId);

//Get all Necessary Fields and Add to a Map - add/modify as needed
rMap = Map();
rMap.put("First_Name",oldRecord.get("First_Name"));
rMap.put("Last_Name",oldRecord.get("Last_Name"));
rMap.put("Email",oldRecord.get("Email"));
rMap.put("Company",ifNull(oldRecord.get("Account_Name"),{"name":""}).get("name"));

//Create Record
newRecord = zoho.crm.createRecord(newModule,rMap);
newRecordId = newRecord.get("id");
info newRecordId;

//Copy Related Notes
allNotes = zoho.crm.getRelatedRecords("Notes",oldModule,recordId);
if(allNotes.size() > 0)
{
	for each  n in allNotes
	{
		notesMap = {"Parent_Id":newRecordId,"Note_Title":n.get("Note_Title"),"Note_Content":n.get("Note_Content"),"$se_module":newModule};
		create = zoho.crm.createRecord("Notes",notesMap);
		info create;
	}
}

//Get and Modify Related Activities
allAct = zoho.crm.getRelatedRecords("Activities",oldModule,recordId);
if(allAct.size() > 0)
{
	for each  a in allAct
	{
		idMap = Map();
		idMap.put("id",newRecordId);
		aMap = Map();
		aMap.put("$se_module",newModule);
		aMap.put("What_Id",idMap);
		aMap.put("Who_Id","");
		updateAct = zoho.crm.updateRecord(a.get("Activity_Type"),a.get("id"),aMap);
		info "UpdateACT : " + updateAct;
	}
}

//Get and Modify Related Closed Activities
allPastAct = zoho.crm.getRelatedRecords("Activities_History",oldModule,recordId);
if(allPastAct.size() > 0)
{
	for each  ap in allPastAct
	{
		idMap = Map();
		idMap.put("id",newRecordId);
		aMap = Map();
		aMap.put("$se_module",newModule);
		aMap.put("What_Id",idMap);
		aMap.put("Who_Id","");
		updatePastAct = zoho.crm.updateRecord(ap.get("Activity_Type"),ap.get("id"),aMap);
		info "UPDATE PAST ACT : " + updatePastAct;
	}
}

//Download & Upload Attachments
attachment = invokeurl
[
	url :"https://www.zohoapis.com/crm/v2/" + oldModule + "/" + recordId + "/Attachments"
	type :GET
	connection:"insert_your_crm_connection_here"
];
if(attachment.get("data").size() > 0)
{
	for each  atm in attachment.get("data")
	{
		download = invokeurl
		[
			url :"https://www.zohoapis.com/crm/v2/" + oldModule + "/" + recordId + "/Attachments/" + atm.get("id")
			type :GET
			connection:"insert_your_crm_connection_here"
		];
		upload = zoho.crm.attachFile(newModule,newRecordId,download);
		info upload;
	}
}

//Delete Related Deals
allDeals = zoho.crm.getRelatedRecords("Deals",oldModule,recordId);
if(allDeals.size() > 0)
{
	for each  d in allDeals
	{
		delete = invokeurl
		[
			url :"https://www.zohoapis.com/crm/v2/Deals?ids=" + d.get("id") + "&wf_trigger=true"
			type :DELETE
			connection:"insert_your_crm_connection_here"
		];
		info delete;
	}
}

//Deleted Related Account
accountid = ifNull(oldRecord.get("Account_Name"),{"id":"none"}).get("id");
if(accountid != "none")
{
	delete = invokeurl
	[
		url :"https://www.zohoapis.com/crm/v2/Accounts?ids=" + accountid + "&wf_trigger=true"
		type :DELETE
		connection:"insert_your_crm_connection_here"
	];
	info delete;
}

//Delete the Old Record
delete = invokeurl
[
	url :"https://www.zohoapis.com/crm/v2/" + oldModule + "?ids=" + recordId + "&wf_trigger=true"
	type :DELETE
	connection:"insert_your_crm_connection_here"
];
info delete;

//Construct success msg
newURL = "https://crm.zoho.com/crm/org707073965/tab/Leads/" + newRecordId;
return "This Contact has been converted back into a Lead here: " + newURL;
				
			

Contact Us!

Book a free 30-minutes consultation with a Zoho expert or send us an email

Related Resources