Home > Client Scripting, CRM 2011, Ribbon > Walkthrough of Asynchronous call from <CustomRule> (RibbonDiff)

Walkthrough of Asynchronous call from <CustomRule> (RibbonDiff)


Walkthrough of Asynchronous call from <CustomRule>(RibbonDiff)

In following walkthrough I am demonstrating how to use EnableRule in Ribbon to call JavaScript function which will do Asynchronous call to CRM using REST and how to set Enable and Disable Ribbon Button

Assuming that Ribbon Customization is done and CustomRule is calling JavaScript function on EnableRule.

Following is how JavaScript. Here I am checking if logged in User is associated with current Entity record using Connections.

///<reference path=”XrmPage-vsdoc.js”/>
//Global Parameter
var isPartnerTeamMember = false;
var PageUI; 

function GetServerUrl() {

    var customServerURL = document.location.protocol + “//” + document.location.host + “/” + Xrm.Page.context.getOrgUniqueName();

    return customServerURL;

}

 

function CheckUserAccessOnPartner(PrimaryEntityTypeName, partnerObjectTypeCode, SelectedEntityTypeName, partnerId) {

    if (isPartnerTeamMember) {

        return true;

    }

    else {

        PageUI = window.top.opener.parent.Xrm.Page.ui;

        var context = Xrm.Page.context;

        var serverUrl = GetServerUrl();

        var userId = context.getUserId();

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

        var retrieveReq = new XMLHttpRequest();

        retrieveReq.open(“GET”, ODataPath + “/ConnectionSet?$select=ConnectionId,Record1Id,Record1ObjectTypeCode,Record2Id,Record2ObjectTypeCode&$filter=Record1ObjectTypeCode/Value eq ” + partnerObjectTypeCode + ” and Record2ObjectTypeCode/Value eq 8 and Record1Id/Id eq guid’” + partnerId + “‘”, true);

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

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

        retrieveReq.onreadystatechange = function () {

            retrievePartnerConnectionsCallBack(this);

        };
        retrieveReq.send();
        return false;
    }
}

function retrievePartnerConnectionsCallBack(retrieveReq) {

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

        if (retrieveReq.status == 200) {
            //Success
            var responseData = JSON.parse(retrieveReq.responseText).d;

            if (responseData != null && responseData.results != null && responseData.results.length > 0) {

                for (i = 0; i < responseData.results.length; i++) {

                    var record2Id = “{” + responseData.results[0].Record2Id.Id + “}”;

                    if (record2Id.toLowerCase() == Xrm.Page.context.getUserId().toLowerCase()) {

                        isPartnerTeamMember = true;

                        if (PageUI != null) {
                            PageUI.refreshRibbon();
                        }
                        break;
                    }
                }
            }
        }

        else {
            //errorHandler(retrieveReq);
        }
    }
}

 Key Point to note in above Script

  1. I am using Global variable to defined return value (isPartnerTeamMember) which is by default set to false.
  2. As first step I am checking isPartnerTeamMember and returning if it is true else calling REST query.
  3. In REST callback I am calling Xrm.Page.ui.refreshRibbon() method, which will reevaluate function and this time it will find isPartnerTeamMember as true and it will be returned from there.
  4. One of the important thing to note here is if you have having Ribbon button on SubGrid or associated view Xrm.Page.ui will be null for this you have to use window.top.opener.parent.Xrm.Page.ui (Thanks Daniel Cai !! for figuring out this)

Hope this Helps

About these ads
  1. July 29, 2011 at 3:08 pm | #1

    Thanks, Makarand! I was able to adapt this to a requirement that we have for a client and it works great.

  2. October 27, 2011 at 11:27 am | #3

    Thanks Jacob for tagging my Blog link in you Blog.

  1. September 9, 2011 at 11:31 am | #1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: