• John Lin 46
  • NEWBIE
  • 10 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
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
 
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