function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JLockardJLockard 

Lead Trigger To Interrogate Lead Owner Status

Hello,

I've seen many related posts, but nothing that has pushed me over the top for this problem.  Basically, I need a Lead trigger that can cause a Lead record to be run through the Lead Assignment rules if certain criteria are met on update of the Lead record.

 

One of the criteria is whether the current Lead Owner is an active or inactive SFDC user.  I had written the code to capture the owner status field via a SOQL select statement, but unfortunately this logic causes the apparently common mistake of having too many SOQL queries because the select statement was inside the For loop in the trigger code.

 

Based on the other posts for that error, I know that I'll need to use Sets and Maps to get it to work, but has anyone written a similar trigger before?

 

Basically, I need to capture the value of the isActive flag on the user record that corresponds to the Lead owner for each Lead being evaluated by the trigger.

 

Any help is appreciated.

 

Best Answer chosen by Admin (Salesforce Developers) 
JLockardJLockard

Thanks for your help guys. 

 

I ended up getting this working with the code posted below.  One thing that tripped me up was needing to avoid the attempt to get the user status if the Lead Owner was actually a Lead Queue.

 

	// Grab a list of the Queue Names and Ids for reference
	List<QueueSobject> qlist = new List<QueueSobject>();
	qlist = [Select q.Queue.Name, q.QueueId from QueueSobject q];
	numQueues = qlist.size();
	
	// Put the Queue ID's in a set to avoid adding them to the Lead Owner Status Map
	Set<ID> queueIds = new Set< Id >();
	for (i=0; i<numQueues; i++) {
		queueIds.add(qlist[i].QueueId);
	}

   	// Loop over each Lead to capture the Lead id for each in a Set, skipping ids for Queues
   	Set<ID> LeadOwnerIds = new Set< Id >();
   	for (Lead l : trigger.new) {
   		// For each Lead, if the owner is not a queue, then put the owner ID in the LeadOwnerSet for later
   		if (!queueIds.contains(l.OwnerId)){
   			LeadOwnerIds.add(l.OwnerId);
   		}
   	}
   	
   	// Create a Map between the Lead Owner ID and the owner status
   	Map <Id, User> ownerStatusMap = new Map <Id, User>([SELECT Id, IsActive FROM User WHERE Id in: LeadOwnerIds]);
   	numLeadOwners = ownerStatusMap.size();
   	
	// Loop each Lead in the Trigger array and check for the conditions that would cause
	// us to want to run the Lead Assignment Rules for each.
    For (lead l:trigger.new){
    	
    	// Get the status of the Lead Owner from the Map
    	Boolean bOwnerActive = True;
    	
    	// If the current Lead owner is a Queue, don't try to get the User Status for it
   		if (!queueIds.contains(l.OwnerId)){
	   		bOwnerActive = ownerStatusMap.get(ownerID).IsActive;
   		}

		// Now I have the Owner Active information I need to decide whether to add this to the list
		// for pushing through the assignment manager....
    }

 

All Answers

Venkat PolisettVenkat Polisett

 

trigger activeLeadOwners on Lead (after update, after insert)
{
     // an owner may have more than one lead
     Map<Lead, Id> leadsByOwner = new Map<Lead, Id>();

     for (Lead l : Trigger.new)
     {
         leadsByOwner.put(l, l.Id);
     }

     Map<Id, User> userMap = new Map<Id, User>([select id, isActive from User where id in :leadsByOwner.values()]);

     for (Lead l : leadsByOwner.keySet())
     {
          User leadOwnerUser = userMap.get(l.OwnerId);

          // now you kwow leadOwnerUser is active or not
          if (!leadOwnerUser.isActive)
          {
              // do some thing with the inactive user
          }
    }
}

 

The above code is not tested. I guess you get the idea.

geetageeta

you can use the SOQL below to get the lead owner's Active flag for each lead being processed by the Trigger:

List<Lead> leadls = [select Id, Owner.IsActive from Lead where Id in: Trigger.newMap.keySet()];

JLockardJLockard

Thanks for your help guys. 

 

I ended up getting this working with the code posted below.  One thing that tripped me up was needing to avoid the attempt to get the user status if the Lead Owner was actually a Lead Queue.

 

	// Grab a list of the Queue Names and Ids for reference
	List<QueueSobject> qlist = new List<QueueSobject>();
	qlist = [Select q.Queue.Name, q.QueueId from QueueSobject q];
	numQueues = qlist.size();
	
	// Put the Queue ID's in a set to avoid adding them to the Lead Owner Status Map
	Set<ID> queueIds = new Set< Id >();
	for (i=0; i<numQueues; i++) {
		queueIds.add(qlist[i].QueueId);
	}

   	// Loop over each Lead to capture the Lead id for each in a Set, skipping ids for Queues
   	Set<ID> LeadOwnerIds = new Set< Id >();
   	for (Lead l : trigger.new) {
   		// For each Lead, if the owner is not a queue, then put the owner ID in the LeadOwnerSet for later
   		if (!queueIds.contains(l.OwnerId)){
   			LeadOwnerIds.add(l.OwnerId);
   		}
   	}
   	
   	// Create a Map between the Lead Owner ID and the owner status
   	Map <Id, User> ownerStatusMap = new Map <Id, User>([SELECT Id, IsActive FROM User WHERE Id in: LeadOwnerIds]);
   	numLeadOwners = ownerStatusMap.size();
   	
	// Loop each Lead in the Trigger array and check for the conditions that would cause
	// us to want to run the Lead Assignment Rules for each.
    For (lead l:trigger.new){
    	
    	// Get the status of the Lead Owner from the Map
    	Boolean bOwnerActive = True;
    	
    	// If the current Lead owner is a Queue, don't try to get the User Status for it
   		if (!queueIds.contains(l.OwnerId)){
	   		bOwnerActive = ownerStatusMap.get(ownerID).IsActive;
   		}

		// Now I have the Owner Active information I need to decide whether to add this to the list
		// for pushing through the assignment manager....
    }

 

This was selected as the best answer