+ Start a Discussion
VollyVolly 

Field being populated is showing record ID, not the data in the field

On a previous thread:

http://community.salesforce.com/t5/Apex-Code-Development/Map-user-custom-field-to-custom-object-via-trigger/td-p/188212

I had an issue with mapping custom fields from the owner of an account to a custom object. I have that working but I now have one small issue. I mapped the standard field "Role" and I'm populating the "User Role" field in my custom object with the following code:

 

        if (xxx.Owner_Role__c == null)
            xxx.Owner_Role__c = approversAccount.owner.UserRole.id;

 The problem is it is showing the record ID, not the text in the field from the user record. The field in the user record has "200" as the role. In my test I'm getting 00E30000000bwlUXXX  in the custom object field. How do I fix this so it will show "200" in the custom object field?

 

Shilpa_SFShilpa_SF

Hi Volly,

 

              Here in your above code you are retrieving the Id not the role name , to do so u will have to do one more query on the User object. This is how u should do :

 

1) Retrieve the OwnerId.

2) Query the rolename in user object for the respective ownerid.

(Query: Select UserRole.Name,UserRoleId From User where id='retrieved userid')

3) Assign the field to the desired destination field