Search code examples
salesforceapex-code

Can we have relationships in a SOSL Query


I want to use SOSL to get relationship values?

List<List<SObject>> searchList = [FIND :mySearchText IN ALL FIELDS 
                              RETURNING 
                                 Account (id, name,phone, BillingStreet,BillingCity,BillingState, ownerID.Alias,ownerID.MobilePhone )];

I am getting an error

Save error: Didn't understand relationship 'ownerID' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the  describe call for the appropriate names.

Is there something i am doing wrong?


Solution

  • You can access the relationship, but you need to use Owner.UserField rather than OwnerId.UserField.

    This should work for you:

    List<List<Account>> searchList = [FIND 'test' IN ALL FIELDS RETURNING 
        Account (id, name, phone, BillingStreet, BillingCity, BillingState, 
        OwnerId, Owner.Alias, Owner.MobilePhone)];
    
    system.debug(searchList[0][0].Owner.Alias);
    system.debug(searchList[0][0].Owner.MobilePhone);