Sample code for 30+ languages & platforms
SQL Server

SugarCRM Getting a Record

See more SugarCRM Examples

Gets a record from the Sugar instance using the //:record endpoint. In this example we get an Account record by it's ID, but only request the Name, Email, and Industry fields.

Chilkat SQL Server Downloads

SQL Server
-- 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)
    DECLARE @success int
    SELECT @success = 0

    -- 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.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    -- Implements the following CURL command:

    -- curl -H OAuth-Token:<access_token> -H Cache-Control:no-cache http://<site_url>/rest/v10/Accounts/<record_id>?fields=name,email1,industry

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Cache-Control', 'no-cache'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'OAuth-Token', '<access_token>'

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

    EXEC sp_OAMethod @http, 'QuickGetSb', @success OUT, 'http://<site_url>/rest/v10/Accounts/<record_id>?fields=name,email1,industry', @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.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)

    -- {
    --   "id": "ab2222df-73da-0e92-6887-5705428f4d68",
    --   "name": "Test Record",
    --   "date_modified": "2016-04-06T15:03:21-04:00",
    --   "industry": "",
    --   "email1": "test@sugar.com",
    --   "_acl": {
    --     "fields": {}
    --   },
    --   "_module": "Accounts"
    -- }

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

    DECLARE @id nvarchar(4000)

    DECLARE @name nvarchar(4000)

    DECLARE @date_modified nvarchar(4000)

    DECLARE @industry nvarchar(4000)

    DECLARE @email1 nvarchar(4000)

    DECLARE @v_module nvarchar(4000)

    EXEC sp_OAMethod @jResp, 'StringOf', @id OUT, 'id'
    EXEC sp_OAMethod @jResp, 'StringOf', @name OUT, 'name'
    EXEC sp_OAMethod @jResp, 'StringOf', @date_modified OUT, 'date_modified'
    EXEC sp_OAMethod @jResp, 'StringOf', @industry OUT, 'industry'
    EXEC sp_OAMethod @jResp, 'StringOf', @email1 OUT, 'email1'
    EXEC sp_OAMethod @jResp, 'StringOf', @v_module OUT, '_module'

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


END
GO