1

我已经检查了论坛,似乎无法得到我需要工作的东西。

在潜在客户实体上,当用户创建新潜在客户时,工作流会在之前运行以更改某些潜在客户字段。

我发现这只能通过实体设置中的 javascript 和 onload 实现。

我已经创建了工作流和一个 JavaScript 文件并将其设置为在加载时运行。我使用的Javascript是:

function callworkfow(workflowId, entityId) {
var request =
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<Execute xmlns="http://schemas.microsoft.com/xrm/2011/Contracts/Services"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<request i:type="b:ExecuteWorkflowRequest" xmlns:a="http://schemas.microsoft.com/xrm/2011      /Contracts" xmlns:b="http://schemas.microsoft.com/crm/2011/Contracts">
 <a:Parameters xmlns:c="http://schemas.datacontract.org/2004/07/System.Collections.Generic">
 <a:KeyValuePairOfstringanyType>
  <c:key>B3D77337-D5FD-E211-BB05-005056AF0003</c:key>
  <c:value i:type="d:guid" xmlns:d="http://schemas.microsoft.com/2003/10/Serialization/">EntityIdValue</c:value>
      </a:KeyValuePairOfstringanyType>
      <a:KeyValuePairOfstringanyType>
        <c:key>2EF8C158-182B-444A-A9DF-FF2DC5E44514</c:key>
        <c:value i:type="d:guid" xmlns:d="http://schemas.microsoft.com/2003/10/Serialization /">WorkflowIdValue</c:value>
      </a:KeyValuePairOfstringanyType>
    </a:Parameters>
    <a:RequestId i:nil="true" />
    <a:RequestName>ExecuteWorkflow</a:RequestName>
  </request>
    </Execute>
  </s:Body>
</s:Envelope>

var xhr = new XMLHttpRequest();
xhr.open('POST', '/XRMServices/2011/Organization.svc/web', true);

xhr.setRequestHeader('Accept', 'application/xml, text/xml, */*');
xhr.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');
xhr.setRequestHeader('SOAPAction', 'http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute');

xhr.onreadystatechange = function () { alert(xhr.status); };
xhr.send(request);
}

但是,这无法加载。

如果有人可以帮助我,我将不胜感激。只需要 JavaScript 即可简单地加载工作流。哦,并且具有跨平台兼容性。


function SetLookupValue(fieldName, id, name, entityType) {
    if (fieldName != null) {
        var lookupValue = new Array();
        lookupValue[0] = new Object();
        lookupValue[0].id = id;
        lookupValue[0].name = name;
        lookupValue[0].entityType = entityType;
        Xrm.Page.getAttribute(fieldName).setValue(lookupValue);
    }
}

var ExistingCase = curentUserId = Xrm.Page.context.getUserId();
if (ExistingCase.getValue() != null) {
    var ExistingCaseGUID = ExistingCase.getValue()[0].id;
    var ExistingCaseName = ExistingCase.getValue()[0].name;
    SetLookupValue("new_accountmanager", ExistingCaseGUID, ExistingCaseName, "incident");
}
4

1 回答 1

0

请检查以下代码,上次我使用它时它对我来说很好:

if (typeof (SDK) == "undefined")
   { SDK = { __namespace: true }; }
       //This will establish a more unique namespace for functions in this library. This will reduce the 
       // potential for functions to be overwritten due to a duplicate name when the library is loaded.
       SDK.Workflow = {
           _getServerUrl: function () {
               ///<summary>
               /// Returns the URL for the SOAP endpoint using the context information available in the form
               /// or HTML Web resource.
               ///</summary>
               var ServicePath = "/XRMServices/2011/Organization.svc/web";
               var serverUrl = "";
               if (typeof GetGlobalContext == "function") {
                   var context = GetGlobalContext();
                   serverUrl = context.getServerUrl();
               }
               else {
                   if (typeof Xrm.Page.context == "object") {
                         serverUrl = Xrm.Page.context.getServerUrl();
                   }
                   else
                   { throw new Error("Unable to access the server URL"); }
                   }
                  if (serverUrl.match(/\/$/)) {
                       serverUrl = serverUrl.substring(0, serverUrl.length - 1);
                   } 
                   return serverUrl + ServicePath;
               }, 
           ExecuteRequest: function () {
               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/\">267d4790-e41a-e311-b6a7-3c4a92db481b</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/\">a6832cc5-1d82-48f5-a940-e41269726204</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", SDK.Workflow._getServerUrl(), 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);
               //work with the response here
               var strResponse = req.responseXML.xml;
               alert(strResponse.toString());
           },
 __namespace: true
};

更新: - - - - - - -

我之前的回答是直接提问。由于问题评论,现在提供新答案。正如评论中所说,工作流程只能在现有记录上运行,但正如您所说,您想更改创建表单上的某些字段:记录尚未保存。为此,您必须使用 Xrm 对象,如下所示:

function OnLoadingForm() {
    var FormType = Xrm.Page.ui.getFormType();
    if (FormType != null) {
        switch (FormType) {
            case 1:
                //  It's a "create" form
                var test = "Mr.";                    
                Xrm.Page.getAttribute("salutation").setValue(test);
                break;
            case 2:
                //  It's a "update" form                              
                break;
            case 3:
                // It's a "readonly" form
                break;
            case 4:
                // It's a "disabled" form
                break;
            case 6:
                // It's a  "bulkedit" form
                break;
            default:
                break;
        }
    }
}

此代码适用于经典 CRM 表单(自定义信息表单)。新版本可能会改变这一点。"salutation"是自定义字段的名称。请注意,此字段更改仅在表单上,​​并且在用户单击“保存”按钮后将保存到 CRM DB。

更新 2:------------ 由于新情况新代码段:

function OnLoadingForm() {
    // Get the current FormType
    var FormType = Xrm.Page.ui.getFormType();      
    if (FormType != null) {
        switch (FormType) {
            case 1:
                //  It's a "create" form                

                var curentUserId = Xrm.Page.context.getUserId();
                var serverUrl;
                if (Xrm.Page.context.getClientUrl !== undefined) {
                    serverUrl = Xrm.Page.context.getClientUrl();
                } else {
                    serverUrl = Xrm.Page.context.getServerUrl();
                }
                var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc"; 
                var userRequest = new XMLHttpRequest(); 
                userRequest.open("GET", ODataPath + "/SystemUserSet(guid'" + Xrm.Page.context.getUserId() + "')", false); 
                userRequest.setRequestHeader("Accept", "application/json"); 
                userRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8"); 
                userRequest.send();
                if (userRequest.status === 200) {
                    var retrievedUser = JSON.parse(userRequest.responseText).d; 
                    var userFullName = retrievedUser.FullName;        
                    SetLookupValue("new_accountmanager", curentUserId, userFullName, "systemuser");
                }
                else {
                    return "error";
                }
                break;
            case 2:
                //  It's a "update" form               
                break;
            case 3:
                // It's a "readonly" form
                break;
            case 4:
                // It's a "disabled" form
                break;
            case 6:
                // It's a  "bulkedit" form
                break;
            default:
                break;
        }
    }
}

function SetLookupValue(fieldName, id, name, entityType) {
    if (fieldName != null) {
        var lookupValue = new Array();
        lookupValue[0] = new Object();
        lookupValue[0].id = id;
        lookupValue[0].name = name;
        lookupValue[0].entityType = entityType;
        Xrm.Page.getAttribute(fieldName).setValue(lookupValue);
    }
}
于 2013-09-24T12:20:04.257 回答