// Retrieve the Deal record
dealRecord = zoho.crm.getRecordById("Deals", input.deal_id);
// Get the values of the "Referred_By" field from the Deal record
referredByValues = dealRecord.get("Referred_By");
info referredByValues;
// Check if the "Referred_By" field is not empty
if (referredByValues != null && referredByValues.size() > 0) {
// Create an empty array to store the contact IDs
contactIds = List();
info contactIds;
// Iterate through the values of the "Referred_By" field
for each referredByValue in referredByValues {
// Get the ID value from each "Referred_By" field value
info referredByValue ;
referredById = referredByValue.get("id").toLong();
// Check if the ID value is not null
if (referredById != null) {
// Add the contact ID to the array
contactIds.add(referredById);
} else {
// Output an error message if the ID value is null
info "Invalid 'Referred_By' value found.";
}
}
// Construct the search criteria string for retrieving Contact records
searchCriteria = "id:(";
for each contactId in contactIds {
searchCriteria = searchCriteria + contactId + ",";
}
searchCriteria = searchCriteria.substring(0, searchCriteria.length() - 1) + ")";
info searchCriteria;
// Retrieve the Contact records associated with the contact IDs
contactRecords = zoho.crm.searchRecords("Contacts", searchCriteria);
info contactRecords;
// Iterate through the contact records
for each contactRecord in contactRecords {
// Get the value of the "Commission_Referral" field from the Contact record
commissionReferral = contactRecord.get("Commission_Referral");
// Update the Deal record's "MTG_Referral_Fee" field with the value from "Commission_Referral"
zoho.crm.updateRecord("Deals", input.deal_id, {"MTG_Referral_Fee": commissionReferral});
// Output a success message
info "The value from 'Commission_Referral' has been copied to 'MTG_Referral_Fee' successfully.";
}
// Output a message if no matching Contact records are found
if (contactRecords == null || contactRecords.size() == 0) {
info "No Contact records found with the provided 'Referred_By' values.";
}
} else {
// Output a message if the "Referred_By" field is empty
info "The 'Referred_By' field in the Deal record is empty.";
}