Dynamics 365 V9. Web API sample to create a record

Sample Web API call to create a Lead record


Create A Record:
Let’s see how we can create a “Lead ”record using newly introduced Web API calls in Dynamics 365 V9
Xrm.WebApi.createRecord(“entityLogicalName”,data).then(successCallback,errorCallback);


function CreateLeadRecord(executionContext)
{
var entityData =
                {
                    "subject": "Created From Web API - D365 V9.0",
                    "lastname": "John"
                }
Xrm.WebApi.createRecord("lead", entityData).then(
                    function success(result)
                    {
                        alert("Record created successfully");
                    },
                    function (error)
                    {
                        console.log(error.message);       
                    });
}



Make sure, "Pass execution context as first parameter" is selected, as shown in the above screenshot. In the above success method, you can write your logic.

Note:

1. Please make sure that a reference of ClientGlobalContext.js.aspx is included. Which is placed at the root directory of web resources to able to use GetGlobalContext function.
2. In this example, we have taken the only subject and lastname attributes of a Lead entity as those are mandatory to create a record. Based on your requirement you can pass the required attributes and their values.

No comments:

Post a Comment