1

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());
}
2

The problem appears to be here:

ticketNumber = Integer.valueOf(c.Lead__c);

The field Lead__c apparently is an Autonumber field whose pattern looks like Lead-{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:

  • Your recursion guard appears to be a simple static Boolean, which is not a good idea - your trigger will mysteriously fail to work with >200 records per transaction, multiple DML operations under 200 records, and a few other circumstances. Search SFSE for "recursion guard" for some great discussions of why and better patterns to use.
  • Your code as written will distribute the Leads across all of the Users who are part of any Queue that owns any of the Leads. That is, it doesn't distribute Leads owned by a Queue to the Users in that specific Queue. This seems unlikely to be your intent but I don't know the business logic here.
  • Hi David, the business logic is to assign lead 1 to user 1, lead 2 to user 2, lead 3 to user 3, lead 4 to user 1, lead 5 to user 2. You get the idea of what I am trying to accomplish. I will look into what you mentioned above and adjust my syntax to not fail at >200 records – Alex van der Merwe Aug 6 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.