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
John Lin 46John Lin 46 

IF statement Lead Owner != Queue

Hello all,

I'm trying to populate owner's name to Enrollment_Rep__c field on Lead Object based on the profile name.  Everything works till I change the Owner from User to Queue.  I'm running to System.NullPointerException on the trigger.
trigger EnrollmentRep on Lead (before insert, before update) {
    
    Set<Id> ownerIds = new Set<Id>();

    for (Lead newLead : Trigger.new) {
        ownerIds.add(newLead.OwnerId); 
    }

    Map<Id,User> mapUsers = new Map<Id, User>([SELECT Id, Profile.Name, Name FROM User Where Id In :ownerIds]);

    for (Lead newLead : Trigger.new) {
        User oOwner = mapUsers.get(newLead.OwnerId);
        
        if (oOwner.Profile.Name == 'CEO'){
            newLead.Enrollment_Rep__c = oOwner.Name;
        }
    }
}

I'm new to the trigger and I'm not sure where to use IF statement Lead Owner != Queue.

Any suggestions will be helpful.

Thanks!
​​​​​​​John
 
Best Answer chosen by John Lin 46
Maharajan CMaharajan C
Sorry John, Small typo mistake.Now try it.   

startWith    ==>   startsWith. 


for (Lead newLead : Trigger.new) {
        if (string.valueOf(newLead.OwnerId).startsWith('005')){
            ownerIds.add(newLead.OwnerId); 
        }
    }

Thanks,
Maharajan.C

All Answers

Andrew GAndrew G
for (Lead newLead : Trigger.new) {
    if(newLead.Owner.Type=='User'){
        System.debug('Owner is a User') 
        // do your user processing
    else {
        System.debug('Owner is a Queue') 
        // do your queue processing
    }
}
or you could try
for (Lead newLead : Trigger.new) {
    String txtLdID = newLead.ownerId;
    if(txtLdID.startsWith('005') == TRUE){
        System.debug('Owner is a User') 
        // do your user processing
    else {
        System.debug('Owner is a Queue') 
        // do your queue processing
    }
}
or for the second, you could test for "00G" is looking for the Queue rather than the user


HTH

Andrew

 
FARSANA PSFARSANA PS
Hai John,

Owner can be a user or a queue .Id will be different for queue and user.we can distinguish them using:
 

if(string.valueOf(OwnerId).startsWith('005'))
       {
                  //owner is user
       }

if(string.valueOf(OwnerId).startsWith('00G'))
       {
                  //owner is Queue
       }


Null pointer exception occurs due to  User oOwner = mapUsers.get(newLead.OwnerId); 
When lead  owner is queue.This statement will return null.Since mapUsers contains details of user only .We have to implement seperate logic for when owner is queue and when owner is user.

Hope this will help.
Thanks

Maharajan CMaharajan C
Hi John,

Try the below updated Trigger:

trigger EnrollmentRep on Lead (before insert, before update) {
    
    Set<Id> ownerIds = new Set<Id>();

    for (Lead newLead : Trigger.new) {
        if(string.valueOf(newLead.OwnerId).startsWith('005'))
        {
            ownerIds.add(newLead.OwnerId); 
        }
        
    }

    Map<Id,User> mapUsers = new Map<Id, User>([SELECT Id, Profile.Name, Name FROM User Where Id In :ownerIds]);

    for (Lead newLead : Trigger.new) {
        
        if(mapUsers.containsKey(newLead.OwnerId))
        {
        User oOwner = mapUsers.get(newLead.OwnerId);
            if (oOwner.Profile.Name == 'CEO'){
                newLead.Enrollment_Rep__c = oOwner.Name;
            }
        }
    }
}


Thanks,
Maharajan.C
Ajay K DubediAjay K Dubedi
Hi John,

* Try below code it will work fine:
 
Trigger--->
trigger EnrollmentRep on Lead (before insert, before update)
{
    Set<Id> ownerIds = new Set<Id>();
    for (Lead newLead : Trigger.new)
    {
        ownerIds.add(newLead.OwnerId); 
    }
    
    Map<Id,User> mapUsers = new Map<Id, User>([SELECT Id, Profile.Name, Name FROM User Where Id In :ownerIds]);
    if(mapUsers!=Null)
    {
        for (Lead newLead : Trigger.new)
        {
            User oOwner = mapUsers.get(newLead.OwnerId);
            if (oOwner.Profile.Name == 'CEO')
            {
                newLead.Enrollment_Rep__c = oOwner.Name;
            }
        }
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
John Lin 46John Lin 46
Hi Maharajan C,

I'm getting the error when using:
    for (Lead newLead : Trigger.new) {
        if (string.valueOf(newLead.OwnerId).startWith('005')){
            ownerIds.add(newLead.OwnerId); 
        }
    }

Error: Method does not exist or incorrect signature: void startWith(String) from the type String
John Lin 46John Lin 46
Hi Ajay K Dubedi,

I'm still getting the Null Pointer Exception when using if(mapUsers!=Null).
Maharajan CMaharajan C
Sorry John, Small typo mistake.Now try it.   

startWith    ==>   startsWith. 


for (Lead newLead : Trigger.new) {
        if (string.valueOf(newLead.OwnerId).startsWith('005')){
            ownerIds.add(newLead.OwnerId); 
        }
    }

Thanks,
Maharajan.C
This was selected as the best answer