Jun 8, 2019

Client side operations using Xrm.WebApi

Xrm.WebApi provides properties and methods to use Dynamics 365 Customer Engagement Web API. Here I am presenting some code snippets.

function XrmWebApi_Create()
{
        var data = {  
        "su_name": "Test2 Office",
        "su_ContactId@odata.bind": "/contacts(dba3e5b9-88df-e311-b8e5-6c3be5a8b200)",
        "su_type": 100000001
        }  
 
        Xrm.WebApi.createRecord("su_office", data).then(  
        function success(result) {  
                 alert("Success. Id: " + result.id);  
        },  
        function(error) {  
                alert("Error: " + error.message);  
        }  
        );
}

function XrmWebApi_Update()
{
        var data = {
        "su_name": "Test2 Office NEW",
        "su_type": 100000002
        }

        Xrm.WebApi.updateRecord("su_office", "c1730963-b087-e911-a992-00224800cc7a", data).then(
        function success(result) {
                alert("Updated");
        },
        function (error) {
                console.log(error.message);
        }
        );
}


function XrmWebApi_Retrive()
{
        parent.Xrm.WebApi.retrieveRecord("su_office", "c1730963-b087-e911-a992-00224800cc7a", "?$select=su_name,su_type").then(
        function success(result) {
                alert("Retrieved values: Name:" + result.su_name + " Type:" + result.su_type);
        },
        function (error) {
                alert(error.message);
        }
        );
}

function XrmWebApi_RetriveMultiple()
{
        Xrm.WebApi.retrieveMultipleRecords("su_office", "?$select=su_name&$filter=su_type eq 100000002").then(
        function success(result) {
                for (var i = 0; i < result.entities.length; i++) {
                     alert(result.entities[i].su_name);
                     break; 
                }
        },
        function(error) {  
                        alert("Error: " + error.message);  
        }  
        );
}

function XrmWebApi_Delete()
{
        Xrm.WebApi.deleteRecord("su_office", "fca4b45e-4087-e911-a992-00224800cc7a").then(
        function success(result) {
                alert("Deleted");
        },
        function (error) {
                alert(error.message);
        }
        );
}

Sometimes it can be quite difficult to deal with PartyLists (in Activities). So I am giving a code snippet for that. This example is for creating a Phone Call. Please note how below fields are being set;

a. Regarding Object
b. ParttList

function XrmWebApi_Create()
{
        var contact = "/contacts(dba3e5b9-88df-e311-b8e5-6c3be5a8b200)";
        var user = "/systemusers(63265A2B-F31C-4E0E-951D-13974E3CCB4E)";
        var activityParties = new Array();
        var partyFrom = {"partyid_contact@odata.bind" : contact, "participationtypemask" : 1};
        var partyTo = {"partyid_systemuser@odata.bind" : user, "participationtypemask" : 2};
        activityParties[0] = partyFrom;
        activityParties[1] = partyTo;

        var data = {  
        "subject": "0003",        
        "phonenumber": "789999",
        "description": "test",
        "regardingobjectid_account@odata.bind": "/accounts(aaa19cdd-88df-e311-b8e5-6c3be5a8b200)",
        "phonecall_activity_parties":activityParties
        }  
 
        Xrm.WebApi.createRecord("phonecall", data).then(  
        function success(result) {  
                 alert("Success. Id: " + result.id);  
        },  
        function(error) {  
                alert("Error: " + error.message);  
        }  
        );
}

Please refer this on passing FetchXml to WebApi.

Enjoy!