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
lodoss118lodoss118 

Trying to get the userRold id from the account owner relationship?

Hi i am having problems in getting the account owner user role id, i was trying to do this but it is wrong from the relationship query?

Didn't understand relationship 'User__r' in FROM part of query call

:'(

public static void Find_User(Account[] b)
{
   try {

             for(Account a : [select Id, PostCode_Area__c, (select Id, Name, UserRoleId,
                          Working_Area__c from User where Id = :b.OwnerId)
                                  from Account where Id = :b.Id])  {
             }

       }
}
SuperfellSuperfell
select id, owner.userRoleId from account

The SOQL-R part of the API docs covers it pretty well
http://www.salesforce.com/us/developer/docs/api/index_CSH.htm#sforce_api_calls_soql_relationships.htm
lodoss118lodoss118
Hi  i keep getting too many soql rows when upadting quite alot of accounts where this code is triggered from so is there any easier way around this to get acount owner user role id? or what am i doing wrong to get this error too many soql rows it always points to that soql statement?
SuperfellSuperfell
would need to see the trigger, no doubt you are doing queries for each account in the trigger, which will always run you into trouble, you should review some of the posts about "bulkifying" your trigger code.
lodoss118lodoss118
My trigger in my accoun calls a class that auto assigns the user depending on their working area but i don;t want accounts that are owned by special people to be changed.


in my class  ihave this method that checks the owner role id: -


public static Boolean isValidRole(Id ownerId) {

if(ownerId != null) {

User o = [select Id, UserRoleId from User where Id = :ownerId];
Id id = o.UserRoleId;

if(id == '123132123' || id == '12313321' || id == '12313123123321' || id == '123132132'
|| id == '12313123' || id == '12313123132' || id == '123131331' || id == '123123312'
|| id == '1231312321' || id == '1231332131')
{

return false;

}
}

return true;
}

Called in Find user method :-

public static void Find_User(Account[] b)
{
try {

for(Account a : b) {

if(isValidRole(a.OwnerId) != true)break;

String postCode = null;
String areaCode = null;
}
}
}

the trigger is called in account before insert, before update i.e.

Find_User(trigger.new);


SuperfellSuperfell
Right, so you do one query per row, that's not going to work. You need to do one query to get all the roleIds in a single query, something like

Set ownerIds = new Set;
for(Account a : Trigger.new)
ownerIds.add(a.ownerId);

Map owners = [select, id, userRoleId from user where id in :ownerIds];

// now do stuff with the Trigger.new array, using the owners Map, to get the userRoleId of the account owner.
lodoss118lodoss118
how come if i do select id, owner.userRoleId from account

owner.userRoleId is always null?