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 an subsequent insert we carry out. In this example I can now insert a note underneath the newly inserted project.
Request:
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:
- Sage CRM's RESTful API, An Introduction
- Architecture and Web Services
- Working with an instance of Sage CRM
- Extending query access to custom entities and views in SData
- Thinking about security
- SOAP CRUD vs REST CRUD
- Limitations of SData 1.1
- Compatibility as a priority
- What is JSON?
- SData 2.0 CRUD