1

Is it possible to SOQL Salesforce for a Record Name without knowing the object name?

I will only know the of the record not the ID or Object unfortunately.

  • Thanks guys I looked into that but... This is essentially for integration in which I will only have a record name where there are no look ups and no ID..... – CodeMonger May 15 at 13:16
  • Is it possible to use the Global Search somehow?? Such as UnifiedSearchResults?searchType through code? – CodeMonger May 15 at 13:19
  • Have you taken a look at search for string rest api, if that helps? – Jayant Das May 15 at 13:25
2

On top of SOSL searches which can find a search string across different entities, e.g.,

FIND {MyRecordName}

which returns the IDs of the records where text is found, you can also utilize REST API here.

If you are open to use REST API, you can utilize the Search for a String here. This will allow you to achieve your use case to just provide the record name and no object details. The result in return will provide you the details as which records consist of the search string. You can find more details on the documentation.

A note here, even though the docs doesn't mention which fields are returned in this search, but it always returns results on matching Name field on different objects (I confirmed this with a test).

So your sample request will look like as below.

Using SOSL Search:

/services/data/v43.0/search/?q=FIND+%7BMyRecordName%7D

Using Parameterized Search:

/services/data/v43.0/parameterizedSearch/?q=MyRecordName

And response will look as:

{
    "searchRecords": [
        {
            "attributes": {
                "type": "Contact",
                "url": "/services/data/v43.0/sobjects/Contact/xxxx"
            },
            "Id": "xxxx"
        },
        {
            "attributes": {
                "type": "Account",
                "url": "/services/data/v43.0/sobjects/Account/xxxx"
            },
            "Id": "xxxx"
        }
    ]
}
2

SOSL lets you search multiple objects to find matching data. An example search might look like:

FIND 'Doe' IN NAME FIELDS

If you do know some objects you want to limit it to, you could add them as follows:

FIND 'Doe' IN NAME FIELDS RETURNING Account, Contact, User
  • I like it but I would have to know the object name. – CodeMonger May 15 at 13:14
  • If you don't know which objects you even want to check, you're out of luck, barring some complicated code implementation. – Adrian Larson May 15 at 13:26
  • According to the SOSL examples, it should be possible to search any object: 'Look for joe anywhere in the system. Return the IDs of the records where joe is found.' FIND {joe} developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/… – Folkert May 15 at 13:38
  • Hmm, it didn't work when I tried it initially, but it may be that the search term simply wasn't found. I'll update accordingly. – Adrian Larson May 15 at 13:41
1

You should probably Use a SOSL for the above use case.

https://trailhead.salesforce.com/en/content/learn/modules/apex_database/apex_database_sosl

1

No, the documentation on SOQL syntax provides the structure needed for valid queries

SELECT fieldList [subquery][...]
[TYPEOF typeOfField whenExpression[...] elseExpression END][...]
FROM objectType[,...] 
    [USING SCOPE filterScope]
[WHERE conditionExpression]
[WITH [DATA CATEGORY] filteringExpression]
[GROUP BY {fieldGroupByList|ROLLUP (fieldSubtotalGroupByList)|CUBE (fieldSubtotalGroupByList)} 
    [HAVING havingConditionExpression] ] 
[ORDER BY fieldOrderByList {ASC|DESC} [NULLS {FIRST|LAST}] ]
[LIMIT numberOfRowsToReturn]
[OFFSET numberOfRowsToSkip]
[FOR {VIEW  | REFERENCE}[,...] ]
      [ UPDATE {TRACKING|VIEWSTAT}[,...] ]

the clause description for objecttype states the following:

Specifies the type of object that you want to query(). You must specify a valid object...

some of the optional ones would be:

TYPEOF, WHERE, WITH, GROUP BY, and ORDER BY

  • Not everything that is there is required, and which is what might be non-obvious to the untrained eye. – Adrian Larson May 15 at 13:32
  • true, the doc does state some of the optional ones, updating accordingly – glls May 15 at 13: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.