UPDATE Execution
When an UPDATE statement is executed, the provider executes the MERGE method of the schema, where you can build the HTTP update request.
To see this method in a complete example, refer to Customizing Schemas.
Execute Updates to JSON
In the MERGE method, the _input item, one of the Items in API Script, contains the columns to update. For example, consider the following statement:
UPDATE Persons SET Name='Ana Trujilo' WHERE Id = '7'
You can use the _input item's attributes to set these column values in the HTTP update request. In the OData API, this can be an HTTP PUT or PATCH. The PUT data to make the preceding update in the OData API is below:
{
"@odata.type": "#ODataDemo.Person",
"@odata.id": "http://services.odata.org/V4/OData/(S(wcpddcnfsjih2ecg0izcegfs))/OData.svc/Persons(7)",
"Name": "Ana Trujilo"
}
In the example schema's corresponding MERGE method, the required column, the primary key, is validated and then used in the PUT data and the URL. You can check that an input was provided and alert the user otherwise with the api:validate keyword.
After validating that the needed attributes exist, you can set the column values in the PUT data. Set the data attribute to the PUT data -- the body of the api:set keyword is convenient for setting long or multiline values like this.
The api:call keyword invokes the operation that will make the HTTP request. Before invoking the operation, use api:set to set the method attribute to the HTTP method you want within the scope of the api:script keyword.
<api:script method="MERGE">
<api:set attr="method" value="PUT"/>
<api:validate attr="_input.Id" desc="An Id is required to update." />
<api:set attr="uri" value="http://services.odata.org/V4/OData/abc123/OData.svc/Persons(ID=[_input.Id])" />
<api:set attr="data">
{
"@odata.type": "#ODataDemo.Person",
"@odata.id": "[uri]",
<api:check attr="_input.Name">
"Name": "[_input.Name]"
</api:check>
}
</api:set>
<api:call op="jsonproviderGet"/>
</api:script>
Access the Components of UPDATE Statements
In the MERGE method, the _query item contains the following attributes:
query | The SQL statement, as in the following statement:
UPDATE Account SET Name='John' WHERE Id = @myId |
table | The table in the SQL statement. For example, Account in the preceding query. |
criteria | The WHERE clause of the statement. For example, the following WHERE clause in the example:
Id = @myId |
nullupdates | The columns of an UPDATE statement, separated by commas, that contain a null value or a null parameter value. |
Keyword Reference
See the API Script Reference for more information on the keywords used in this section: