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
stollmeyerastollmeyera 

Trigger to grab Owner Profile ID

I am a begginer at this and wrote a simple trigger to grab the Profile ID of a Lead owner and store it under a custom field.  It is below:

 

 

trigger ProfileIDCopy on Lead (before Insert, before Update) {

    for(Lead x : Trigger.New){

        // Confirm owner is not a Queue then map Profile ID to custom field
        if( ((String)x.OwnerID).substring(0,3) == '005' ){
            x.Owner_Profile_ID__c = x.Owner.Profile.ID;
        }
        else{
            // If not above, mark as queue
            x.Owner_Profile_ID__c = 'Queue';
        }
    }

}

 

The trigger saves, but it does not actually work when I test it.  The Owner_Profile__ID does not get populated.  My guess is that "Owner.Profile.ID" is not a proper call, but the fact that it saves with no error is not reassuring.  Could someone please offer some guidance?  Thanks in advance!

 

jkucerajkucera

I think you want 

x.Owner.ProfileId
hermihermi

for getting the Profile Ids, it will be better if you use Userinfo Methods. Try

userinfo.getprofileId

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_userinfo.htm

 

x.Owner_Profile_ID__c = x.userinfo.getProfileId;
Imran MohammedImran Mohammed

Did you find a solution for this?

stollmeyerastollmeyera
Thanks for the help everyone. I tried all of the suggestions to no avail.

I think the only option is to query the User table and then do a vlookup type call to grab the Profile ID for the referenced Owner ID. Now I just have to figure out how to actually do this in APEX :)

Any other suggestions would be welcome.
jkucerajkucera

Should be something like:

 

  List<user> u=[Select ProfileId FROM User WHERE Id =:x.owner LIMIT 1];
  x.owner_profile_id__c=u[0].ProfileId;

 If you want the name of the profile, you'd need to query profile to get it.

 

ccusicccusic
Something like this should work: 
Set<ID> leadOwnerIdSet = new Set<ID>();
List<Lead> leadOwnerLst = new List<Lead>();
for(Lead x : Trigger.New){
    // Confirm owner is not a Queue then map Profile ID to custom field
    if(String.valueOf(x.OwnerId).startsWith('005')){
        leadOwnerIdSet.add(x.OwnerId);
        leadOwnerLst.add(x);
    } else x.Owner_Profile_ID__c = 'Queue';    
}

Map<ID, User> profileMap = new Map<ID, User>([SELECT Id, ProfileId 
                                                FROM User 
                                                WHERE ID IN :leadOwnerIdSet]);
for(Lead currLead : leadOwnerLst) {
    if(profileMap.containsKey(currLead.OwnerId)) {
        User leadOwner = profileMap.get(currLead.OwnerId);
        if(leadOwner.ProfileId == 'xxx') {
            //Execute Lead Logic ...
        }
    }
}