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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Cannot trigger plugin via action and custom javascript

(0) ShareShare
ReportReport
Posted on by 165

Hello,

I need to create Orders and Entitlements (based on the opportunity products data) when user clicks a custom button.

I have already created an action (without any parameters. Please let me know if I need to use parameters or not for this case).

5315.Capture.PNG

I have also created the custom button:

7317.Capture1.PNG

Custom button successfully reads the javascript code, I have tested it with an alert('hello world').

I have created a dummy plugin, just to see if it works with the button:

8664.Capture2.PNG

I've also updated the step like this:

6303.Capture3.PNG

I think that everything is OK until here.

Now, the javascript code to call the plugin which is not working...


function createordersbutton() {

debugger;
var entityId = Xrm.Page.data.entity.getId();
var entityName = "opportunityproduct";
var requestName = "zst_opportunityproductcreateordersandentitlements";

var xml = ""  
"Target" entityId "" entityName "" requestName ""  
"";

var xmlHttpRequest = new XMLHttpRequest();

xmlHttpRequest.open("POST", "cenacis.crm8.dynamics.com/.../web", true);
xmlHttpRequest.setRequestHeader("SOAPAction","schemas.microsoft.com/.../Execute");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Accept", "application/xml, text/xml, */*");
//xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);

var resultXml = xmlHttpRequest.responseText;
alert(resultXml);

}


The alert appears but it is empty. I also get 404 not found on console (network).

Any help would be greatly appreciated.



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

    Hello,

    One thing is that your plugin code contains conditions that do not let it jump inside the first statement.

    Target is EntityReference in your case and your code checks if it is Entity.

    Other thing - it seems that you're online and the same time you use SOAP endpoint. You should switch to WebApi. As an example you can check this post - butenko.pro/.../

    To make building of the request easier to avoid misstypes you can use CrmRestBuilder - github.com/.../2.6.0.0

    Good luck.

  • irenegr Profile Picture
    165 on at

    Thank you for your response, I managed to do it with WebApi and CrmRestBuild as you suggested.

  • irenegr Profile Picture
    165 on at

    It seems that I have another problem though.

    I tested it with a static ID (that I inserted manually in the code) to see if it works, and it was working fine.

    function createordersbutton() {
    
    debugger;
    //var entityId = Xrm.Page.data.entity.getId();
    
    var req = new XMLHttpRequest();
    req.open("POST", Xrm.Page.context.getClientUrl()   "/api/data/v9.1/opportunities(1234-5678-91011)/Microsoft.Dynamics.CRM.zst_OpportunityCreateOrdersandEntitlements", false);
    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.onreadystatechange = function() {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 204) {
                //Success - No Return Data - Do Something
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send();
    
    }


    But it doesn't work like this, specifically it gives the id like this: {8E103D74-3D1F-EB11-A813-000D3A4B2C9E}

    function createordersbutton() {
    
    debugger;
    var entityId = Xrm.Page.data.entity.getId();
    
    var req = new XMLHttpRequest();
    req.open("POST", Xrm.Page.context.getClientUrl()   "/api/data/v9.1/opportunities(" entityId ")/Microsoft.Dynamics.CRM.zst_OpportunityCreateOrdersandEntitlements", false);
    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.onreadystatechange = function() {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 204) {
                //Success - No Return Data - Do Something
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send();
    
    }

  • irenegr Profile Picture
    165 on at

    I mean like this:

    2211.Capture.PNG

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Actions don't work with curly brackets so in order to fix your issue replace line

    var entityId = Xrm.Page.data.entity.getId();

    with line

    var entityId = Xrm.Page.data.entity.getId().replace("{", "").replace("}", "");

  • irenegr Profile Picture
    165 on at

    Again, thank you so much.

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
ManoVerse Profile Picture

ManoVerse 81 Super User 2026 Season 1

#2
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 69 Most Valuable Professional

#3
11manish Profile Picture

11manish 62

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans