You need to sign in to do that
Don't have an account?
John 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.
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
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
startWith ==> startsWith.
for (Lead newLead : Trigger.new) {
if (string.valueOf(newLead.OwnerId).startsWith('005')){
ownerIds.add(newLead.OwnerId);
}
}
Thanks,
Maharajan.C
All Answers
HTH
Andrew
Owner can be a user or a queue .Id will be different for queue and user.we can distinguish them using:
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
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
* Try below code it will work fine:
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
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
I'm still getting the Null Pointer Exception when using if(mapUsers!=Null).
startWith ==> startsWith.
for (Lead newLead : Trigger.new) {
if (string.valueOf(newLead.OwnerId).startsWith('005')){
ownerIds.add(newLead.OwnerId);
}
}
Thanks,
Maharajan.C