Xrm.WebApi cheat sheet: create, retrieve, update and delete
This is the cheat sheet I keep around for the four basic operations with Xrm.WebApi from a form, using async/await:
// Create: returns { entityType, id }
const { id } = await Xrm.WebApi.createRecord("account", {
name: "Axazure",
revenue: 5000000,
"primarycontactid@odata.bind": "/contacts(00000000-0000-0000-0000-000000000000)",
});
// Retrieve a record
const account = await Xrm.WebApi.retrieveRecord("account", id, "?$select=name,revenue");
// Update (note: the id is the second parameter)
await Xrm.WebApi.updateRecord("account", id, { revenue: 6000000 });
// Delete
await Xrm.WebApi.deleteRecord("account", id);Lookups are set with field@odata.bind and the path /<entity-set-name>(<guid>), in plural (/contacts(...)). That field name is the navigation property, which for custom columns usually matches the schema name, capitals included.
Always use $select when reading. Without it you get every column back and the call is noticeably slower.
The mistake I see most is with updateRecord, when someone forgets the id and passes the data as the second parameter. It fails, and the error message doesn’t make it obvious why.