Salesforce SOQL To Get Case Owner In One Hit
Using Salesforce SOQL I can get the Owner's Id using the following:
SELECT Case.OwnerId
FRO开发者_StackOverflowM Case
WHERE Case.CaseNumber = '00001234'
I can then get the User details for the User who owns the case in this query:
SELECT User.Id, User.Name, User.Custom_Field__c
FROM User
WHERE User.Id = '001A0000001abc1DEF'
But I can't get it to work in a single statement, I think this is because Owner != User, even though the owner is in fact a user in this case. I have tried:
SELECT Owner.Custom_Field__c
FROM Case
WHERE Case.CaseNumber = '00001234'
But I get an error that the Custom_Field__c is not a valid field.
Owner on Case is a polymorphic relationship, it can be one of multiple different types. When that happens then using SOQL-R you can only select a subset of fields that are common to the types being pointed to (these are all exposed on a pseudo entity called "Name"), so you won't be able query the custom fields on User. You'd have to do a query on Case, then collect up the set of Owners and make a query to User and/or Group to the get more detailed information.
You can do this with a semi-join in SOQL. I tested it out, and it still works even though Owner is polymorphic:
SELECT Custom_Field__c
FROM User
WHERE Id IN (SELECT OwnerId
FROM Case
WHERE Case.CaseNumber = '00001234')
精彩评论