I have created a round robin assignment of leads to users. I am getting the following error
Lead_Trigger: execution of AfterUpdate caused by: System.TypeException: Invalid integer: (Lead-0002) Class.leadRoundRobinAssignment.assignTicketsRoundRobin: line 35, column 1 Trigger.Lead_Trigger: line 2, column 1
Below is my code. Where is my error?
Apex Class
public class leadRoundRobinAssignment{
public static Boolean runOnce = false;
public static Boolean runMerge = false;
public static void assignTicketsRoundRobin(Set<Id> ticketIdsSet){
/* get list of all the tickets */
List<Lead> ticketList = [Select Id, OwnerId, Lead__c FROM Lead Where Id IN:ticketIdsSet];
Integer index;
Integer ticketNumber;
Integer agentSize;
List<User> agentList = new List<User>();
Set<Id> queueIdsSet = new Set<Id>();
System.debug('#### ticketList = '+ticketList);
// Fetch Ids of the group.
For(Lead c : ticketList){
If(String.valueOf(c.ownerId).startsWith('00G')){
queueIdsSet.add(c.ownerId);
}
}
// return if Lead is already assigned to user
If(queueIdsSet==null || queueIdsSet.size()==0)return;
System.debug('#### queueIdsSet = '+queueIdsSet);
Set<Id> userIdsSet = new Set<Id>();
// Fetch Ids of the users
For(GroupMember gm : [Select Id, UserOrGroupId FROM GROUPMEMBER WHERE GroupId IN : queueIdsSet]){
userIdsSet.add(gm.UserOrGroupId);
}
System.debug('#### userIdsSet = '+userIdsSet);
/* fetch the total no of users for RRD that are active */
agentList = [Select Id, Name, Profile.Name From User Where Id In : userIdsSet AND ISACTIVE = true];
// return if there are no active users
If(agentList==null || agentList.size()==0)return;
System.debug('#### agentList = '+agentList);
For(Lead c : ticketList){
if(c.Lead__c!=null){
ticketNumber = Integer.valueOf(c.Lead__c);
System.debug('#### ticketNumber = '+ticketNumber);
agentSize = agentList.size();
index = Math.MOD(ticketNumber ,agentSize);//+1;
System.debug('#### index = '+index);
c.OwnerId = agentList[index].id;
}
}
If(ticketList!=null && ticketList.size()>0){
System.debug('#### Updating tickets = '+ticketList);
update ticketList;
}
}
}
Apex Trigger
trigger Lead_Trigger on Lead (After Update) {
leadRoundRobinAssignment.assignTicketsRoundRobin(Trigger.NewMap.keyset());
}
Best Answer
The problem appears to be here:
The field
Lead__c
apparently is an Autonumber field whose pattern looks likeLead-{0000}
, yielding the value "Lead-0002". This is not a value that can be converted to an integer. You'll need to change the field definition to use the pattern{0000}
, to get a plain number that you can convert to an integer and use in your modulo operation.Important notes about functionality: