我们在CRM 2013上。要求是完全从“任务”表单中删除“关闭”按钮,然后创建一个新的“取消”按钮以取消任务。因此,当单击此按钮时,任务将为“另存为已取消”。
因此,我们关注了Guru Prasad关于使用javascript更改记录的博客:http://guruprasadcrm.blogspot.com/2013/02/change-record-status-using-javascript.html
脚本运行正常。单击“取消”按钮后,它会将状态更改为已取消。
如果在发送取消请求之前修改了页面,我还在SOAP请求之前包含了这行代码。
Xrm.Page.data.entity.save("saveandclose");
然而,我们发现了一个非常奇怪的错误,由于某些原因,从Advanced Find打开记录时不会触发脚本。就好像单击这个新的自定义取消按钮时页面立即关闭一样。
您是否知道在从高级查找打开的记录与常规记录之间运行脚本之间是否存在任何差异?
以下是更改记录状态的javascript:
function changeRecordStatus(RECORD_ID, stateCode, statusCode) {
//first of all save all data before sending the request, in case if there are some data that hasn't been saved
Xrm.Page.data.entity.save("saveandclose");
// create the SetState request
var request = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
request += "<s:Body>";
request += "<Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
request += "<request i:type=\"b:SetStateRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">";
request += "<a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
request += "<a:KeyValuePairOfstringanyType>";
request += "<c:key>EntityMoniker</c:key>";
request += "<c:value i:type=\"a:EntityReference\">";
request += "<a:Id>" + RECORD_ID + "</a:Id>";
request += "<a:LogicalName>task</a:LogicalName>";
request += "<a:Name i:nil=\"true\" />";
request += "</c:value>";
request += "</a:KeyValuePairOfstringanyType>";
request += "<a:KeyValuePairOfstringanyType>";
request += "<c:key>State</c:key>";
request += "<c:value i:type=\"a:OptionSetValue\">";
request += "<a:Value>" + stateCode + "</a:Value>";
request += "</c:value>";
request += "</a:KeyValuePairOfstringanyType>";
request += "<a:KeyValuePairOfstringanyType>";
request += "<c:key>Status</c:key>";
request += "<c:value i:type=\"a:OptionSetValue\">";
request += "<a:Value>" + statusCode + "</a:Value>";
request += "</c:value>";
request += "</a:KeyValuePairOfstringanyType>";
request += "</a:Parameters>";
request += "<a:RequestId i:nil=\"true\" />";
request += "<a:RequestName>SetState</a:RequestName>";
request += "</request>";
request += "</Execute>";
request += "</s:Body>";
request += "</s:Envelope>";
//send set state request
$.ajax({
type: "POST",
contentType: "text/xml; charset=utf-8",
datatype: "xml",
url: Xrm.Page.context.getServerUrl() + "/XRMServices/2011/Organization.svc/web",
data: request,
beforeSend: function (XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("Accept", "application/xml, text/xml, */*");
XMLHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
},
success: function (data, textStatus, XmlHttpRequest) {
Xrm.Page.ui.close();
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
}
然后,这是取消任务的新自定义功能区按钮的命令定义。我将statuscode = 2和statecode = 6传入上面的函数。
<CommandDefinitions>
<CommandDefinition Id="Mscrm.SaveAsCancelled">
<EnableRules>
<EnableRule Id="Mscrm.VisualizationPaneNotMaximized" />
<EnableRule Id="Mscrm.SaveAsCancelled.FormRule" />
</EnableRules>
<DisplayRules>
<DisplayRule Id="Mscrm.CanWriteSelected" />
<DisplayRule Id="Mscrm.SelectedEntityHasStatecode" />
</DisplayRules>
<Actions>
<JavaScriptFunction FunctionName="changeRecordStatus" Library="$webresource:new_DeactivateTask.js">
<CrmParameter Value="FirstPrimaryItemId" />
<IntParameter Value="2" />
<IntParameter Value="6" />
</JavaScriptFunction>
</Actions>
</CommandDefinition>
谢谢,非常感谢您的帮助。
答案 0 :(得分:0)
此链接极大地帮助了我们。这是一个新的CRM 2013功能: http://msdn.microsoft.com/en-us/library/dn481607.aspx