In this post, I’m going to show you how to copy data from a lookup field in one module to another module’s lookup field. In this particular module, there’s a lookup field called Rep Firm.
Originally, it wasn’t set up like that. It was set up with a dropdown list, with hard-coded values. We realized that wasn’t really going to work because it meant that we needed to keep changing the dropdown list in both the deals module and in the sales orders module to keep them in sync. A lookup field gets rid of that issue because it will always have the same options in both modules.
I’d already written some code to copy data from the deals record when a new sales order is created. Now I want to modify that code to also copy over the lookup field as well.
Walking you through the code, the first step is we’re going to get the data for the deal, and then within the deal record, we’ve got the linked account.
We’re going to use “zoho.crm.getRecordByID” to get the account record.
The same thing goes for the sales order record. What we’re doing then is we are copying all of the data from the deal and we’re putting it into a map, which we will then use to update the sales order.
In order to update the Sales Order, we need to check the API names. We’ll discover the relevant fields in the Deals module to draw data from and then figure out the API name of the corresponding fields in the Sales Orders module.
We’ll go into the deals and we will find that Rep Firm has the name “Rep_Firm” as the API name.
Sales orders also have a Rep Firm lookup, which is “Rep_Firm1.”
To copy over the lookup data from the Deals’ module, here’s what our code will look like:
update_payload = map(); rep_firm_id = deal_record.get("Rep_Firm").get("id"); update_payload.put("Rep_Firm1", rep_firm_id); update_resp = zoho.crm.updateRecord("Sales_Orders", sales_order_id, update_payload); info update_resp;
The key thing to note here is that if we want to fill out a lookup field, we need to supply the ID of the linked record. Rep Firm is a lookup pointing to the Accounts module. In effect, what we are doing is setting the ID of an accounts record in Rep_Firm1. If you run this code, the lookup will be populated with the correct value.
Hope this helps you to be able to automatically fill out your lookup fields.