3

I'm having an issue with the type field on related records from an SOSL search.

In our organization, Leads can be owned by either a User or a Group/Queue and Accounts can only be owned by Users.

SOSL Query

FIND 
    {test@test.com} IN Email Fields 
RETURNING 
    Lead(Id, OwnerId, Status, Owner.Email, Owner.Name), 
    Account(Id, OwnerId, Type, Owner.Email, Owner.Name)

My result set contains all the required fields, but the owner type on Leads does not show the correct type; it always shows Name as the type. Accounts show correctly.

To get the correct Owner Type, I'm currently comparing the OwnerId with the keyPrefix from the sObject description to determine whether the Lead is owned by a User or a Group.

Is there a way to make the SOSL Search return the correct owner type for Leads?

I'm using the Force.com PHP Toolkit and the SOAP API to retrieve the data. An example result set is below.

Result Set

Array
(
    [0] => SObject Object
        (
            [type] => Lead
            [fields] => stdClass Object
                (
                    [OwnerId] => 00570000000xxxAA2
                    [Status] => Unverified
                    [Owner] => SObject Object
                        (
                            [type] => Name             (This should be User)
                            [fields] => stdClass Object
                                (
                                    [Email] => user1@myorg.com
                                    [Name] => Joe User
                                )

                        )

                )

            [Id] => 00Q7000000xnxnxEAO
        )

    [1] => SObject Object
        (
            [type] => Lead
            [fields] => stdClass Object
                (
                    [OwnerId] => 00G00000000xgxgxAA2
                    [Status] => Unverified
                    [Owner] => SObject Object
                        (
                            [type] => Name             (This should be Group)
                            [fields] => stdClass Object
                                (
                                    [Email] => group@myorg.com
                                    [Name] => US/CN
                                )

                        )

                )

            [Id] => 00Q7000000xlxlxEAO
        )

    [2] => SObject Object
        (
            [type] => Account
            [fields] => stdClass Object
                (
                    [OwnerId] => 0057000000xaxaxAAI
                    [Type] => Prospect
                    [Owner] => SObject Object
                        (
                            [type] => User             (This is correct)
                            [fields] => stdClass Object
                                (
                                    [Email] => user2@myorg.com
                                    [Name] => Jane User
                                )

                        )

                )

            [Id] => 0017000000xnxnxAAK
        )

)
6
  • Have you tried Owner.Type instead of just Type? – Chris Duncombe Aug 20 '13 at 13:08
  • Owner.Type is not a valid field. Type isn't really a Property of the record; it's a description of the record. – keithhatfield Aug 20 '13 at 13:12
  • Well I just ran this in execute anon and it works list<Lead> leads = [Select Id, Name, OwnerId, Owner.Type From Lead Limit 25]; For(Lead l: leads){ if(l.Owner.Type == 'User'){ system.debug('Its a user'); } if(l.Owner.Type == 'Queue'){ system.debug('Its a queue'); } } – Chris Duncombe Aug 20 '13 at 13:18
  • If I add Owner.Type into the query, I get and API exception because Owner.Type is an invalid field for the User object type. – keithhatfield Aug 20 '13 at 13:23
  • You are using SOQL not SOSL ... I need to do an SOSL search (FIND) ... – keithhatfield Aug 20 '13 at 13:24
2

From memory, that polymorphic field doesn't quite work as expected in this case. I vaguely recall that "Name" is always returned, and that there may not be a SOSL-native way to get the return type. Like I said, vague recollection, but it's supported by the fact that the SOSL docs don't discuss polymorphic fields while the SOQL docs do refer to them.

Couple workarounds I can think of:

  • inspecting the key prefix isn't that bad a workaround, IMO. I use key prefix inspection pretty regularly in other contexts. It's cheap, and it works.
  • you could create a custom formula (text) field on lead, called something like "Owner Type", with a formula that refers to the polymorphic owner field (relatively new feature) and have your SOSL return it - the formula would be something like IF(ISBLANK(Owner:Queue.Id), "User", "Queue") - I have tested this and it does work.

Like I said, there may be a SOSL-native way I'm not aware, but either of those other approaches above should work fine also.

1
  • Yep ... I'm just going to keep checking the prefix ... Thanks for your input! – keithhatfield Aug 21 '13 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.