Find your content:

Search form

You are here

UserMap, getting NAME for User, not ID, in Trigger

 
Share

UPDATED 1pm EST:

I am working with THIS line of code, where LEFT of the equals sign is NOT the issue, ONLY the usermap reference. I am trying to replace the OwnerID with the actual NAME from the User database.

accObj.put('pb_Close_Owner_' + tempFieldName + '__c',  userMap.get(tmpClosedWonOwner).name );

. .

ORIGINAL POST:

What if you are trying to populate the Owner Name from a VARIABLE that contains the ID of the user? How would this one line change?

c.MerchantOwner__c = userMap.get(accountMap.get(c.AccountId).OwnerId).name;

(ABOVE THIS IS NOT THE CODE I'M WORRIED ABOUT, SEE BELOW)

I tried some variations but can't get it to work. I ENVISION it being something similar to:

accObj.put('xx',  userMap.tmpClosedWonOwner.name );

Thanks. (this is a cut down version of the code I'm using, hence the diff on the left - I am using a reference to a previous Answer on the first codeset)


Attribution to: AMM

Possible Suggestion/Solution #1

accountMap.get(c.AccountId).OwnerId

This above code has error .Since the output from the map accountMap.get(c.AccountId) i assume its a record and say record is type account named 'NewAccount' and now you cannot refer as a field as NewAccount.ownerId.

The second one is dynamic dml where you input both field token and value and hence its valid.


Attribution to: Mohith Shrivastava

Possible Suggestion/Solution #2

Most likely either accountMap or userMap are returning null and then throwing an error when you try to reference the null value or have not been initialized themselves. Try the folllowing:

system.debug('accountMap: '+ accountMap);
system.debug('userMap: 'userMap)

Account acc = accountMap.get(c.AccountId);
system.debug('account: '+acc);

User u = userMap.get(acc.OwnerID);
system.debug('user: '+u);

c.MerchantOwner__c = u.name;

Attribution to: Greg Grinberg
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/3333

My Block Status

My Block Content