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
kminevkminev 

Determine if case owner is a Queue or a user via Apex

Hi,

 

I need to come up with a piece of code that will allow me to determine if the case owner is a queue or a user, The first things that come to my mind is to run a query on the owner and try to store the result to a user obect. If it faile therefore the case owner is a queue. I don't think this is a slick solution bo any means, so I am hoping someone can provide with a better suggestion.

 

Thank you.

Jeremy-KraybillJeremy-Kraybill

You just want to query Owner.Type on Case, e.g. select owner.type from case where id='blah..'

 

Hope that helps

 

Jeremy Kraybill

sfdcfoxsfdcfox

In addition to that, you can also run a DescribeSObjects on User and Queue, and check the keyPrefix values against the first three characters of the OwnerId, which will identify the type of the record. This method would be adventageous if you wanted to potentially modify your code to support other objects, such as Leads and Custom Objects as well as Cases.

kminevkminev

Thank you, is there also a way via an apex trigger to determine if the auto-assignment check box is selected?

kminevkminev

Can you provide with an apex code for the owner.type query. Do I need to query against the user or case?

sfdcfoxsfdcfox

Thank you, is there also a way via an apex trigger to determine if the auto-assignment check box is selected?

 

Don't I wish. Regrettably, you can't tell if an assignment rule will fire based on any code in Apex. This effect occurs after the record is committed to the triggers and does not cause triggers to run again. Custom Apex Code-based assignment rule logic that uses that checkbox would be a nice feature.

 

Can you provide with an apex code for the owner.type query. Do I need to query against the user or case?

 

It looks like this:

 

 

for(case c:[select id,owner.type,ownerid,contactid from case where id in :caseids]) {
  if(c.owner.type=='User') {
    // This is a user owned case
  }
  if(c.owner.type=='Queue') {
    // This is a queue owned case
  }
}

You can use Owner.Type both as a field reference (as above), and as a filter condition, such as "owner.type = 'Queue'".

 

 

 

JamuraiJamurai
Also, all User IDs start with '005' and all Queue IDs start with '00G'
TheDeckbladTheDeckblad
Jamurai's post is best post. Don't waste SOQL queries on something this simple.