Кастомизация
12
Дек
0

Запуск Бизнес-процессов через JavaScript

С формы

JS код запуска Бизнес-процесса для текущей записи будет таким:

if (typeof (Sample) == "undefined") { Sample = {}; }

Sample.MyFunctions = {
    callWorkfow: function () {
        var WorkflowId = "27da4e99-579e-459b-9102-d8274dcd75ce"; // ID Бизнес-процесса 

        var requestMain = ""
        requestMain += "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
        requestMain += "  <s:Body>";
        requestMain += "    <Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
        requestMain += "      <request i:type=\"b:ExecuteWorkflowRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">";
        requestMain += "        <a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
        requestMain += "          <a:KeyValuePairOfstringanyType>";
        requestMain += "            <c:key>EntityId</c:key>";
        requestMain += "            <c:value i:type=\"d:guid\" xmlns:d=\"http://schemas.microsoft.com/2003/10/Serialization/\">" + Xrm.Page.data.entity.getId() + "</c:value>";
        requestMain += "          </a:KeyValuePairOfstringanyType>";
        requestMain += "          <a:KeyValuePairOfstringanyType>";
        requestMain += "            <c:key>WorkflowId</c:key>";
        requestMain += "            <c:value i:type=\"d:guid\" xmlns:d=\"http://schemas.microsoft.com/2003/10/Serialization/\">" + WorkflowId + "</c:value>";
        requestMain += "          </a:KeyValuePairOfstringanyType>";
        requestMain += "        </a:Parameters>";
        requestMain += "        <a:RequestId i:nil=\"true\" />";
        requestMain += "        <a:RequestName>ExecuteWorkflow</a:RequestName>";
        requestMain += "      </request>";
        requestMain += "    </Execute>";
        requestMain += "  </s:Body>";
        requestMain += "</s:Envelope>";

        var req = new XMLHttpRequest();
        req.open("POST", Xrm.Page.context.getServerUrl() + "/XRMServices/2011/Organization.svc/web", 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(requestMain);
        // var strResponse = req.responseXML.xml;
        // alert(strResponse.toString());

        alert("Бизнес-процесс запущен");
    }
};

В коде Вам нужнго только подставить ID своего Бизнес-процесса.

А если оформить его в виде Веб-ресурса new_callworkfow.js, запускать по кнопке можно будет так:

<RibbonDiffXml>
  <CustomActions>
    <CustomAction Id="BPCall.CustomAction"
                  Sequence="1"
                  Location="Mscrm.Form.account.MainTab.Workflow.Controls._children">
      <CommandUIDefinition>
        <Button Id="BPCall.Button"
                Command="BPCall.javascriptCommand"
                Sequence="1"
                LabelText="Запуск Бизнес-процесса"
                ToolTipTitle="Запуск Бизнес-процесса"
                ToolTipDescription="Запуск Бизнес-процесса"
                TemplateAlias="o1"
                Image32by32="$webresource:new_icon_bp32" />
      </CommandUIDefinition>
    </CustomAction>
  </CustomActions>
  <Templates>
    <RibbonTemplates Id="Mscrm.Templates"></RibbonTemplates>
  </Templates>
  <CommandDefinitions>
    <CommandDefinition Id="BPCall.javascriptCommand">
      <EnableRules>
        <EnableRule Id="Mscrm.Enabled"/>
      </EnableRules>
      <DisplayRules />
      <Actions>
        <JavaScriptFunction
            Library="$webresource:new_callworkfow.js"
            FunctionName="Sample.MyFunctions.callWorkfow">
        </JavaScriptFunction>
      </Actions>
    </CommandDefinition>
  </CommandDefinitions>
  <RuleDefinitions>
    <TabDisplayRules />
    <DisplayRules />
    <EnableRules />
  </RuleDefinitions>
  <LocLabels />
</RibbonDiffXml>



С Представления

А если Вам нужно запустить Бизнес-процесс (не забудьте поменять ID на свой) сразу для нескольких записей, то JS-код будет таким:

if (typeof (Sample) == "undefined") { Sample = {}; }

Sample.MyFunctions = {
    parceRecord: function (SelectedItem) {
        var allItems = new Array();
        var allItems = SelectedItem;

        if (allItems.length > 0) {
            for (var i = 0; i < allItems.length; i++)
                Sample.MyFunctions.callWorkfow(allItems[i].Id);
            alert("Бизнес-процессы запущены");
        } else
            alert("Выделите хотя бы одну запись");
    },
    callWorkfow: function (recordId) {
        var WorkflowId = "27da4e99-579e-459b-9102-d8274dcd75ce"; // ID Бизнес-процесса 

        var requestMain = ""
        requestMain += "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
        requestMain += "  <s:Body>";
        requestMain += "    <Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
        requestMain += "      <request i:type=\"b:ExecuteWorkflowRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">";
        requestMain += "        <a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
        requestMain += "          <a:KeyValuePairOfstringanyType>";
        requestMain += "            <c:key>EntityId</c:key>";
        requestMain += "            <c:value i:type=\"d:guid\" xmlns:d=\"http://schemas.microsoft.com/2003/10/Serialization/\">" + recordId + "</c:value>";
        requestMain += "          </a:KeyValuePairOfstringanyType>";
        requestMain += "          <a:KeyValuePairOfstringanyType>";
        requestMain += "            <c:key>WorkflowId</c:key>";
        requestMain += "            <c:value i:type=\"d:guid\" xmlns:d=\"http://schemas.microsoft.com/2003/10/Serialization/\">" + WorkflowId + "</c:value>";
        requestMain += "          </a:KeyValuePairOfstringanyType>";
        requestMain += "        </a:Parameters>";
        requestMain += "        <a:RequestId i:nil=\"true\" />";
        requestMain += "        <a:RequestName>ExecuteWorkflow</a:RequestName>";
        requestMain += "      </request>";
        requestMain += "    </Execute>";
        requestMain += "  </s:Body>";
        requestMain += "</s:Envelope>";

        var req = new XMLHttpRequest();
        req.open("POST", Xrm.Page.context.getServerUrl() + "/XRMServices/2011/Organization.svc/web", 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(requestMain);
        // var strResponse = req.responseXML.xml;
        // alert(strResponse.toString());
    }
};

Соответственно, кнопка для вызова Веб-ресурса new_callmanyworkfow.js, содержащего этот код будет такой:

<RibbonDiffXml>
  <CustomActions>
    <CustomAction Id="BPCall.CustomAction"
                  Sequence="1"
                  Location="Mscrm.HomepageGrid.account.MainTab.Workflow.Controls._children">
      <CommandUIDefinition>
        <Button Id="BPCall.Button"
                Command="BPCall.javascriptCommand"
                Sequence="1"
                LabelText="Запуск Бизнес-процесса"
                ToolTipTitle="Запуск Бизнес-процесса"
                ToolTipDescription="Запуск Бизнес-процесса"
                TemplateAlias="o1"
                Image32by32="$webresource:new_icon_bp32" />
      </CommandUIDefinition>
    </CustomAction>
  </CustomActions>
  <Templates>
    <RibbonTemplates Id="Mscrm.Templates"></RibbonTemplates>
  </Templates>
  <CommandDefinitions>
    <CommandDefinition Id="BPCall.javascriptCommand">
      <EnableRules>
        <EnableRule Id="Mscrm.Enabled"/>
      </EnableRules>
      <DisplayRules />
      <Actions>
        <JavaScriptFunction Library="$webresource:new_callManyWorkfow.js" FunctionName="Sample.MyFunctions.parceRecord">
          <CrmParameter Value="SelectedControlSelectedItemReferences" />
        </JavaScriptFunction>
      </Actions>
    </CommandDefinition>
  </CommandDefinitions>
  <RuleDefinitions>
    <TabDisplayRules />
    <DisplayRules />
    <EnableRules />
  </RuleDefinitions>
  <LocLabels />
</RibbonDiffXml>



Комментарии (0)

*

code