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)

Calling global custom action using JavaScript

(0) ShareShare
ReportReport
Posted on by

I'm having trouble calling my global custom action using JavaScript in Dynamics CRM.

In CRM I created this global action that takes an in parameter and returns an out parameter.

global-action.PNG

I've confirmed that the action is activated and works. The problem arises when I try to call it using JavaScript.

My JavaScript code is as follows:

callCustomAction: function (actionName, actionParameters) {
    var result = null;

    var req = new XMLHttpRequest();
    req.open("POST", encodeURI(Xrm.Page.context.getClientUrl() + "/api/data/v8.2/" + actionName), false);
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.onreadystatechange = function () {
        if (this.readyState == 4) {
            req.onreadystatechange = null;

            if (this.status == 200) {
                result = JSON.parse(this.response);
                //alert(this.status);
            } else {
                var error = JSON.parse(this.response).error;
                alert(error.message);
                //alert(this.status);
            }
        };

        req.send(window.JSON.stringify(actionParameters));

        return result;
    }
}

Now, to me this looks correct but I got an issue with the URL which at the moment leads to nowhere. Everywhere I look the URL for a global action is simply the organization URL followed by "/api/data/v8.2/" and the name of my action but for me it's not working and I can't figure out why.

I'd appreciate any help I can get as I've been stuck for days :)

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Try to replace

    encodeURI(Xrm.Page.context.getClientUrl() + "/api/data/v8.2/" + actionName)

    with

    Xrm.Page.context.getClientUrl() + "/api/data/v8.2/" + actionName

  • Suggested answer
    Ivan Ficko Profile Picture
    1,380 on at

    Did you try CRM REST Builder (github.com/.../CRMRESTBuilder) to generate Web API JavaScript code?

    I use it every time when I need to execute some CRM REST requests in JavaScript where you can define your query in intuitive GUI which is really easy to use and generate JS code.

  • Verified answer
    Community Member Profile Picture
    on at

    I found the issue. The problem was that my action didn't appear under metadata. More information here: stackoverflow.com/.../calling-global-custom-action-using-javascript-in-dynamics-crm

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