Monday 11 March 2013


Update Record In mscrm 2011 form form javascript:


Soap:



function updateAccount() {
    var FORM_TYPE_UPDATE = 2;
    var formType = Xrm.Page.ui.getFormType();
    if (formType == FORM_TYPE_UPDATE) {
        // Prepare variables for updating a contact.
        var accountId = Xrm.Page.data.entity.getId();
        var parentAccount = Xrm.Page.getAttribute('parentaccountid');
        var parentAccountId;

        if (parentAccount != null) {

            var lookUpObjectValue = parentAccount.getValue();

            if ((lookUpObjectValue != null)) {

                parentAccountId = lookUpObjectValue[0].id;

            }
            var authenticationHeader = GenerateAuthenticationHeader();

            // Prepare the SOAP message.
            var xml = "<?xml version='1.0' encoding='utf-8'?>" +
"<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'" +
" xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'" +
" xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
authenticationHeader +
"<soap:Body>" +
"<Update xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
"<entity xsi:type='account'>" +
"<parentaccountid>" + parentAccountId + "</parentaccountid>" +
"<accountid>" + accountId + "</accountid>" +
"</entity>" +
"</Update>" +
"</soap:Body>" +
"</soap:Envelope>";
            // Prepare the xmlHttpObject and send the request.
            var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
            xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
            xHReq.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Update");
            xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
            xHReq.setRequestHeader("Content-Length", xml.length);
            xHReq.send(xml);
        }
    }
}




Rest:

function updateContactRest() {

    var FORM_TYPE_UPDATE = 2;
    var formType = Xrm.Page.ui.getFormType();
    if (formType == FORM_TYPE_UPDATE) {
        // Prepare variables for updating a contact.
        var accountId = Xrm.Page.data.entity.getId();
        var parentAccount = Xrm.Page.getAttribute('parentaccountid');
        var parentAccountId;

        if (parentAccount != null) {

            var lookUpObjectValue = parentAccount.getValue();

            if ((lookUpObjectValue != null)) {

                parentAccountId = lookUpObjectValue[0].id;


            }
            // Gets the record Guid
            var id = Xrm.Page.data.entity.getId();
            var changes = {
                // Text field

                // Lookup field
                ParentAccountId: {
                    Id: parentAccountId, // Guid of the parent account
                    LogicalName: "account"
                }
            };

            //updateRecord exists in JQueryRESTDataOperationFunctions.js
            updateRecord(id, changes, "AccountSet", updateAccountCompleted, null);
        }
    }
}


function updateRecord(id, entityObject, odataSetName, successCallback, errorCallback) {
    var context = Xrm.Page.context;
    var serverUrl = context.getServerUrl();

    //The XRM OData end-point
    var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";

    //id is required
    if (!id) {
        alert("record id is required.");
        return;
    }
    //odataSetName is required, i.e. "AccountSet"
    if (!odataSetName) {
        alert("odataSetName is required.");
        return;
    }

    //Parse the entity object into JSON
    var jsonEntity = window.JSON.stringify(entityObject);

    //Asynchronous AJAX function to Update a CRM record using OData
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        data: jsonEntity,
        url: serverUrl + ODATA_ENDPOINT + "/" + odataSetName + "(guid'" + id + "')",
        beforeSend: function (XMLHttpRequest) {
            //Specifying this header ensures that the results will be returned as JSON.          
            XMLHttpRequest.setRequestHeader("Accept", "application/json");

            //Specify the HTTP method MERGE to update just the changes you are submitting.          
            XMLHttpRequest.setRequestHeader("X-HTTP-Method", "MERGE");
        },
        success: function (data, textStatus, XmlHttpRequest) {
            //The MERGE does not return any data at all, so we'll add the id
            //onto the data object so it can be leveraged in a Callback. When data
            //is used in the callback function, the field will be named generically, "id"
            data = new Object();
            data.id = id;
            if (successCallback) {
                successCallback(data, textStatus, XmlHttpRequest);
            }
        },
        error: function (XmlHttpRequest, textStatus, errorThrown) {
            if (errorCallback)
                errorCallback(XmlHttpRequest, textStatus, errorThrown);
            else
                errorHandler(XmlHttpRequest, textStatus, errorThrown);
        }
    });
}


function errorHandler(xmlHttpRequest, textStatus, errorThrow) {
    alert("Error : " + textStatus + ": " + xmlHttpRequest.statusText);
}

//Called upon successful Account update.
function updateAccountCompleted(data, textStatus, XmlHttpRequest) {
    //Get back the Account JSON object
    var account = data;
    // alert("Account updated: id = " + account.id);
}

















No comments:

Post a Comment