gareth99
gareth99

Reputation: 41

Trigger plugin from CRM for multiple entities

I've seen solutions for executing a plugin from a ribbon button. For example here: https://www.greenbeacon.com/insights/trigger-plugin-from-ribbon-button-using-custom-actions-in-dynamics-crm-2013/. However what I want to do is enable the execution of a plugin that does not relate to one entity in particular. In the JavaScript in that solution it includes this:

requestXML += "            <b:value i:type="a:EntityReference">";
requestXML += "              <a:Id>" + Xrm.Page.data.entity.getId() + "</a:Id>";

However I don't want to limit it to one id. I want someone to be in an accounts view and click a button to perform a batch process, using the plugin, on all accounts (filtered within the C# code). Is there a way to do this?

Upvotes: 0

Views: 1003

Answers (2)

Meet
Meet

Reputation: 159

While registering the step for such plugin do not select Entity but Message, then while calling it from Javascript no need to pass any input parameter to plugin.

Upvotes: 0

Polshgiant
Polshgiant

Reputation: 3664

When you create your action, specify None for Entity. This makes the action global. Global actions don't need to be passed an entity reference. So the JS would then look like this:

function ExecuteAction(requestName, refreshPage, stringParameter) {
    // Creating the request XML for calling the Action
    var requestXML = ""

    requestXML += '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">';
    requestXML += '  <s:Body>';
    requestXML += '    <Execute xmlns="http://schemas.microsoft.com/xrm/2011/Contracts/Services" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">';
    requestXML += '      <request xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts">';
    requestXML += '        <a:Parameters xmlns:b="http://schemas.datacontract.org/2004/07/System.Collections.Generic">';
    requestXML += '          <a:KeyValuePairOfstringanyType>';
    requestXML += '            <b:key>Data</b:key>';
    requestXML += '            <b:value i:type="c:string" xmlns:c="http://www.w3.org/2001/XMLSchema">' + stringParameter + '</b:value>';
    requestXML += '          </a:KeyValuePairOfstringanyType>';
    requestXML += '        </a:Parameters>';
    requestXML += '        <a:RequestId i:nil="true " />';
    requestXML += '        <a:RequestName>' + requestName + '</a:RequestName>';
    requestXML += '      </request>';
    requestXML += '    </Execute>';
    requestXML += '  </s:Body>';
    requestXML += '</s:Envelope>';

    var req = new XMLHttpRequest();
    req.open("POST", GetServiceUrl(), false)
    req.setRequestHeader("Accept", "application/xml, text/xml, */*");
    req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
    req.send(requestXML);
    //refresh the page if the request was successful.
    if (req.status == 200) {
        if (refreshPage) {
            RefreshForm()
        }
    } else {
        Xrm.Utility.alertDialog(req.statusText + "n" + req.responseXML.getElementsByTagName("faultstring")[0].textContent);
    }
}

(Note that when I copied the code from the blog post I had to flip all of the quote characters and reformat the code. I may have missed something, so you'll want to double check the validity of the XML.)

Upvotes: 1

Related Questions