In previous posts I described how to use Xrm.WebApi methods to perform CRUD operations and use “execute” method to call Actions. This part is dedicated to functions.
Here is example of composing and using of WhoAmI unbound function:
var whoAmIRequest = { getMetadata: function () { var metadata = { boundParameter: null, parameterTypes: { }, operationName: "WhoAmI", operationType: 1 }; return metadata; } }; Xrm.WebApi.execute(whoAmIRequest) .then(function (result) { var response = JSON.parse(result.responseText); var BusinessUnitId = response.BusinessUnitId; var OrganizationId = response.OrganizationId; var UserId = response.UserId; //Handle retrieved data }) .fail(function (error) { var message = error.message; //Add handling of error that occurred });
As you can see difference between unbound actions and unbound function – value of “operationType” property that is equal to 1.
Here is example how to call “CalculateTotalTimeIncident” function that is incident-entity-bound function:
var calculateTotalTimeIncidentRequest = { entity: { id: "34669EC6-47B4-E711-A94F-000D3A109280", entityType: "incident" }, getMetadata: function () { var metadata = { boundParameter: "entity", parameterTypes: { "entity": { "typeName": "Microsoft.Dynamics.CRM.incident", "structuralProperty": 5 } }, operationName: "CalculateTotalTimeIncident", operationType: 1 }; return metadata; } }; Xrm.WebApi.execute(calculateTotalTimeIncidentRequest) .then(function (result) { var response = JSON.parse(result.responseText); var totalTime = response.TotalTime; //Handle retrieved data }) .fail(function (error) { var message = error.message; //Add handling of error that occurred });
Again, difference between requests for entity-bound action and entity-bound function – value for operationType that is equal to “1” for functions.
Here is example how to call “GetAllTimeZonesWithDisplayName” function that is timezonedefinition-entityset-bound function:
var getAllTimeZonesWithDisplayNameRequest = { LocaleId: 1033, getMetadata: function () { var metadata = { boundParameter: "entityset", parameterTypes: { entityset: { typeName: "mscrm.timezonedefinition", structuralProperty: 4 }, LocaleId: { typeName: "Edm.Int32", structuralProperty: 1 } }, operationName: "GetAllTimeZonesWithDisplayName", operationType: 1 }; return metadata; } }; Xrm.WebApi.execute(getAllTimeZonesWithDisplayNameRequest) .then(function (result) { var response = JSON.parse(result.responseText); //Handle retrieved data }) .fail(function (error) { var message = error.message; //Add handling of error that occurred });
Again, difference only in operationType property.
And the last – executeMultiple. Usage is really simple:
var request1 = { //create request N1 you want to run in batch }; var request2 = { //create request N2 you want to run in batch }; Xrm.WebApi.executeMultiple([request1, request2]) .then(function (result) { var response = JSON.parse(result.responseText); //Handle responses }) .fail(function (error) { var message = error.message; //Add handling of error that occurred });
Good stuff. Thanks for sharing.