+ Start a Discussion
Ross JamesRoss James 

If an Owner is a User or a Queue - a Lead Trigger

I have a simple trigger on Leads that needs to know if the Owner is a User or a Queue.

 

Can anyone show me the (proably very) simple solution to this? My code is below, with the 'real world' question if Owner = Queue.

 

 

 Set<id> ownerIds = new Set<id>();
    for (Lead l : Trigger.new)
        ownerIds.add(l.OwnerId);
        
    Map<id, User> owners = new Map<id, User>([Select Id from User Where Id in :ownerIds]);      
    
    for (Lead l : Trigger.new)
        if(l.Owner = queue){
        //l.Owner_Usable__c = null;
        } else {
        //l.Owner_Usable__c  = owners.get(l.OwnerId).Id;
        }

 

 

 

homer671homer671

try adding  an extra = sign for comparison.   One = is for assignment. 

 

 if(l.Owner == queue){
Ross JamesRoss James

if Owner = Queue is just the question I am asking - I have no idea how to approach asking the IF Statement about Users and Queues.

AhmedPotAhmedPot

Hey you can do query on group object.

 

Select g.Type, g.OwnerId, g.Name,g.id From Group g where type= 'queue'

jaslozjasloz

I'm having the same problem.  One thing we can check is the prefix of the OwnerId.  If this is 00G then it is a queue; if it is 005 it is a user.

 

I am mid development of the following trigger to create a task assigned to the new Owner (only where the owner is not a queue) when the enquiry_details__c <> ''.

 

 

trigger Create_taskforChangeofOwner on Lead (before insert, before update) {

List<Task> tasksToCreate = new List<Task>();

for (Lead ld : Trigger.new)
    {
        string txtLdID = ld.ownerId;
            
        IF (txtLdID.startsWith('OOG') == True && ld.Enquiry_Details__c != '')
        {
            Task newTask = new Task(
            
                OwnerId = ld.OwnerId,
                WhoId = ld.Id,
                Subject = 'Web Enquiry',
                Description = 'Enquiry Details: ' + ld.Enquiry_Details__c,
                ActivityDate = system.today(),
                priority = 'High'
            );
            tasksToCreate.add(newTask);
        }
    }
insert tasksToCreate;
}

 

I think this will work but it requires testing.

 

jaslozjasloz

Sorry I'll let you spot the deliberate mistake of typing OOG (letter Oh) not 00G (number zero).

 

:robotvery-happy:

jaslozjasloz

OK 3rd post in a couple of hours, I did say I was mid development.  I think I am finished.

 

Note: I am not a coder! I beg, borrow and steal code from wherever I can to get a job done.  Don't blame me if it's wrong.  Any suggestions from real coders would be most appreciated.

 

 

trigger Create_taskforChangeofOwner on Lead (before insert, before update) {

//List for new Tasks to be created
List<Task> tasksToCreate = new List<Task>();
string txtLdID;

//Loop all leads in the Trigger
for (Lead ld : Trigger.new) {
	txtLdID = ld.ownerId;
	
	//Check to see if Owner is a User (not a Queue)
	if (txtLdID.startsWith('005') == TRUE) 
	{
		//Set OwnerisQueue field to No
		ld.OwnerisQueue__c = false;
		
		//Declare txtldID as the old Owner
		txtldID =  Trigger.oldMap.get(ld.Id).ownerID;
		//Check to see if the old Owner was a Queue and if so create a Task
		//This still is not optimal as a task may of been created to a previous User
		if (ld.Enquiry_Details__c != '' && txtLdID.startsWith('00G') == TRUE)
		{
			//New task details
			Task newTask = new Task( 
			
			OwnerId = ld.OwnerId,
			WhoId = ld.Id,
			Subject = 'Web Enquiry',
			Description = 'Enquiry Details: ' + ld.Enquiry_Details__c,
			ActivityDate = system.today(),
			priority = 'High'
			);
		
		//add new tasks to list
		tasksToCreate.add(newTask);
		}
	} else {
		//Owner is not a User so must be a queue, update field
		ld.OwnerisQueue__c = true;
	}
}
//Create tasks
insert tasksToCreate;
}

 

 

jaslozjasloz

I wish you could edit posts :(

 

the line

 

 

if (ld.Enquiry_Details__c != '' && txtLdID.startsWith('00G') == TRUE)

 

 

should be

 

 

if (ld.Enquiry_Details__c != NULL  && txtLdID.startsWith('00G') == TRUE)