Skip to content

Instantly share code, notes, and snippets.

@jeznag
Created December 5, 2015 01:15
Show Gist options
  • Select an option

  • Save jeznag/5889dbddac14e73db878 to your computer and use it in GitHub Desktop.

Select an option

Save jeznag/5889dbddac14e73db878 to your computer and use it in GitHub Desktop.
NAME_OF_ROLE_TO_ASSIGN_LEADS_TO = "SOMEROLE";
//you'll need to create this record and assign it to someone who should receive leads
//before you can use this function
ID_FOR_REFERENCE_LEAD = 1176607000005707001;
AUTH_TOKEN = "MYAUTHTOKEN"
//get a list of all users who can be assigned leads
userResp = getUrl(("https://crm.zoho.com/crm/private/xml/Users/getUsers?authtoken=" + AUTH_TOKEN + "&scope=crmapi&type=ActiveUsers"));
leadidStr = input.leadid.toString();
listOfLeadOwners = List:Int();
users = userResp.executeXPath("/users/user");
if((users != null) && (users != ""))
{
userList=users.toList("-|-");
for each user in userList
{
roleNode = user.executeXPath("/user/@role");
role = roleNode.executeXPath("/role/text()");
if(role == NAME_OF_ROLE_TO_ASSIGN_LEADS_TO)
{
idNode = user.executeXPath("/user/@id");
id = idNode.executeXPath("/id/text()");
idInt=id.toLong();
listOfLeadOwners.add(idInt);
}
}
listOfLeadOwners = listOfLeadOwners.sort(true);
leadOwnerListSize = listOfLeadOwners.size();
//refRecord is where we store the ID of the lead owner who was last assigned a lead
refRecord = zoho.crm.getRecordById("Leads", ID_FOR_REFERENCE_LEAD);
refOwner = refRecord.get("SMOWNERID");
refOwnerInt = refOwner.toLong();
indexForLastTimeRefOwnerWasAssigned = listOfLeadOwners.lastIndexOf(refOwnerInt);
indexForOwnerWhoShouldGetThisLead = (indexForLastTimeRefOwnerWasAssigned + 1);
if(indexForOwnerWhoShouldGetThisLead == leadOwnerListSize)
{
//we've hit the end of the owner list - will cause an exception if
//we try to get this one so let's go back to the start of the list
newowner = listOfLeadOwners.get(0);
}
else
{
//reference owner is not at the end of the list so we can use this one
newowner = listOfLeadOwners.get(indexForOwnerWhoShouldGetThisLead);
}
refMap = map();
refMap.put("SMOWNERID", newowner);
//update the reference lead
updateRef = zoho.crm.updateRecord("Leads", ID_FOR_REFERENCE_LEAD.toString(),refMap);
//update the current lead
updateOwner = zoho.crm.updateRecord("Leads",leadidStr,refMap);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment