Your business may run different lead generation campaigns, such as webinars, trade shows, emails, and forms. By knowing which campaigns generate the most leads, we can allocate resources towards those campaigns and maximize our return on investment. This solution explains how to link a lead record to a campaign by utilizing a custom field.
Tracking the source of a lead is often done by using tags or UTM parameters for online or web sources. For offline campaigns, the campaign name can be manually entered or auto-updated through a workflow rule. To begin with, add a custom field to the leads module and update it with the lead's campaign name using one of the above methods.
Using a custom function, you can link a lead record to its related campaign based on its custom field value. For example, if a new lead is generated from a campaign named "Newsletter" and has the keyword newsletter in its custom field, it will be linked with that campaign in the Campaigns module.
Permissions and availability
- Users with the Modules Customization permission can add custom fields.
- Users with the Manage Automation permission can create and update workflow rules.
- Users with the Manage Extensibility permission can create custom functions.
- Users with the Manage Sandbox permission can manage the sandbox.
Requirements
- Add a Single Line field in the Leads module. This field will hold the name of the campaign that generated the lead. Learn more
- Create a workflow rule for the Leads module that triggers every time a lead is created or edited. Learn more
- Write a custom Deluge function and link it up with the workflow rule. Learn more
- Test the workflow rule in a sandbox before deploying it to your production environment. Learn more
Add a custom field
The first step is adding the custom single line field to the Leads module.
- Navigate to Setup > Customization > Modules and Fields.
- Click the Leads module to open the layout editor.
- Drag and drop the Single Line field from the New Fields tray to the desired section of the layout.
- Name the single line field (e.g. "Campaign Name") and define its properties as required.
- Once you have finished, click Save and Close.
Create a workflow rule
Follow the steps below to create a workflow rule in your Zoho CRM account:
- Navigate to Setup > Workflow Rules > +Create Rule.
- Select Leads from the module dropdown list.
- Provide a name for the rule. For example: "Link Leads with Related Campaigns".
- Add a description (optional) and then click Next.
- In the Execute this workflow rule based on section, choose Record Action, then select Create or Edit from the dropdown and click Next.
- In the Which leads would you like to apply the rule to? section, specify the condition as "<Campaign Name> <is not empty>" and click Next.
- Under Instant Actions, select Function and Write your own.
- Provide a name and description, if necessary, for the function.
- On the Deluge Script Editor, do the following:
- Copy and paste the code given below.
- Click Edit Arguments.
- Choose Leads - Lead Id, name it LeadId, then click Save.
- Click Save & Execute Script.
- Click Save on the workflow rule page.
The code
leadDetails = zoho.crm.getRecordById("Leads",leadId);
campgName = ifnull(leadDetails.get("Campaign_Name"),"");
searchCampg = zoho.crm.searchRecords("Campaigns","(Campaign_Name:equals:" + campgName + ")");
if(searchCampg.size() > 0)
{
campgId = searchCampg.get(0).get("id");
mp = Map();
mp.put("CAMPAIGNID",campgId.toLong());
linkLeadtoCampg = zoho.crm.updateRelatedRecord("Campaigns",campgId.toLong(),"Leads",leadId.toLong(),mp);
info linkLeadtoCampg;
}
info searchCampg;
info campgId;
Notes
- Make sure to use the accurate API names for their corresponding fields (e.g., "Campaign Name") in the code snippet. Learn more
- The code above is an example of how to automatically link a lead record to the campaign it originated from in your CRM. You can use this code for any other module, such as a custom module, by modifying the module name and parameters.
Tip
- Configure and test the function in a sandbox to ensure that further development doesn't disrupt your production environment.
Test the solution
- Navigate to Leads > +Create Lead.
- On the Lead Details page, enter the first and last name, company, and campaign name.
Note: Enter the campaign name that already exists in the Campaigns module. - Click Save.
The workflow rule will be triggered. - Check whether the lead has been added to the specified campaign in the Campaigns module. You can also modify an existing lead record to confirm the solution.
Did you find this useful? Try it out and let us know how it works. Share this with your team if they'd benefit from it! If you have questions, please don't hesitate to contact us.
More info
- ModuleLeads
- Trigger PointWorkflow Rule
- EditionEnterprise and above
- ComplexityMedium
- Implementation Time30 minutes

Looking for a custom solution?
Contact us, we will help enhance your productivity at lightning speed.
SUBMIT REQUEST