You need to sign in to do that
Don't have an account?
jvolkov
Field Reference Not Working
This field reference is not working, not sure why. Basically it seems that it is assuming the reference field is blank even though it is populated.
for (Opportunity opp: trigger.new) { if(opp.CustomUserField__r.UserRoleId=='00E60000000oDgG') { //do this } }
Thanks
If you want to access reference objects field (deep linking) in trigger you have to do query. Reference object fields are not supported directly when accessing using child object.
For example in Apex Code we write SOQL something like
[select CustomUserField__r.UserRoleId from Opportunity]
So in trigger you have to additional query to access CustomUserField object's field.
Also, I would suggest not to use hard code ids. Do query for the same using Name.
The above code snippet should work for you. However as Ankit_Arora suggested you are using 15 digits in comparision.. and using query you always get 18 digit ID, so be careful using same digits in comparision.
With Best,
All Answers
Not sure about your problem, but have you used debug to track what value is returned by opp.CustomUserField__r.UserRoleId as it may return 18 digit Id and you are comparing it with 15 digit Id.
Try this and verify the userroleid is same with what you are comparing.
Thanks
Ankit Arora
Blog | Facebook | Blog Page
If you want to access reference objects field (deep linking) in trigger you have to do query. Reference object fields are not supported directly when accessing using child object.
For example in Apex Code we write SOQL something like
[select CustomUserField__r.UserRoleId from Opportunity]
So in trigger you have to additional query to access CustomUserField object's field.
Also, I would suggest not to use hard code ids. Do query for the same using Name.
The above code snippet should work for you. However as Ankit_Arora suggested you are using 15 digits in comparision.. and using query you always get 18 digit ID, so be careful using same digits in comparision.
With Best,
Thanks that works, although I was hoping to avoid having to add that much more code but no big deal.