You need to sign in to do that
Don't have an account?
jeremy1.3936053840663044E12
Need help with Unexpected Token on a modified LeadRoundRobin trigger
Hi all-
Can anyone help me with this? I am trying to put this trigger in the Lead object to interact with the Lead Round Robin app from the App exchange.
Thank you!
Error: Compile Error: unexpected token: trigger leadRoundRobin on Lead at line 5 column 0
// LeadsRoundRobin Trigger
// Modifications to bulkify
// Some of this code has been borrowed from developer.force.com
// Last update by Rodney Jennings 2-11-2013
trigger leadRoundRobin on Lead (before insert, before update)
{
//Check if assignment owner has changed
//Trigger index --> Queue ID
Map<Integer,Id> queueIds = new Map<Integer,Id>();
Integer idx = 0;
for (Lead l : Trigger.new)
{
if(Trigger.isUpdate)
{
if(l.OwnerId <> Trigger.oldMap.get(l.id).OwnerId)
{
if (l.TempOwnerId__c == 'SKIP')
{
Trigger.new[idx].TempOwnerId__c = '';
}
else
{
queueIds.put(idx, l.OwnerId);
}
}
}
else
{
queueIds.put(idx, l.OwnerId);
}
idx++;
}
System.debug('>>>>>queueIds: '+queueIds);
if (queueIds.isEmpty()) return;
//Find active Assignment Group for Queue
//Trigger index --> Assignment_Group_Name ID
Map<Integer,Id> asgnGroupNameIds = new Map<Integer,Id>();
//Queue ID --> Assignment Group Queues
Map<Id,Assignment_Group_Queues__c> asgnGroupQueues = new Map<Id,Assignment_Group_Queues__c>();
for(Assignment_Group_Queues__c[] agq : [SELECT Assignment_Group_Name__c, QueueId__c
FROM Assignment_Group_Queues__c
WHERE QueueId__c in :queueIds.values()
AND Active__c = 'True'])
{
for (Integer i = 0; i < agq.size() ; i++)
{
asgnGroupQueues.put(agq[i].QueueId__c, agq[i]);
}
}
System.debug('>>>>>asgnGroupQueues: '+asgnGroupQueues);
if (asgnGroupQueues.isEmpty()) return;
for (Integer i : queueIds.keySet())
{
Assignment_Group_Queues__c agq = asgnGroupQueues.get(queueIds.get(i));
if (agq <> null)
{
asgnGroupNameIds.put(i, agq.Assignment_Group_Name__c);
}
//else no active assignment group queue error
}
System.debug('>>>>>asgnGroupNameIds: '+asgnGroupNameIds);
if (asgnGroupNameIds.isEmpty()) return;
//Determine next valid user in Queue/Assignment Group for round robin
//User with earliest last assignment date wins.
//asgnGroups maps an AG name to an arrary of AG members
Map<Id, Integer> assignedCountMap = new Map<Id, Integer>();
// Assignment Group Name ID --> User ID
Map<Id,Assignment_Groups__c[]> asgnGroups = new Map<Id,Assignment_Groups__c[]>();
for(Assignment_Groups__c[] ags : [SELECT Group_Name__c, User__c, Last_Assignment__c, Millisecond__c
FROM Assignment_Groups__c
WHERE Group_Name__c in :asgnGroupNameIds.values()
AND Active__c = 'True' AND User_Active__c = 'True'
ORDER BY Group_Name__c, Last_Assignment__c, Millisecond__c])
{
// First build map of AGNs with null user arrays
for (Integer i = 0; i < ags.size() ; i++)
{
if (!asgnGroups.containsKey(ags[i].Group_Name__c))
{
asgnGroups.put(ags[i].Group_Name__c, null);
//initialize assigned counts to zero
assignedCountMap.put(ags[i].Group_Name__c, 0);
}
}
// Loop over unique AG Names and add user arrays
for (Id gn : asgnGroups.keySet())
{
List<Assignment_Groups__c> users = new List<Assignment_Groups__c>();
for (Integer i = 0; i < ags.size() ; i++)
{
if (ags[i].Group_Name__c == gn)
{
users.add(ags[i]);
}
}
asgnGroups.put(gn, users);
}
}
System.debug('>>>>>asgnGroups: '+asgnGroups);
if (asgnGroups.isEmpty()) return;
// updateAssignmentGroups maps AG member Id to an AG member object
Map<Id,Assignment_Groups__c> updateAssignmentGroups = new Map<Id,Assignment_Groups__c>();
Map<Id, datetime> latestAGDateTime = new Map<Id,datetime>();
for (Integer i : queueIds.keySet())
{
// this can return null if the lead is not in a Queue that has an assignment group
Id assignedGroupNameId = asgnGroupNameIds.get(i);
if(assignedGroupNameId <> null)
{
// this has a size of zero if there are no users in the assigment queue
Assignment_Groups__c[] ags = asgnGroups.get(assignedGroupNameId);
if (ags.size()>0)
{
//Choose next user in line if user ID has already been used but not committed in this trigger batch
idx = assignedCountMap.get(assignedGroupNameId);
Assignment_Groups__c ag = ags[math.mod(idx, ags.size())];
//Assign User to Lead as the new owner
System.debug('>>>>>Owner changed for Lead ' + Trigger.new[i].Id + ' from '+Trigger.new[i].OwnerId+' to '+ ag.User__c);
Trigger.new[i].OwnerId = ag.User__c;
// don't assign back in an endless loop
Trigger.new[i].TempOwnerId__c = '';
//Set last assignment datetime
datetime now = datetime.now();
ag.Last_Assignment__c = now;
ag.Millisecond__c = now.millisecondGMT();
//update only latest Assignment Groups per ID
if (latestAGDateTime.containsKey(ag.id))
{
if(latestAGDateTime.get(ag.id) < now)
{
updateAssignmentGroups.put(ag.id, ag);
latestAGDateTime.put(ag.id, now);
}
}
else
{
updateAssignmentGroups.put(ag.id, ag);
latestAGDateTime.put(ag.id,now);
}
//increment counter so next lead in AG goes to next user in line
idx++;
assignedCountMap.put(assignedGroupNameId,idx);
}
}
}
//Map --> List/Array for DML update
List<Assignment_Groups__c> updateAG = new List<Assignment_Groups__c>();
for (Id agId : updateAssignmentGroups.keySet())
{
updateAG.add(updateAssignmentGroups.get(agId));
}
System.debug('>>>>>Update Assignment Groups: '+updateAG);
//Update last assignment for Assignment Group in batch
if (updateAG.size()>0)
{
try
{
update updateAG;
}
catch (Exception e)
{
for (Integer i : queueIds.keySet())
{
Trigger.new[i].addError('ERROR: Could not update Assignment Group records ' + ' DETAIL: '+e.getMessage());
}
}
}
}
Can anyone help me with this? I am trying to put this trigger in the Lead object to interact with the Lead Round Robin app from the App exchange.
Thank you!
Error: Compile Error: unexpected token: trigger leadRoundRobin on Lead at line 5 column 0
// LeadsRoundRobin Trigger
// Modifications to bulkify
// Some of this code has been borrowed from developer.force.com
// Last update by Rodney Jennings 2-11-2013
trigger leadRoundRobin on Lead (before insert, before update)
{
//Check if assignment owner has changed
//Trigger index --> Queue ID
Map<Integer,Id> queueIds = new Map<Integer,Id>();
Integer idx = 0;
for (Lead l : Trigger.new)
{
if(Trigger.isUpdate)
{
if(l.OwnerId <> Trigger.oldMap.get(l.id).OwnerId)
{
if (l.TempOwnerId__c == 'SKIP')
{
Trigger.new[idx].TempOwnerId__c = '';
}
else
{
queueIds.put(idx, l.OwnerId);
}
}
}
else
{
queueIds.put(idx, l.OwnerId);
}
idx++;
}
System.debug('>>>>>queueIds: '+queueIds);
if (queueIds.isEmpty()) return;
//Find active Assignment Group for Queue
//Trigger index --> Assignment_Group_Name ID
Map<Integer,Id> asgnGroupNameIds = new Map<Integer,Id>();
//Queue ID --> Assignment Group Queues
Map<Id,Assignment_Group_Queues__c> asgnGroupQueues = new Map<Id,Assignment_Group_Queues__c>();
for(Assignment_Group_Queues__c[] agq : [SELECT Assignment_Group_Name__c, QueueId__c
FROM Assignment_Group_Queues__c
WHERE QueueId__c in :queueIds.values()
AND Active__c = 'True'])
{
for (Integer i = 0; i < agq.size() ; i++)
{
asgnGroupQueues.put(agq[i].QueueId__c, agq[i]);
}
}
System.debug('>>>>>asgnGroupQueues: '+asgnGroupQueues);
if (asgnGroupQueues.isEmpty()) return;
for (Integer i : queueIds.keySet())
{
Assignment_Group_Queues__c agq = asgnGroupQueues.get(queueIds.get(i));
if (agq <> null)
{
asgnGroupNameIds.put(i, agq.Assignment_Group_Name__c);
}
//else no active assignment group queue error
}
System.debug('>>>>>asgnGroupNameIds: '+asgnGroupNameIds);
if (asgnGroupNameIds.isEmpty()) return;
//Determine next valid user in Queue/Assignment Group for round robin
//User with earliest last assignment date wins.
//asgnGroups maps an AG name to an arrary of AG members
Map<Id, Integer> assignedCountMap = new Map<Id, Integer>();
// Assignment Group Name ID --> User ID
Map<Id,Assignment_Groups__c[]> asgnGroups = new Map<Id,Assignment_Groups__c[]>();
for(Assignment_Groups__c[] ags : [SELECT Group_Name__c, User__c, Last_Assignment__c, Millisecond__c
FROM Assignment_Groups__c
WHERE Group_Name__c in :asgnGroupNameIds.values()
AND Active__c = 'True' AND User_Active__c = 'True'
ORDER BY Group_Name__c, Last_Assignment__c, Millisecond__c])
{
// First build map of AGNs with null user arrays
for (Integer i = 0; i < ags.size() ; i++)
{
if (!asgnGroups.containsKey(ags[i].Group_Name__c))
{
asgnGroups.put(ags[i].Group_Name__c, null);
//initialize assigned counts to zero
assignedCountMap.put(ags[i].Group_Name__c, 0);
}
}
// Loop over unique AG Names and add user arrays
for (Id gn : asgnGroups.keySet())
{
List<Assignment_Groups__c> users = new List<Assignment_Groups__c>();
for (Integer i = 0; i < ags.size() ; i++)
{
if (ags[i].Group_Name__c == gn)
{
users.add(ags[i]);
}
}
asgnGroups.put(gn, users);
}
}
System.debug('>>>>>asgnGroups: '+asgnGroups);
if (asgnGroups.isEmpty()) return;
// updateAssignmentGroups maps AG member Id to an AG member object
Map<Id,Assignment_Groups__c> updateAssignmentGroups = new Map<Id,Assignment_Groups__c>();
Map<Id, datetime> latestAGDateTime = new Map<Id,datetime>();
for (Integer i : queueIds.keySet())
{
// this can return null if the lead is not in a Queue that has an assignment group
Id assignedGroupNameId = asgnGroupNameIds.get(i);
if(assignedGroupNameId <> null)
{
// this has a size of zero if there are no users in the assigment queue
Assignment_Groups__c[] ags = asgnGroups.get(assignedGroupNameId);
if (ags.size()>0)
{
//Choose next user in line if user ID has already been used but not committed in this trigger batch
idx = assignedCountMap.get(assignedGroupNameId);
Assignment_Groups__c ag = ags[math.mod(idx, ags.size())];
//Assign User to Lead as the new owner
System.debug('>>>>>Owner changed for Lead ' + Trigger.new[i].Id + ' from '+Trigger.new[i].OwnerId+' to '+ ag.User__c);
Trigger.new[i].OwnerId = ag.User__c;
// don't assign back in an endless loop
Trigger.new[i].TempOwnerId__c = '';
//Set last assignment datetime
datetime now = datetime.now();
ag.Last_Assignment__c = now;
ag.Millisecond__c = now.millisecondGMT();
//update only latest Assignment Groups per ID
if (latestAGDateTime.containsKey(ag.id))
{
if(latestAGDateTime.get(ag.id) < now)
{
updateAssignmentGroups.put(ag.id, ag);
latestAGDateTime.put(ag.id, now);
}
}
else
{
updateAssignmentGroups.put(ag.id, ag);
latestAGDateTime.put(ag.id,now);
}
//increment counter so next lead in AG goes to next user in line
idx++;
assignedCountMap.put(assignedGroupNameId,idx);
}
}
}
//Map --> List/Array for DML update
List<Assignment_Groups__c> updateAG = new List<Assignment_Groups__c>();
for (Id agId : updateAssignmentGroups.keySet())
{
updateAG.add(updateAssignmentGroups.get(agId));
}
System.debug('>>>>>Update Assignment Groups: '+updateAG);
//Update last assignment for Assignment Group in batch
if (updateAG.size()>0)
{
try
{
update updateAG;
}
catch (Exception e)
{
for (Integer i : queueIds.keySet())
{
Trigger.new[i].addError('ERROR: Could not update Assignment Group records ' + ' DETAIL: '+e.getMessage());
}
}
}
}
Do you mean for all instances of <>?
I'd comment out all the code and then start uncommenting section by section to see what is causing the problem.