+ Start a Discussion
jungleeejungleee 

Traversing Owner field in trigger

Hi All,

 

I am aware of the fact that we cannot traverse on the ownerId field in the formula field. But I came across a similar problem in triggers. In the below class I am passing a map from the trigger to a class as below

 

//passing newMap to the below method.

public static void someMethod(map<id, opportunity> newOppMap){
	list<opportunity> oppty  = newOppMap.values();
	//iterating thru the list of opportunity.
	for(opportunity opp: oppty){
		if(opp.owner.isportalEnabled){//I am unable to fetch the CORRECT value of isportalEnabled. it always returns False for all the owners.
		.....some code....
		}
	}
}

 Any idea why this is happening??

 

Workaround: to create a map of user based on the owner and pass the the id to the map to fetch the correct value.

 

Regards

Sam

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

The full object graph isn't populated when you hit a trigger, so while the OwnerId will be populated, the Owner reference won't be, so any attempt to access the related object's fields will return null.

 

Thus regardless of the type of lookup (even when polymorphic like the owner), you'd still need to retrieve the related objects.

All Answers

bob_buzzardbob_buzzard

The full object graph isn't populated when you hit a trigger, so while the OwnerId will be populated, the Owner reference won't be, so any attempt to access the related object's fields will return null.

 

Thus regardless of the type of lookup (even when polymorphic like the owner), you'd still need to retrieve the related objects.

This was selected as the best answer
kcpluspluskcplusplus

You could try nesting a loop like this going through each oppy and then the owner, it adds another query though, but it might be a good way to check your code. 

 

Might want to run a few queries in the workbench, just to check and make sure you are referencing correctly, and that you are referencing portal users. 

 

--KC

 

for(Opportunity o : oppty){
	for(User u : [SELECT id,IsPortalEnabled FROM User WHERE id = :o.OwnerId){
		if(u.IsPortalEnabled){
			//code here
		}
	}
}

 

bob_buzzardbob_buzzard

This is quite a bad idea - nesting soql queries inside for loop means they aren't bulk-safe - more than 100 records and you will blow governor limits.

 

There's a discussion on this at:

 

http://wiki.developerforce.com/page/Best_Practice:_Avoid_SOQL_Queries_Inside_FOR_Loops

jungleeejungleee

Thanks Bob for the clear explaination.

 

The workaround that I have uesd is this:

//passing newMap to the below method.

public static void someMethod(map<id, opportunity> newOppMap){
	set<id> ownerId = new set<id>();
	map<id, user> ownerMap = new map<id, user>();
	for(opportunity opp: newOppMap.values()){
		ownerId.add(opp.ownerId);
	}
	for(user u : [select id, isPortalEnabled from user where Id IN: ownerId]){
		ownerMap.put(u.id, u);
	}
	list<opportunity> oppty  = newOppMap.values();
	//iterating thru the list of opportunity.
	for(opportunity opp: oppty){
		user opptyOwner = ownerMap(opp.ownerId);
		if(opptyOwner.isPortalEnabled){
		.....some code....
		}
	}
}

 Hope it helps!!

 

 

Regards

Sam