web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Asynchronous call on an enable rule to show\hide a ribbon button

(0) ShareShare
ReportReport
Posted on by 205

Hello All,

I am stuck with a situation. I am trying to make a button visible on the flyout menu on the form ribbon based on an enable rule. The enable rule has a javascript, which performs an xmlhttprequest to get a related record, if there are no realted records i want to show the button and if there are then i want to hide it.

When i debugged the javascript it seems to be working but the button's visibility is not changing based on the return value from the javascript.

i am doing this on CRM 365 On-Premise

Here is my code:

function checkRelatedRecords()
{
debugger;
var userId = Xrm.Page.context.getUserId();
userId=userId.replace("{","").replace("}","");
var recId = Xrm.Page.data.entity.getId();
recId=recId.replace("{","").replace("}","");
var recType = Xrm.Page.data.entity.getEntityName();
var filterExpression="_new_followedby_value eq " +userId + " and _new_following" + recType + "_value eq " + recId;
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/new_followemails?$filter="+filterExpression, true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function() {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 200) {
            var results = JSON.parse(this.response);
			if(results.value.length > 0)
			{
				return false;
			}
			else
			{
				return true;
			}

        }else {
            Xrm.Utility.alertDialog(this.statusText);
        }
    }
};
req.send();
}


*This post is locked for comments

I have the same question (0)
  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi Suresh ,

    Hope below old thread might be useful for you-

    community.dynamics.com/.../283305

  • Community Member Profile Picture
    on at

    Hi Suresh,

    I guess you are making an asynchronous call and expecting the call to give you data instantly,

    Try change it to make synchronous call

    change "true" to "false" in the code below

    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/new_followemails?$filter="+filterExpression, true);

  • Community Member Profile Picture
    on at

    Hi Suresh,

    As Vijay Bonam says you should make the call synchronously.

    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/new_followemails?$filter="+filterExpression, true); Just change true to false.

    Create a variable at the beginning of the function.

    var isRelatedRecordExist = false;

    Then,

    if(results.value.length > 0)

    {

    isRelatedRecordExist = false;

    }

    else

    {

    isRelatedRecordExist = true;

    }

    After req.send(); return the value.

    req.send();

    return isRelatedRecordExist ;

    }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans