+ Start a Discussion
MiguelGuerreiroMiguelGuerreiro 

Trigger on Case - Error when Owner is a Queue

Hello,

 

I have a trigger (before insert, before update) to update some number fields. The field to update depends on the case owner role. This is the code that is getting the error:

 

 

for (Case updatedCase:System.Trigger.new) { Case oldCase = System.Trigger.oldMap.get(updatedCase.Id); User usr = [select Id,UserRoleId from User where Id =:oldCase.OwnerId]; UserRole role = [select Id,Name from UserRole where Id =:usr.UserRoleId];

 Is errors when the owner is a Queue. Is there a easy way to solve this?

 

thanks!

 

Miguel

 

 

Best Answer chosen by Admin (Salesforce Developers) 
JimRaeJimRae

I would agree witw micwa, you should filter when the user is a user type(005) or when not equal to queue type (00G).

You have another issue with your coding.  You have 2 SOQL statements inside of a for loop, this is likely to give you max query governor errors.

You should refactor your code to do one query  and a Map to store and reference the data.

Lastly, you can use a relationship query to get the role name from a user query.

 

This query can get both for you:

 

select Id,UserRoleId,userrole.name from User where Id =:oldCase.OwnerId

 

 

 

All Answers

micwamicwa

Prefix User: 005

Prefix Group (Queue): 00G

 

So whenever your ownerId starts with OOG you know that this is a queue and there's no entry in the user table.

JimRaeJimRae

I would agree witw micwa, you should filter when the user is a user type(005) or when not equal to queue type (00G).

You have another issue with your coding.  You have 2 SOQL statements inside of a for loop, this is likely to give you max query governor errors.

You should refactor your code to do one query  and a Map to store and reference the data.

Lastly, you can use a relationship query to get the role name from a user query.

 

This query can get both for you:

 

select Id,UserRoleId,userrole.name from User where Id =:oldCase.OwnerId

 

 

 

This was selected as the best answer
MiguelGuerreiroMiguelGuerreiro

I'm having issues with my code because of those queries inside the FOR loop (too mayn queries when I do mass update on cases). But I havent found a solution for this... I have a custom object to store user roles ids + queue ids. Then in my trigger I must check if the case owner role is store in the custom object (to update some fields). 

 

I must iterate over each triggered case to get the case owner id. With the ID I get the role. How can I not break the query limit?

 

I'm sure there's a way but I'm quite puzzled at the moment. 

 

Thanks for any help.

 

 

 

Message Edited by MiguelGuerreiro on 08-07-2009 03:51 PM
MiguelGuerreiroMiguelGuerreiro

solution: create two arrays outside the loop...