Play with XrmServiceToolkit Using Javascript

Create, Retrieve, Update & Delete (CRUD) operation using XrmServiceToolkit in MSCRM with Javascript.

Retrieve records using XrmServiceToolkit.

function retrievePolicyDetails(e) {
    var application = Xrm.Page.getAttribute("ccs_applicationtext").getValue();//A custom field using which we will retrieve policy entity details
    if (application != null) {
        //Policy is also a custom entity from where we are retrieving data.
        var fetch = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
            "<entity name='appointment'>" +
            "<attribute name='ccs_policynumber' />" +
            "<attribute name='ccs_policystatus' />" +
            "<attribute name='ccs_policyproduct' />" +
            "<attribute name='ccs_contact' />" +
            "<order attribute='subject' descending='false' />" +
            "<filter type='and'>" +
            "<condition attribute='activityid' operator='eq'  value = '" + application[0].id + "'/>" +
            "</filter>" +
            "</entity>" +
            "</fetch>";
        var ResultsRetrived = XrmServiceToolkit.Soap.Fetch(fetch); //XrmServiceToolkit.min.js file should be added to form resources with jquery.js and json.min.js
        //you can also use for loop if record count is more tha one
        if (ResultsRetrived.length > 0) {
            var polno = ResultsRetrived[0].attributes["ccs_policynumber"].value;
            Xrm.Page.getAttribute("ccs_policy").setValue([{ id: ResultsRetrived[0].attributes["ccs_policyid"].value, name: polno, entityType: "ccs_policy" }]);

            if (ResultsRetrived[0].attributes.ccs_policystatus != null) {
                var policyStatus = ResultsRetrived[0].attributes.ccs_policystatus.value;
                Xrm.Page.getAttribute("ccs_policystatustext").setValue(policyStatus);
            }
            if (ResultsRetrived[0].attributes.ccs_policyproduct != null) {
                var policyProductName = ResultsRetrived[0].attributes.ccs_policyproduct.name;
                var policyProductId = ResultsRetrived[0].attributes.ccs_policyproduct.id;
                Xrm.Page.getAttribute("ccs_policyproduct").setValue([{ id: policyProductId, name: policyProductName, entityType: "ccs_customproduct" }]);

            }
            if (ResultsRetrived[0].attributes.ccs_contact != null) {
                var policyContactName = ResultsRetrived[0].attributes.ccs_contact.name;
                var policyContactId = ResultsRetrived[0].attributes.ccs_contact.id;
                Xrm.Page.getAttribute("customerid").setValue([{ id: policyContactId, name: policyContactName, entityType: "contact" }]);

            }
        }
    }
}

 

Create records using XrmServiceToolkit

var entity = new XrmServiceToolkit.Soap.BusinessEntity("sud_nominee");
entity.attributes["fieldname"] = string; //for string fields
entity.attributes['fieldname'] = { id: id, logicalName: "entitylogicalname", type: 'EntityReference' };
entity.attributes["fieldname"] = { value: integerValue, type: 'OptionSetValue' };
entity.attributes["fieldName"] = { Value: 2, type: "int" };
entity.attributes["fieldName"] = { Value: 1.5617, type: "double" };
entity.attributes["fieldName"] = { Value: date }; &nbsp; //for date field
var createnominee = XrmServiceToolkit.Soap.Create(entity);

 

Update Records using  XrmServiceToolkit.

//update already created entity record by using logical name and guid
var entity = new XrmServiceToolkit.Soap.BusinessEntity("sud_nominee", entityID);
//Add value of string
entity.attributes["fieldName"] = "Value";
//Lookup to update
entity.attributes["fieldName"] = { id: entityGuid, logicalName: 'entityName', type: 'EntityReference' };
//OptionSet
entity.attributes["fieldName"] = { value: Value, type: 'OptionSetValue' };
//Currency
entity.attributes["fieldName"] = { value: Value, type: 'Money' };
//Boolean
entity.attributes["fieldName"] = { value: true / false, type: 'boolean' };

entity.attributes["fieldName"] = { Value: 2, type: "int" };

entity.attributes["fieldName"] = { Value: 1.5617, type: "double" };
var nomineeUpdated = XrmServiceToolkit.Soap.Update(entity);