Chilkat Online Tools

SQL Server / Salesforce Platform APIs / Get Lookup Field Suggestions for a Specified Object

Back to Collection Items

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    -- Adds the "Authorization: Bearer <access_token>" header.
    EXEC sp_OASetProperty @http, 'AuthToken', '<access_token>'

    DECLARE @sbResponseBody int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @http, 'QuickGetSb', @success OUT, 'https://domain.com/services/data/v{{version}}/ui-api/lookups/:SOBJECT_API_NAME/:FIELD_API_NAME/:TARGET_API_NAME', @sbResponseBody
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @sbResponseBody
        RETURN
      END

    DECLARE @jResp int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @jResp OUT

    EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
    EXEC sp_OASetProperty @jResp, 'EmitCompact', 0


    PRINT 'Response Body:'
    EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @http, 'LastStatus', @respStatusCode OUT

    PRINT 'Response Status Code = ' + @respStatusCode
    IF @respStatusCode >= 400
      BEGIN

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @http, 'LastHeader', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @sbResponseBody
        EXEC @hr = sp_OADestroy @jResp
        RETURN
      END

    -- Sample JSON response:
    -- (Sample code for parsing the JSON response is shown below)

    -- {
    --   "count": 2,
    --   "currentPageToken": null,
    --   "currentPageUrl": "/services/data/v58.0/ui-api/lookups/Contact/AccountId/Account?searchType=Recent&page=1&pageSize=25",
    --   "nextPageToken": null,
    --   "nextPageUrl": null,
    --   "previousPageToken": null,
    --   "previousPageUrl": null,
    --   "records": [
    --     {
    --       "apiName": "Account",
    --       "childRelationships": {},
    --       "eTag": "2316c5f4a27ac31b407d91aab610b281",
    --       "fields": {
    --         "DisambiguationField": {
    --           "displayValue": null,
    --           "value": null
    --         },
    --         "Id": {
    --           "displayValue": null,
    --           "value": "001B000000UnQ2yIAF"
    --         },
    --         "Name": {
    --           "displayValue": null,
    --           "value": "Dickenson plc"
    --         },
    --         "Site": {
    --           "displayValue": null,
    --           "value": null
    --         }
    --       },
    --       "id": "001B000000UnQ2yIAF",
    --       "lastModifiedById": "005B0000003TOI6IAO",
    --       "lastModifiedDate": "2017-08-18T14:46:22.000Z",
    --       "recordTypeId": "012000000000000AAA",
    --       "recordTypeInfo": null,
    --       "systemModstamp": "2017-08-18T14:46:22.000Z",
    --       "weakEtag": 1503067582000
    --     },
    --     {
    --       "apiName": "Account",
    --       "childRelationships": {},
    --       "eTag": "cb5afb4494d9c9f6f81ab0e5f214585c",
    --       "fields": {
    --         "DisambiguationField": {
    --           "displayValue": null,
    --           "value": null
    --         },
    --         "Id": {
    --           "displayValue": null,
    --           "value": "001B000000UnQ2wIAF"
    --         },
    --         "Name": {
    --           "displayValue": null,
    --           "value": "Burlington Textiles Corp of America"
    --         },
    --         "Site": {
    --           "displayValue": null,
    --           "value": null
    --         }
    --       },
    --       "id": "001B000000UnQ2wIAF",
    --       "lastModifiedById": "005B0000003TOI6IAO",
    --       "lastModifiedDate": "2017-08-18T14:46:22.000Z",
    --       "recordTypeId": "012000000000000AAA",
    --       "recordTypeInfo": null,
    --       "systemModstamp": "2017-08-18T14:46:22.000Z",
    --       "weakEtag": 1503067582000
    --     }
    --   ]
    -- }

    -- Sample code for parsing the JSON response...
    -- Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

    DECLARE @apiName nvarchar(4000)

    DECLARE @eTag nvarchar(4000)

    DECLARE @DisplayValue nvarchar(4000)

    DECLARE @Value nvarchar(4000)

    DECLARE @IdDisplayValue nvarchar(4000)

    DECLARE @IdValue nvarchar(4000)

    DECLARE @NameDisplayValue nvarchar(4000)

    DECLARE @NameValue nvarchar(4000)

    DECLARE @SiteDisplayValue nvarchar(4000)

    DECLARE @SiteValue nvarchar(4000)

    DECLARE @id nvarchar(4000)

    DECLARE @lastModifiedById nvarchar(4000)

    DECLARE @lastModifiedDate nvarchar(4000)

    DECLARE @recordTypeId nvarchar(4000)

    DECLARE @recordTypeInfo nvarchar(4000)

    DECLARE @systemModstamp nvarchar(4000)

    DECLARE @weakEtag int

    DECLARE @count int
    EXEC sp_OAMethod @jResp, 'IntOf', @count OUT, 'count'
    DECLARE @currentPageToken nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @currentPageToken OUT, 'currentPageToken'
    DECLARE @currentPageUrl nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @currentPageUrl OUT, 'currentPageUrl'
    DECLARE @nextPageToken nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @nextPageToken OUT, 'nextPageToken'
    DECLARE @nextPageUrl nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @nextPageUrl OUT, 'nextPageUrl'
    DECLARE @previousPageToken nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @previousPageToken OUT, 'previousPageToken'
    DECLARE @previousPageUrl nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @previousPageUrl OUT, 'previousPageUrl'
    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'records'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @apiName OUT, 'records[i].apiName'
        EXEC sp_OAMethod @jResp, 'StringOf', @eTag OUT, 'records[i].eTag'
        EXEC sp_OAMethod @jResp, 'StringOf', @DisplayValue OUT, 'records[i].fields.DisambiguationField.displayValue'
        EXEC sp_OAMethod @jResp, 'StringOf', @Value OUT, 'records[i].fields.DisambiguationField.value'
        EXEC sp_OAMethod @jResp, 'StringOf', @IdDisplayValue OUT, 'records[i].fields.Id.displayValue'
        EXEC sp_OAMethod @jResp, 'StringOf', @IdValue OUT, 'records[i].fields.Id.value'
        EXEC sp_OAMethod @jResp, 'StringOf', @NameDisplayValue OUT, 'records[i].fields.Name.displayValue'
        EXEC sp_OAMethod @jResp, 'StringOf', @NameValue OUT, 'records[i].fields.Name.value'
        EXEC sp_OAMethod @jResp, 'StringOf', @SiteDisplayValue OUT, 'records[i].fields.Site.displayValue'
        EXEC sp_OAMethod @jResp, 'StringOf', @SiteValue OUT, 'records[i].fields.Site.value'
        EXEC sp_OAMethod @jResp, 'StringOf', @id OUT, 'records[i].id'
        EXEC sp_OAMethod @jResp, 'StringOf', @lastModifiedById OUT, 'records[i].lastModifiedById'
        EXEC sp_OAMethod @jResp, 'StringOf', @lastModifiedDate OUT, 'records[i].lastModifiedDate'
        EXEC sp_OAMethod @jResp, 'StringOf', @recordTypeId OUT, 'records[i].recordTypeId'
        EXEC sp_OAMethod @jResp, 'StringOf', @recordTypeInfo OUT, 'records[i].recordTypeInfo'
        EXEC sp_OAMethod @jResp, 'StringOf', @systemModstamp OUT, 'records[i].systemModstamp'
        EXEC sp_OAMethod @jResp, 'IntOf', @weakEtag OUT, 'records[i].weakEtag'
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jResp


