1

For my batch job, I've written the following query, since i want a string for the email, since I need a string (which I'll convert to string array later) for the setToAddresses method of the singleEmailMessage :

String mgrEmail = [select Owner.manager.email from Lead].Owner.manager.email;

The error is:

cannot understand relationship 'manager' in field path

How can I fix it?

List u = [select Owner.Id from Lead WHERE Owner.Type IN ('User')];

gives the following error : Illegal assignment from list to list

1
  • As Owner can be a user or a Queue and so on, you cannot do this in one simple query.
    – m Peixoto
    Apr 24, 2017 at 13:06

2 Answers 2

4

Lead.Owner is a polymorphic relationship between Lead and User. You need to tell your query that's the relationship between the two. See the documentation on Working with Polymorphic Relationships in SOQL Queries. Your query should look more like the following:

list<User>Usr = [select Owner.Id from Lead WHERE Owner.Type IN ('User')];

You'll probable then need to run a separate query to get your manager Email

list<User>mgrUsrs = [Select manager.email FROM User where Id IN: Usr];

// use a for loop to extract the list of email addresses into list<string>mgremails

You shouldn't need a WHERE manager.Type IN (User) since Manager is a valid field of User, but you may since it's also a reference to a different User. I've never tried to get that field, so you'll have to try it for yourself.

If the owner can be a Queue in your org, you'll want to take that into account by changing the first query to WHERE Owner.Type in (User, Queue), then adjust the 2nd query accordingly.

4
  • List <User> u = [select Owner.Id from Lead WHERE Owner.Type IN ('User')]; gives the following error : Illegal assignment from list to list
    – ngc
    Apr 24, 2017 at 18:40
  • See edit above where I've applied LIMIT 1 clause and had query return 1 record. I don't know the context you're using this in, so you may want to bulkify it; in which case you'd want a list<string> of email addresses you'd get from your 2nd query.
    – crmprogdev
    Apr 24, 2017 at 20:39
  • Sir, this time for the first query, i got the following error : Illegal assignment from list to user. I'm trying to write a batch job that searches for leads that have not been modified in the current month. for all such leads, i want to send an email alert to the lead owner's manager
    – ngc
    Apr 24, 2017 at 21:14
  • Since I don't know what your batch code looks like, I've bulkified the code to give you a list from the first query and used those results in the 2nd query. If that doesn't work, you may need to extract the Ids from the 1st query into a list for use in the 2nd query even though that's what gets returned from the 1st. You'll then need to extract the email addresses.
    – crmprogdev
    Apr 24, 2017 at 21:41
1

I do not think you can use this SOQL syntax as Lead owner represents polymorphic relationships where Owner can be a Group or User. You would need to make use of "TYPEOF" clause in order to achieve your requirement.

[SELECT 
 TYPEOF Owner 
  WHEN USER THEN Owner.Manager.Email 
 END
 FROM Lead]

Please note:

TYPEOF is currently available as a Developer Preview as part of the SOQL Polymorphism feature. For more information on enabling TYPEOF for your organization, contact Salesforce.

2
  • 2
    TypOf is in Developer Preview, but querying for Type is currently available.
    – crmprogdev
    Apr 24, 2017 at 13:29
  • Yes, agreed. I was thinking in lines of achieving using single query and hence suggested TypeOf. Apr 24, 2017 at 13:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .