Sage CRM's RESTful API: SData (Part 10 of 10)

2 minute read time.

In this article, I want to look at three different examples of how we can use the SData 2.0 based RESTful API within Sage CRM to carry out CRUD actions.

  • Post (Insert) a new record into a system entity
  • Post an Entry and link to an existing entity.
  • Post an Update to an Entry

Post (Insert) a new record into a Sage CRM custom entity

Request

  • http://localhost/sdata/crmj/sagecrm2/-/project

The request to insert the new record is directed at a custom entity. The HTTP method is POST and the raw payload on the request contains the following JSON object.

{
"proj_CompanyId": 28,
"proj_UserId": 1,
"proj_Name": "testy",
"proj_SecTerr": -2147483640,
"proj_ChannelId": 5,
"proj_OpportunityId": null,
"proj_PersonId": 30
}

The new project 'Texty' has been created. It is linked to a parent company & person.

It is important to realise that the response message of the successful insert will include the record ID of the newly created record.

Post an Entry and link to an existing custom entity.

We can use the record ID of the newly created record to link that to a subsequent insert we carry out. In this example, I can now insert a note underneath the newly inserted project.

Request:

  • http://localhost/sdata/crmj/sagecrm2/-/notes

The parent project had the ID of 4 in the example above. This is a POST request and contains the payload in JSON form.

{
"Note_Note": "Main focus: modem manufacturing and sales.",
"Note_ForeignTableId": 10240,
"Note_ForeignId": 4
}

This inserts the note underneath the project created in the first example.

The response will contain like the first insert the ID of the record which was just added.

Post an Update to an Entry

We can change any of the records that we have already entered. All of the details can be updated and we can identify the record to be changed using its ID. In this example I want to change the name of the Project to be "SData Training".

Request:

  • http://localhost/sdata/crmj/sagecrm2/-/project('4')

The newly created Project had the ID of '4' and the ID can be passed within the URL to retrieve the record that needs to be changed whereas the JSON payload indicates which fields are to be updated.

{
"proj_Name": "SData Training."
}



The articles in the series are:

  1. Sage CRM's RESTful API, An Introduction
  2. Architecture and Web Services
  3. Working with an instance of Sage CRM
  4. Extending query access to custom entities and views in SData
  5. Thinking about security
  6. SOAP CRUD vs REST CRUD
  7. Limitations of SData 1.1
  8. Compatibility as a priority
  9. What is JSON?
  10. SData 2.0 CRUD
Please note: For information about REST API that supports full create, update, read and delete behaviour please see: https://developer.sage.com/crm/

For articles about the REST API see: https://www.sagecity.com/sage-global-solutions/sage-crm/b/sage-crm-hints-tips-and-tricks/posts/the-rest-api-a-round-up-of-articles
  • Following on from this,

    I've managed to add Authentication to the AJAX post. But I've had to hardcode in the base64 encoded username and password of a user (system administrator in this case).

    e.g:

    headers: {

    'Authorization':'Basic xxxxxxyyyyyyyzzzzzzzzzz=='

    },

    This works OK in the scenario we are using it for. However, it'll obviously stop working in the event of a password change or changes to the security of the user being used for the authentication.

    I was hoping that there would be a way to piggyback an existing session in the same way that we can pass in the SID in the url of a GET call. Is this possible?

  • Hi Jeff,

    We're are doing some customisation for a client on SageCRM.com and we using a method similar to your example above to write back to the Person entity from the Opportunity screen.

    The code has been placed in Custom Content of the Opportunity Details screen.

    Writing back to the database all works as we'd expect, however, occasionally when the AJAX call is made, we are prompted for a user name and password to authenticate.

    Do we need to pass in authentication details in the header of the AJAX call?

    Gerry.