END
GO

Curl Command

curl -X GET
	-H "Authorization: Bearer <access_token>"
https://domain.com/services/data/v{{version}}/ui-api/lookups/:SOBJECT_API_NAME/:FIELD_API_NAME/:TARGET_API_NAME

Postman Collection Item JSON

{
  "name": "Get Lookup Field Suggestions for a Specified Object",
  "event": [
    {
      "listen": "prerequest",
      "script": {
        "exec": [
          ""
        ],
        "type": "text/javascript"
      }
    }
  ],
  "protocolProfileBehavior": {
    "disableBodyPruning": true
  },
  "request": {
    "method": "GET",
    "header": [
    ],
    "body": {
      "mode": "raw",
      "raw": ""
    },
    "url": {
      "raw": "{{_endpoint}}/services/data/v{{version}}/ui-api/lookups/:SOBJECT_API_NAME/:FIELD_API_NAME/:TARGET_API_NAME",
      "host": [
        "{{_endpoint}}"
      ],
      "path": [
        "services",
        "data",
        "v{{version}}",
        "ui-api",
        "lookups",
        ":SOBJECT_API_NAME",
        ":FIELD_API_NAME",
        ":TARGET_API_NAME"
      ],
      "query": [
        {
          "key": "dependentFieldBindings",
          "value": "",
          "description": "The dependent field bindings for dependent lookups. These field bindings represent the lookup filter that restricts the valid values for the field.\nSpecify field bindings in a comma-separated list in the format dependentFieldBindings=<FIELD_API_NAME1>​=[FIELD_VALUE1],<FIELD_API_NAME2>=[FIELD_VALUE2].\n\nTo know whether a field is a dependent lookup, check the Object Info response body for a non-null filteredLookupInfo property. Specify the name and a value for each field in the controllingFields property. Get the field values from the Record response body. Both responses are returned from the /ui-api/record-ui/<RECORD_IDS> resource.",
          "disabled": true
        },
        {
          "key": "page",
          "value": "",
          "description": "The page number. The default value is 1.",
          "disabled": true
        },
        {
          "key": "pageSize",
          "value": "",
          "description": "The number of items per page. The default value is 25.\n",
          "disabled": true
        },
        {
          "key": "q",
          "value": "",
          "description": "The term the user is searching for.\nWhen searchType=Search, specify at least 2 characters. A wildcard at the end of the search term is implied. For example, q=ca returns Cat and Cats.\n\nWhen searchType=TypeAhead, specify at least 3 characters. A wildcard at the end of the search term is implied. You can’t use a ?.",
          "disabled": true
        },
        {
          "key": "searchType",
          "value": "",
          "description": "The type of search to perform. One of these values:\nRecent—Return most recently used matches.\nSearch—Search for records with searchable fields that match the query term.\nTypeAhead—Search for records whose names start with the query term.\nThe default value is Recent.",
          "disabled": true
        }
      ],
      "variable": [
        {
          "key": "SOBJECT_API_NAME",
          "value": "",
          "description": "The API name of a source object."
        },
        {
          "key": "FIELD_API_NAME",
          "value": "",
          "description": "The API name of a lookup field on the source object."
        },
        {
          "key": "TARGET_API_NAME",
          "value": "",
          "description": "The API name of the target (lookup) object."
        }
      ]
    },
    "description": "When a user edits a lookup field, use this resource to search for and display suggestions for a specified object. You can search for most recently used matches, for matching names, or for any match in a searchable field. You can also specify lookup filter bindings for dependent lookups."
  },
  "response": [
    {
      "name": "Get Lookup Field Suggestions for a Specified Object",
      "originalRequest": {
        "method": "GET",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": ""
        },
        "url": {
          "raw": "{{_endpoint}}/services/data/v{{version}}/ui-api/lookups/:SOBJECT_API_NAME/:FIELD_API_NAME/:TARGET_API_NAME",
          "host": [
            "{{_endpoint}}"
          ],
          "path": [
            "services",
            "data",
            "v{{version}}",
            "ui-api",
            "lookups",
            ":SOBJECT_API_NAME",
            ":FIELD_API_NAME",
            ":TARGET_API_NAME"
          ],
          "query": [
            {
              "key": "dependentFieldBindings",
              "value": "",
              "description": "The dependent field bindings for dependent lookups. These field bindings represent the lookup filter that restricts the valid values for the field.\nSpecify field bindings in a comma-separated list in the format dependentFieldBindings=<FIELD_API_NAME1>​=[FIELD_VALUE1],<FIELD_API_NAME2>=[FIELD_VALUE2].\n\nTo know whether a field is a dependent lookup, check the Object Info response body for a non-null filteredLookupInfo property. Specify the name and a value for each field in the controllingFields property. Get the field values from the Record response body. Both responses are returned from the /ui-api/record-ui/<RECORD_IDS> resource.",
              "disabled": true
            },
            {
              "key": "page",
              "value": "",
              "description": "The page number. The default value is 1.",
              "disabled": true
            },
            {
              "key": "pageSize",
              "value": "",
              "description": "The number of items per page. The default value is 25.\n",
              "disabled": true
            },
            {
              "key": "q",
              "value": "",
              "description": "The term the user is searching for.\nWhen searchType=Search, specify at least 2 characters. A wildcard at the end of the search term is implied. For example, q=ca returns Cat and Cats.\n\nWhen searchType=TypeAhead, specify at least 3 characters. A wildcard at the end of the search term is implied. You can’t use a ?.",
              "disabled": true
            },
            {
              "key": "searchType",
              "value": "",
              "description": "The type of search to perform. One of these values:\nRecent—Return most recently used matches.\nSearch—Search for records with searchable fields that match the query term.\nTypeAhead—Search for records whose names start with the query term.\nThe default value is Recent.",
              "disabled": true
            }
          ],
          "variable": [
            {
              "key": "SOBJECT_API_NAME",
              "value": "Contact",
              "description": "The API name of a source object."
            },
            {
              "key": "FIELD_API_NAME",
              "value": "AccountId",
              "description": "The API name of a lookup field on the source object."
            },
            {
              "key": "TARGET_API_NAME",
              "value": "Account",
              "description": "The API name of the target (lookup) object."
            }
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Date",
          "value": "Wed, 27 Sep 2023 16:10:07 GMT"
        },
        {
          "key": "Strict-Transport-Security",
          "value": "max-age=63072000; includeSubDomains"
        },
        {
          "key": "X-Content-Type-Options",
          "value": "nosniff"
        },
        {
          "key": "X-XSS-Protection",
          "value": "1; mode=block"
        },
        {
          "key": "X-Robots-Tag",
          "value": "none"
        },
        {
          "key": "Cache-Control",
          "value": "no-cache,must-revalidate,max-age=0,no-store,private"
        },
        {
          "key": "Content-Type",
          "value": "application/json;charset=UTF-8"
        },
        {
          "key": "Vary",
          "value": "Accept-Encoding"
        },
        {
          "key": "Content-Encoding",
          "value": "gzip"
        },
        {
          "key": "Transfer-Encoding",
          "value": "chunked"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"count\": 2,\n    \"currentPageToken\": null,\n    \"currentPageUrl\": \"/services/data/v58.0/ui-api/lookups/Contact/AccountId/Account?searchType=Recent&page=1&pageSize=25\",\n    \"nextPageToken\": null,\n    \"nextPageUrl\": null,\n    \"previousPageToken\": null,\n    \"previousPageUrl\": null,\n    \"records\": [\n        {\n            \"apiName\": \"Account\",\n            \"childRelationships\": {},\n            \"eTag\": \"2316c5f4a27ac31b407d91aab610b281\",\n            \"fields\": {\n                \"DisambiguationField\": {\n                    \"displayValue\": null,\n                    \"value\": null\n                },\n                \"Id\": {\n                    \"displayValue\": null,\n                    \"value\": \"001B000000UnQ2yIAF\"\n                },\n                \"Name\": {\n                    \"displayValue\": null,\n                    \"value\": \"Dickenson plc\"\n                },\n                \"Site\": {\n                    \"displayValue\": null,\n                    \"value\": null\n                }\n            },\n            \"id\": \"001B000000UnQ2yIAF\",\n            \"lastModifiedById\": \"005B0000003TOI6IAO\",\n            \"lastModifiedDate\": \"2017-08-18T14:46:22.000Z\",\n            \"recordTypeId\": \"012000000000000AAA\",\n            \"recordTypeInfo\": null,\n            \"systemModstamp\": \"2017-08-18T14:46:22.000Z\",\n            \"weakEtag\": 1503067582000\n        },\n        {\n            \"apiName\": \"Account\",\n            \"childRelationships\": {},\n            \"eTag\": \"cb5afb4494d9c9f6f81ab0e5f214585c\",\n            \"fields\": {\n                \"DisambiguationField\": {\n                    \"displayValue\": null,\n                    \"value\": null\n                },\n                \"Id\": {\n                    \"displayValue\": null,\n                    \"value\": \"001B000000UnQ2wIAF\"\n                },\n                \"Name\": {\n                    \"displayValue\": null,\n                    \"value\": \"Burlington Textiles Corp of America\"\n                },\n                \"Site\": {\n                    \"displayValue\": null,\n                    \"value\": null\n                }\n            },\n            \"id\": \"001B000000UnQ2wIAF\",\n            \"lastModifiedById\": \"005B0000003TOI6IAO\",\n            \"lastModifiedDate\": \"2017-08-18T14:46:22.000Z\",\n            \"recordTypeId\": \"012000000000000AAA\",\n            \"recordTypeInfo\": null,\n            \"systemModstamp\": \"2017-08-18T14:46:22.000Z\",\n            \"weakEtag\": 1503067582000\n        }\n    ]\n}"
    }
  ]
}