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
jvolkovjvolkov 

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

 

Best Answer chosen by Admin (Salesforce Developers) 
Prafull G.Prafull G.

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.

 

Set<Id> setUserIds = new Set<Id>();

for(Opportunity opp : trigger.new){
setUserIds(opp.CustomUserField__c);
}

Map<Id, User> mapUser = new Map<Id, User>([select UserRoleId from User where Id IN : setUserIds]);



for (Opportunity opp: trigger.new) {

if(mapUsers.get(opp.CustomUserField__c).UserRoleId=='00E60000000oDgG') {
//do this
}
}

 

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

Ankit AroraAnkit Arora

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.

 

 

for (Opportunity opp: trigger.new) {
	
	System.debug(' ::::::::::::::  ' + opp.CustomUserField__r.UserRoleId) ;
	if(opp.CustomUserField__r.UserRoleId=='00E60000000oDgG') {
		//do this
	}
}

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

 

Prafull G.Prafull G.

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.

 

Set<Id> setUserIds = new Set<Id>();

for(Opportunity opp : trigger.new){
setUserIds(opp.CustomUserField__c);
}

Map<Id, User> mapUser = new Map<Id, User>([select UserRoleId from User where Id IN : setUserIds]);



for (Opportunity opp: trigger.new) {

if(mapUsers.get(opp.CustomUserField__c).UserRoleId=='00E60000000oDgG') {
//do this
}
}

 

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,

 

This was selected as the best answer
jvolkovjvolkov

Thanks that works, although I was hoping to avoid having to add that much more code but no big deal.