+ Start a Discussion
Steven Dang 9Steven Dang 9 

need round robin web2lead trigger guidance

Hi, 
I am currently having issue with my code, not sure how to finish it. 

I want to assign round robin opportunity to a lead object only if its status is working and the user is round robin available. I want to assign this to every new lead. If there are no available users put the lead in Queue, and when a user is available assign it to one with the least amount of leads. I have attached my code below. The comments is where i am stuck. Any help is appreciated, thank you. 
trigger WebToLeadRoundRobinTrigger on Lead (after insert, after update, before insert, before update) 
{	
	List<Lead> newLeadLst = new List<Lead>();
	
	if(Trigger.isBefore && Trigger.isInsert)
	{
		for (Lead newLead : Trigger.new)
		{
			if (newLead.Status.equalsIgnoreCase('New') && newLead.LeadSource.equalsIgnoreCase('Website'))
			{
				newLeadLst.add(newLead);
			}
				
            //Grabs the user where it is round robin avaialble 
			List<User> lsAvailableUsers = [SELECT Id, Name, Robin_Counter__c, Is_Available_Round_Robin__c
										FROM User 
										WHERE Is_Available_Round_Robin__c = true
										ORDER BY Robin_Counter__c ASC
			];

			if(lsAvailableUsers != null && lsAvailableUsers.size() > 0)
			{
				Integer currentMaxRobinCounter = lsAvailableUsers[lsAvailableUsers.size()-1].Robin_Counter__c.intValue(); 
                Integer nextUser = 0;
		          	  
				if(currentMaxRobinCounter == null) 
				{     
					currentMaxRobinCounter = 0; 
				}
		              
				for(Lead newLeadFromWeb : newLeadLst)
				{
					newLeadFromWeb.ownerid = lsAvailableUsers[nextUser].id;
					nextUser = math.mod((nextUser + 1), lsAvailableUsers.size());
					lsAvailableUsers[nextUser].Robin_Counter__c = currentMaxRobinCounter + nextUser + 1;
					update lsAvailableUsers;  
				}			
			} 
			else
			{   
            
            /* how can i assign leads to user with the least amount of leads? I want to query a list of users where Is_Available_Round_Robin__c = true and map it   
                to a list of leads with status = working. I should have ownerid as 1st column of the query and then the number of leads the owner currently have
                for the 2nd column. Need to somehow convert this soql to a map */
       
				//if the list size = 0 assign it to ownerID = Queue
				
				Group listEmptyFreeUserInQueue = [SELECT Id FROM Group WHERE Type = 'Queue' AND Name = 'Web to Lead' LIMIT 1];

				for(Lead newLeadFromWeb : newLeadLst)
				{
					newLeadFromWeb.ownerid = listEmptyFreeUserInQueue.Id;
				}			
			}
		}
	}
}

Also, if I were to add this in, how would i approach this? If a lead have been left in a status ='assigned' for more than 2 hours, assign the lead to another user that is Is_Available_Round_Robin__c.

 
Prem RPrem R
Hi Steven,  

Your Question: Also, if I were to add this in, how would i approach this? If a lead have been left in a status ='assigned' for more than 2 hours, assign the lead to another user that is Is_Available_Round_Robin__c.

Answer:  You have to write a batch apex and query with the same logic which you have written in trigger(Some minor changes are required on this) and schedule a batch for 2 hours once.  

Thanks,
Prem
Steven Dang 9Steven Dang 9
thank you. what about the comment on line 42? that is mostly where i am stuck