I am trying to add a Duplicate TAG to the lead on Email or phone search, it is working well but it removes the existing Tags and just updates the DUPLICATE tag. Please help.
leadRecord = zoho.crm.getRecordById("Leads",leadId);
email = leadRecord.get("Email");
phone = leadRecord.get("Phone");
info phone;
info email;
// Construct search criteria to find leads with the same email or phone
search_criteria = "(Email:equals:" + email + ") or (Phone:equals:" + phone + ")";
relcont = zoho.crm.searchRecords("Leads",search_criteria);
relcontsize = relcont.size();
// Log the results of the search
info relcont;
info search_criteria;
// Check if any leads with the same email or phone exist
if(relcontsize > 0)
{
info "Duplicate leads found with the same email or phone.";
// Retrieve existing tags from the lead
existingTags = leadRecord.get("Tags");
newTagsList = List();
// Add existing tags to the new tags list
if(existingTags != null)
{
for each tag in existingTags
{
newTagsList.add(tag);
}
}
// Create the new tag for "DUPLICATE"
tag_duplicate = Map();
tag_duplicate.put("name","DUPLICATE");
// Add the new tag to the tags list
newTagsList.add(tag_duplicate);
// Update the lead record with the complete tags list
NewTag = {"Tag":newTagsList};
response = zoho.crm.updateRecord("Leads",leadId,NewTag);
// Log the response
info response;
// Check if the tag was added successfully
if(response.get("code") == "200")
{
info "Tag 'DUPLICATE' added successfully.";
}
else
{
info "Failed to add tag: " + response.get("message");
}
}
else
{
info "No duplicate leads found with the same email or phone.";
}