+ Start a Discussion
ckellieckellie 

How to detect user from queue ownership?

In this trigger than I am working on I am needing to seperate leads that are members of a group from  leads that are owned by individual users.

 

Here is my Error

System.QueryException: List has no rows for assignment

 Erroneous Code

lUser = [select id from user where id =: lead[0].ownerid].id;

 Complete Code:

 

trigger cmOwner on CampaignMember (Before Insert, Before Update) {

    Set<Id> lIds = new Set<Id>();
    Set<Id> cIds = new Set<Id>();
    
    for(CampaignMember cm : Trigger.new) {
    System.debug('**** 1 lIds id : '+cm.leadid);
    System.debug('**** 1.5 cIds id : '+cm.contactid);

        LIds.add(cm.leadid);
        cids.add(cm.contactid);

    System.debug('**** 2 lIds id : '+cm.leadid);
    System.debug('**** 2.5 cIds id : '+cm.contactid);

   }
   
   String sUser;
   String lUser; 
   List<lead> lead = [select ownerid from Lead where id in: Lids];
   sUser = [select id from user where Name = 'Shayla Wentz'].id;
   lUser = [select id from user where id =: lead[0].ownerid].id;
   List<contact> contact = [select ownerid from contact where id in:cIds];


    for(CampaignMember c: trigger.new){
      If(c.Lead.ownerid == lUser){   
        
        c.OwnerName__c = lead[0].ownerid;
        } else{
        
        c.OwnerName__c = sUser;
        }
        
     if(c.contactid <> null){
        c.OwnerName__c = contact[0].ownerid;
        }
    }  
}

 

How do I determine if the Lead ownerid is a user.id rather than a queue id?

 

Thank you

Best Answer chosen by Admin (Salesforce Developers) 
WizradWizrad
cm.lead.ownerid.substring(0, 3) = '005'

 Cool kids would utilize getKeyPrefix() instead of hardcoding the first 3 digits of the ID.

All Answers

bob_buzzardbob_buzzard

I've done this in the past by determining the three character key prefix for the User sobject type (its 005) and then checking the lead owner id to see if it starts with this prefix.  If it doesn't, then its owned by a queue.

ckellieckellie

That is what I was thinking. If I was to write a formula fiueld in SF.com UI, I would write the formula as

 

If(Contains(lead.ownerid, '005'), lead.ownerid, null).

 

In apex I am not sure how to write the if statement as I am not sure how to write contain or start with or begins. Here is my stab:

 

For (CampaignMember cm : trigger.new) {

 

If(cm.lead.ownerid =='005***********')

OwnerName__c = cm.lead.ownerid;

 

}

 

How can I say "if ownerid begins with.... then..."?

 

Thanks,

ckellie

WizradWizrad
cm.lead.ownerid.substring(0, 3) = '005'

 Cool kids would utilize getKeyPrefix() instead of hardcoding the first 3 digits of the ID.

This was selected as the best answer
ckellieckellie

Thank you for your help,

 

Although I did not \figure hou to use getKeyprefix, I was able to complete the forloop without hardcoding the '005'

 

   String l;
   String sUser;
   String lUser;
   l = [select ownerid from Lead where id in: Lids].id;
   sUser = [select id from user where Name = 'Shayla Wentz'].id;
   List<contact> contact = [select ownerid from contact where id in:cIds];

   
    for(CampaignMember c: trigger.new){
      If(String.valueOf(l).substring(0, 3) == String.valueOf(sUser).substring(0, 3)){  
       
        c.OwnerName__c = l;
        } else{
       
        c.OwnerName__c = sUser;
        }
       
     if(c.contactid <> null){
        c.OwnerName__c = contact[0].ownerid;
        }
    }