Thursday, 8 August 2013

Formula filed functionality in MSCRM 2011 like Sales Force.

This needs Jquery  Library support


// This function is called in onchange of some  look up field.
function RetrieveAccountInfo() {

// Get the lookup filed id value
    customerAttribute = XP.data.entity.attributes.get("xxxid");

    if (customerAttribute != null) {

        customerValue = customerAttribute.getValue();
        if (customerValue != null && customerValue[0] != null) {
            customerType = customerValue[0].typename;
            customerId = customerValue[0].id;
            return retrieveRecord(customerId, "AccountSet", retrieveCompleted, null);
        }
    }
}

// Firing the Rest call to get the all the attributes of this look up record
function retrieveRecord(id, odataSetName, successCallback, errorCallback) {
    //var context = GetGlobalContext();

    var odataEndPoint = "/XRMServices/2011/OrganizationData.svc";
    if (!id) {
        alert("record id is required.");
        return;
    }
    if (!odataSetName) {
        alert("odataSetName is required.");
        return;
    }

    $.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: serverUrl + odataEndPoint + "/" + odataSetName + "(guid'" + id + "')",
        beforeSend: function (XMLHttpRequest) {

            XMLHttpRequest.setRequestHeader("Accept", "application/json");
        },
        success: function (data, textStatus, XmlHttpRequest) {
            if (successCallback) {
                successCallback(data.d, textStatus, XmlHttpRequest);
            }
        },
        error: function (XmlHttpRequest, textStatus, errorThrown) {
            if (errorCallback)
                errorCallback(XmlHttpRequest, textStatus, errorThrown);
            else
                errorHandler(XmlHttpRequest, textStatus, errorThrown);
        }
    });
}

// Call Back method to parse values from response
function retrieveCompleted(data, textStatus, XmlHttpRequest) {
    /*Display the required fields and hide if the fields are null */
    entity = data;
    if (!IsNull(entity.xxxxfield. value)) {

        if (entity.xxxxfield.LogicalName == "pricelevel") {

            var lookupValue = new Array();
            lookupValue[0] = new Object();
            lookupValue[0].id = entity.xxxxfield.Id;
            lookupValue[0].name = entity.xxxxfield.Name;
            lookupValue[0].entityType = entity.xxxxfield.LogicalName;
            XP.getAttribute("pricelevelid").setValue(lookupValue);


        }
        else {
            XP.getAttribute("xxxxfiledvalue").setValue(null);
        }
    }
    else {
        XP.getAttribute("xxxxfiledvalue").setValue(null);
    }
}