Some CRM 2011 JavaScript

//Hide Section in

Xrm.Page.ui.tabs.get(“Name of the tab”).sections.get(“Name of the section”).setVisible(true);

Xrm.Page.ui.tabs.get(“Name of the tab”).sections.get(“Name of the section”).setVisible(False);

//Force Submit method has changed to

Xrm.Page.getAttribute(“Field Name”).setSubmitMode(“always”);

//Get Selected Picklist value

var _Value = Xrm.Page.getAttribute(“Name of Picklist”).getSelectedOption().text;

//Get Entity Attributes based on ID

var context;

var serverUrl;

var ODataPath;

function Getinfo() {

    context = Xrm.Page.context;

    serverUrl = context.getServerUrl();

    ODataPath = serverUrl + “/XRMServices/2011/OrganizationData.svc”;

}

function GetID() {

    var ID = Xrm.Page.getAttribute(“Nameof ID field”).getValue()[0].id;

    RetrieveAccountRecord(ID);

}

function RetrieveAccountRecord(Id) {

    var RetrieveAccountReq = new XMLHttpRequest();

    RetrieveAccountReq.open(“GET”, ODataPath + “/AccountSet(guid'” + Id + “‘)”, true); //I am fetch account data

    RetrieveAccountReq.setRequestHeader(“Accept”, “application/json”);

    RetrieveAccountReq.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);

    RetrieveAccountReq.onreadystatechange = function() {

    RetrieveAccountReqCallBack(this);

    };

    RetrieveAccountReq.send();

}

function RetrieveAccountReqCallBack(RetrieveAccountReq) {

    if (RetrieveAccountReq.readyState == 4 /* complete */) {

        if (RetrieveAccountReq.status == 200) {

            var RetrievedAccount = this.parent.JSON.parse(RetrieveAccountReq.responseText).d;

            if(RetrievedAccount.Telephone1!=null)

                Xrm.Page.getAttribute(“TargetTelephonefield”).setValue(RetrievedAccount.Telephone1);

            if(RetrievedAccount.Emailaddress1!=null)

                Xrm.Page.getAttribute(“TargetEmailfield”).setValue(RetrievedAccount.Emailaddress1);

                      }

        else {

            errorHandler(RetrievedAccount);

            alert(“Error in Fetch Account data”);

        }    }}

 Reference : Microsoft Dynamics CRM 2011 SDK

Enjoy !!!

6 thoughts on “Some CRM 2011 JavaScript

  1. j0ngleur

    very nice!

    just some small errors: “retrievedAccount” is sometimes beginning with lower case but defined as “RetrievedAccount” (the same goes for “retrieveAccountReq”)

    Reply
  2. Derrick

    Greetings! Very helpful advice in this particular article! It’s the little changes that produce the largest changes. Many thanks for sharing!

    Reply
  3. rahul

    I am using this code but its not working for me.Please check and let me know if something wrong happning with me.

    function onLoad() {
    var mostRecentQuery = “/xrmservices/2011/OrganizationData.svc/Lead?$select=FirstName&$top=1”;
    getContact(mostRecentQuery);
    }

    function getContact(oDataQuery) {
    try {
    var serverUrl;
    /* This is the heart and soul of the whole thing – the actual oData call. */
    serverUrl = Xrm.Page.context.getClientUrl() + oDataQuery;
    //alert(serverUrl);
    var request = new XMLHttpRequest();
    request.open(“GET”, encodeURI(serverUrl), true);
    request.setRequestHeader(“Accept”, “application/json”);
    request.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);
    /* Next, we specify the function that we want called when the XMLHttpRequest state
    changes. This will get called on each state change. */
    request.onreadystatechange = function () {
    if (this.readyState == 4 && this.status == 200) {

    var json = $.parseJSON(this.responseText);
    // if ((json != undefined) && (json.d != undefined) && (json.d.results != undefined) && (json.d.results[0] != null)) {
    // json = json.d.results[0];
    alert(“test fullname”);
    //alert(json.FullName);

    // }
    }
    }
    request.send();
    } catch (e) {
    // You probably want to do something other than an alert here.
    alert(“exeption”);
    alert(e.Description);
    }
    }

    Debugger is not entring in the function request.onreadystatechange .Please check.

    Thanks
    Rahul

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *