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)

Call action that trigger a plugin from js

(0) ShareShare
ReportReport
Posted on by 75

 have a scenario where I need to trigger a plugin on a ribbon button click (SYNCH), I have setup the commands and buttons, I have created a custom action,that will be registered in the plugin reg tool to wire up the event. I have tested the ribbon button that just does a simple hello world, so I am sure that the basics are dine and set. My issue lies when I try to call the function that does a SOAP invocation. Here is my code below. 

function CallAction() {
var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/products/Microsoft.Dynamics.CRM.new_ProductPr", 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();

}

2318.s.PNG
}

*This post is locked for comments

I have the same question (0)
  • Jf Marwen Profile Picture
    75 on at

    Also i used this code

    function CallAction() {

    debugger;

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

    var entityName = "Product";

    var requestName = "new_ProductPr";

    ExecuteActionCreateProject(entityId, entityName, requestName);

    }

    function ExecuteActionCreateProject(EntityId,entityId, entityName, requestName) {

    // Creating the request XML for calling the Action

    var requestXML = ""

    requestXML += "<s:Envelope xmlns:s=\"schemas.xmlsoap.org/.../envelope\">";

    requestXML += " <s:Body>";

    requestXML += " <Execute xmlns=\"schemas.microsoft.com/.../Services\" xmlns:i=\"www.w3.org/.../XMLSchema-instance\">";

    requestXML += " <request xmlns:a=\"schemas.microsoft.com/.../Contracts\">";

    requestXML += " <a:Parameters xmlns:b=\"schemas.datacontract.org…/System.Collections.Generic\">";

    requestXML += " <a:KeyValuePairOfstringanyType>";

    requestXML += " <b:key>Target</b:key>";

    requestXML += " <b:value i:type=\"a:EntityReference\">";

    requestXML += " <a:Id>"+entityId+"</a:Id>";

    requestXML += " <a:LogicalName>"+entityName+"</a:LogicalName>";

    requestXML += " <a:Name i:nil=\"true\">";

    requestXML += " </a:Name></b:value>";

    requestXML += " </a:KeyValuePairOfstringanyType>";

    requestXML += " <a:KeyValuePairOfstringanyType>";

    requestXML += " <b:key>EntityId</b:key>";

    requestXML += " <b:value i:type=\"c:string\" xmlns:c=\"www.w3.org/.../XMLSchema\">"+EntityId+"</b:value>";

    requestXML += " </a:KeyValuePairOfstringanyType>";

    requestXML += " </a:Parameters>";

    requestXML += " <a:RequestId i:nil=\"true\">";

    requestXML += " </a:RequestId>";

    requestXML += " <a:RequestName>"+requestName+"</a:RequestName>";

    requestXML += " </request>";

    requestXML += " </Execute>";

    requestXML += " </s:Body>";

    requestXML += "</s:Envelope>";

    var req = new XMLHttpRequest();

    req.open("POST", GetClientUrl(), false)

    req.setRequestHeader("Accept", "application/xml, text/xml, */*");

    req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

    req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/…/Services/IOrganizati…/Execute");

    req.send(requestXML);

    //Get the Response from the CRM Execute method

    //var response = req.responseXML.xml;

    req.onerror = function (e) {

    alert(req.statusText);

    };

    if (req.status === 200) {

    alert(req.responseText);

    }

    }

    function GetClientUrl() {

    if (typeof Xrm.Page.context == "object") {

    clientUrl = Xrm.Page.context.getClientUrl();

    }

    var ServicePath = "/XRMServices/2011/Organization.svc/web";

    return clientUrl + ServicePath;

    }

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

    Hello,

    I would recommend to master basics of troubleshooting to see what is going wrong. I wrote a video that could be helpful for you:

  • Jf Marwen Profile Picture
    75 on at

    I need javascript code please

  • Suggested answer
    Pawar Pravin  Profile Picture
    5,237 on at

    Hi Jf Marwen,

    Please do refer following code to call action that will trigger plugin from js. It's working for me.

    //Call main function on ribbon button

    Synch = function()

    {

    var url;

    url = "actionlogicalName"; //Your Action Name

    var parameters = {};

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

    if (CurrentRecordId != null)

    parameters.RecordId = CurrentRecordId.replace("{", "").replace("}", "");

    callCustomAction(url, SynchProductFunction, parameters);

    }

    //Get Output

    SynchProductFunction = function(result)

    {

       var ResultData = result.OutPutParameterIfRequired;

       alert(ResultData);

    }

    //Call action

    callCustomAction = function (uri, successCallback, data) {

       debugger;

       if (!typeof uri === "string") {

           console.log("sdkRequest: uri parameter must be a string.");

       }

       if (data) {

           data = JSON.stringify(data);

       }

       var req = new XMLHttpRequest();

       req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/" + uri, 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) {

                   var result = JSON.parse(this.response)

                   successCallback(result);

               }

               else {

                   var errorText = this.responseText;

                   Xrm.Utility.alertDialog(errorText);

               }

           }

       };

       req.send(data);

    }

    Note: One more thing I have noticed that we need to create action in internet explorer browser only.

  • Suggested answer
    CRASH Profile Picture
    2 on at
    var new_TestActionRequest = {
    
       getMetadata: function() {
    
           return {
    
               boundParameter: null,
    
               parameterTypes: {},
    
               operationType: 0,
    
               operationName: "new_TestAction"
    
           };
    
       }
    
    };
    
    Xrm.WebApi.online.execute(new_TestActionRequest).then(
    
       function success(result) {
    
           if (result.ok) {
    
               var results = JSON.parse(result.responseText);
    
           }
    
       },
    
       function(error) {
    
           Xrm.Utility.alertDialog(error.message);
    
       }
    
    );


    Which CRM version are you working on, if its D365, try using this code to invoke action, or create this code for your own action using CRM Rest Builder([View:https://github.com/jlattimer/CRMRESTBuilder/releases:750:50])

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi,

    There are 3 pieces to this implementation-

    1) If the javascript code is getting called from the ribbon button

    2) If the code to call the action in JavaScript is able to call the action successfully

    3) If the plugin is getting triggered when you execute the custom action

    You have verified the 1st piece, Use Rest Builder tools and call your custom action from there and see if the plugin is getting triggered or not.

    carldesouza.com/dynamics-crm-rest-builder

    You can download the tool here if not already- github.com/.../releases

    Hope this helps.

  • Suggested answer
    chauhanhardik Profile Picture
    687 on at

    Hi Jf Marwen,

    The code given by Pravin Pawar will trigger the action to be called on click of ribbon button, then register the plugin in Plugin registration tool and call it using the action name. This will fire plugin on click of ribbon button.

    Regards,

    Hardik Chauhan

  • Jf Marwen Profile Picture
    75 on at

    Vaibhaw Wadhwa , when i used your code i got this error www.PNG

  • CRASH Profile Picture
    2 on at

    Have you copy pasted exact same code?

    If not please share the code you have pasted, i have shared a link with my reply as well, you can find a managed solution on that link CRMRESTBUILDER.zip(something along that line), import it on your CRM instance, go to settings-->customization, you will see a button for CRM rest builder near ribbon, click on that to open solution, and create a code to run your action from there, most of the time, code created from that tool will just run fine, without editing.

  • Jf Marwen Profile Picture
    75 on at

    This is the code from CRMRESTBUILDER 

    var parameters = {};
    var entity = {};
    entity.id = "";
    entity.entityType = "product";
    parameters.entity = entity;

    var new_ProductPrRequest = {
    entity: parameters.entity,

    getMetadata: function() {
    return {
    boundParameter: "entity",
    parameterTypes: {
    "entity": {
    "typeName": "mscrm.product",
    "structuralProperty": 5
    }
    },
    operationType: 0,
    operationName: "new_ProductPr"
    };
    }
    };

    Xrm.WebApi.online.execute(new_ProductPrRequest).then(
    function success(result) {
    if (result.ok) {
    //Success - No Return Data - Do Something
    }
    },
    function(error) {
    Xrm.Utility.alertDialog(error.message);
    }
    );

    not working

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