请检查以下代码,上次我使用它时它对我来说很好:
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);
}
}