+ Start a Discussion
KarlKarl 

Invalid field Email for SObject Name

In an Apex script, I do the following:

____________________

Case myCase = [Select
            Id,
            Owner.FirstName,
            Owner.LastName,
            Owner.Alias,
            Owner.Email
            From Case
            where id = :id];

// the id field in the SOQL where clause is passed into the method

string ownerEmail = myCase.Owner.Email;
_________

The last  line generates the error "Invalid field Email for SObject Name" when trying to compile.  When I try this query in the Sforce Explorer, It shows the relationship as "Name" for Case and Task and you can drill down and see the appropriate results.  But other objects show the relationship for Owner as "User".  I've tried casting to User but that generates an error saying that "Name" is never an instance of "User".

Is this a known issue, and are there any work-arounds?


Message Edited by Karl on 07-07-2008 10:39 AM
werewolfwerewolf
That's because the Owner field on Case is polymorphic -- it can be a User or a Queue.  This means it's subject to the polymorphic key rules where only a couple of fields are available for relationships (look up the word "polymorphic" in the API docs for info on that).

Instead, get the list of OwnerIds for your cases, pull out any ones that are not Users (i.e. don't start with '005'), and do a retrieve call to get all the User objects that correspond to them.