Discussions
- General Development
- Schema Development
- Apex Code Development
- Visualforce Development
- Formulas & Validation Rules
- Security
- Mobile
- Force.com Sites & Site.com
- Chatter Development
- Java Development
- .NET Development
- Perl, PHP, Python & Ruby
- Desktop Integration
- APIs and Integrations
- Visual Workflow
- Apple, Mac and OS X
- VB and Office Development
- AppExchange Directory & Packaging
- Salesforce Labs & Open Source Projects
- Other Salesforce Applications
- Jobs Board
- Force.com Discussion Boards
- :
- Developer Boards for Force.com and Database.com
- :
- Apex Code Development
- :
- Traversing Owner field in trigger
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
Traversing Owner field in trigger
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-02-2013 04:58 AM
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
Solved! Go to Solution.
Re: Traversing Owner field in trigger
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-02-2013 05:46 AM
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.
Certified Salesforce Technical Architect, Developer, Advanced Developer, Administrator, Advanced Administrator, Consultant, Sales Cloud Consultant,Service Cloud Consultant
Force.com MVP | The Bob Buzzard Blog | Linked In | Twitter
I don't respond to private messages/emails asking for help.
Take the Bob Buzzard Sobject Fields quiz.
Re: Traversing Owner field in trigger
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-02-2013 05:49 AM
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
}
}
}
Re: Traversing Owner field in trigger
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-02-2013 06:16 AM
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:
Certified Salesforce Technical Architect, Developer, Advanced Developer, Administrator, Advanced Administrator, Consultant, Sales Cloud Consultant,Service Cloud Consultant
Force.com MVP | The Bob Buzzard Blog | Linked In | Twitter
I don't respond to private messages/emails asking for help.
Take the Bob Buzzard Sobject Fields quiz.
Re: Traversing Owner field in trigger
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-02-2013 08:31 AM
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

