2014-03-26 34 views
0

我有這個javascript解析代碼,當在案例上按下「解析」按鈕時它會被調用。CRM 2011 - 在解決之前保存事件更改

function CloseIncidentRequest(incidentId) { 
    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:CloseIncidentRequest\" 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>IncidentResolution</c:key>"; 
    requestMain += "   <c:value i:type=\"a:Entity\">"; 
    requestMain += "    <a:Attributes>"; 
    requestMain += "    <a:KeyValuePairOfstringanyType>"; 
    requestMain += "     <c:key>incidentid</c:key>"; 
    requestMain += "     <c:value i:type=\"a:EntityReference\">"; 
    requestMain += "     <a:Id>" + incidentId + "</a:Id>"; 
    requestMain += "     <a:LogicalName>incident</a:LogicalName>"; 
    requestMain += "     <a:Name i:nil=\"true\" />"; 
    requestMain += "     </c:value>"; 
    requestMain += "    </a:KeyValuePairOfstringanyType>"; 
    requestMain += "    <a:KeyValuePairOfstringanyType>"; 
    requestMain += "     <c:key>subject</c:key>"; 
    requestMain += "     <c:value i:type=\"d:string\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\">Parent Case has been resolved</c:value>"; 
    requestMain += "    </a:KeyValuePairOfstringanyType>"; 
    requestMain += "    </a:Attributes>"; 
    requestMain += "    <a:EntityState i:nil=\"true\" />"; 
    requestMain += "    <a:FormattedValues />"; 
    requestMain += "    <a:Id>00000000-0000-0000-0000-000000000000</a:Id>"; 
    requestMain += "    <a:LogicalName>incidentresolution</a:LogicalName>"; 
    requestMain += "    <a:RelatedEntities />"; 
    requestMain += "   </c:value>"; 
    requestMain += "   </a:KeyValuePairOfstringanyType>"; 
    requestMain += "   <a:KeyValuePairOfstringanyType>"; 
    requestMain += "   <c:key>Status</c:key>"; 
    requestMain += "   <c:value i:type=\"a:OptionSetValue\">"; 
    requestMain += "    <a:Value>140310001</a:Value>"; 
    requestMain += "   </c:value>"; 
    requestMain += "   </a:KeyValuePairOfstringanyType>"; 
    requestMain += "  </a:Parameters>"; 
    requestMain += "  <a:RequestId i:nil=\"true\" />"; 
    requestMain += "  <a:RequestName>CloseIncident</a:RequestName>"; 
    requestMain += "  </request>"; 
    requestMain += " </Execute>"; 
    requestMain += " </s:Body>"; 
    requestMain += "</s:Envelope>"; 

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

    if (req.responseXML.getElementsByTagName("faultstring").length == 0) 
     window.location.reload(true); 
    else 
     alert(req.responseXML.getElementsByTagName("faultstring")[0].childNodes[0].nodeValue); 
} 

我需要保存非保存的更改做的情況下(如果有的話)解決它之前。 我試過CloseIncidentRequest函數的調用,但它不工作之前添加此

Xrm.Page.data.entity.save("save"); 

,更改將丟失的情況下得到解決。我也嘗試過「saveandclose」,但它也沒有做到這一點。我也嘗試在保存之後和解決之前刷新頁面,但它也沒有工作。

我也有一個插件,它觸發了案件的「OnClose」事件,但我不知道一種方法來檢索我試圖解決的案例所做的未保存更改。

我可以去任何一種方式,從JavaScript或從插件。我只需要保存更改,不會丟失。

任何想法可以幫助我嗎? 謝謝!

回答

0

您是否嘗試過如下保存案例。

Xrm.Page.data.entity.save(); 

更新

放在保存表單的事件下面的JavaScript。我沒有測試過,所以你可能需要調整它。

function FrmOnSave(prmContext) { 
    // Local variable to store value indicating how the save event was initiated by the user. 
    var wod_SaveMode, wod_SaveEventVal; 

    // Change the Save Event Value as per required Save Event 
    wod_SaveEventVal = 5; 

    if (prmContext != null && prmContext.getEventArgs() != null) { 

      wod_SaveMode = prmContext.getEventArgs().getSaveMode(); 
      if (wod_SaveMode == wod_SaveEventVal) { 

       // Write your validation code here 
       var ismodified = Xrm.Page.data.entity.getIsDirty(); 
       if(ismodified == true) 
       { 
       alert("please save the changes first"); 
       prmContext.getEventArgs().preventDefault(); 
       }     
     } 
    } 
} 

僅供參考請通過this博客。

+0

是的。不工作! – Nicole

+0

我想你需要重寫'Case Close'事件。 – Scorpion

+0

通過這個博客,我相信你會得到一個想法:http://social.technet.microsoft.com/wiki/contents/articles/4122.dynamics-crm-2011-perform-jscript-validations-on-entity -form-b​​efore-execution-of-special-events.aspx – Scorpion