2013-05-13 50 views
2

您可能会遇到此问题Microsoft Dynamics CRM 2011但今天当我分配任务以动态更改Currency查找值以匹配账户货币在有任何差异的情况下。我只是通过调用我的setLookupValue JavaScript函数来根据账户币种设置货币值。在更改2011年客户关系管理表单中的货币时更改货币字段的符号

setLookupValue("transactioncurrencyid", "transactioncurrency", accountCurrency.Id, accountCurrency.Name); 

做单元测试之后,将货币查找值被完全改变,但我观察到,用表单上的货币数据类型中定义的所有字段的货币符号没有改变到目的地货币这在我的情况下是accountCurrency

例如Currency字段从更改为美元(USD)欧元(EUR)但所有字段都显示USD作为前缀。

回答

-1

在网上花了几个小时后,我收集了一些有关这个问题的不同来源的有用信息。

我已成功设置两种方式,并在IE浏览器上同时测试了两种方式,用于Microsoft Dynamics CRM 2011以动态更改表单上每个货币字段的货币符号。


  • 使用的OData(简单而有效的,没有跨浏览器兼容性问题):
function changeCurrencySymbolOData(guid) { 
    returnValue = retrieveMultiple("TransactionCurrencySet", "?$filter=TransactionCurrencyId eq guid'" + guid + "'"); 
    if (returnValue != null && returnValue[0] != null) { 
     var currencyInfo = returnValue[0]; 
     // looping through all controls on the form and sets the currency symbol. 
     var oCtrl; 
     for (var i = 0; i < crmForm.all.length; i++) { 
      oCtrl = crmForm.all[i]; 
      if (oCtrl.tagName == "INPUT" && 
         (oCtrl.className == "ms-crm-Money-CurrencySymbol" || 
         oCtrl.className == "ms-crm-Money-CurrencySymbol ms-crm-ReadOnly")) { 
       oCtrl.value = currencyInfo.ISOCurrencyCode; 
      } 
     } 
    } 
} 

用法:

changeCurrencySymbolOData(accountCurrency.Id); 

其中accountCurrency是指欧元(EUR)货币。


  • 使用获取XML
function changeCurrencySymbolFetchXML(isoCurrencyCode) { 
    var currencySymbolName = crmForm.all.transactioncurrencyid.cursymclm; 
    // ensuring that currency is present. 
    var fetchCurr = '<fetch mapping="logical" count="50" version="1.0">'; 
    fetchCurr += '<entity name="transactioncurrency">'; 
    fetchCurr += ' <attribute name="currencyname" />'; 
    fetchCurr += ' <attribute name="' + currencySymbolName + '" />'; 
    fetchCurr += ' <filter>'; 
    fetchCurr += '  <condition attribute="isocurrencycode" operator="eq" value="' + isoCurrencyCode + '" />'; 
    fetchCurr += ' </filter>'; 
    fetchCurr += '</entity>'; 
    fetchCurr += '</fetch>'; 

    results = doFetch(fetchCurr); 
    if (results != null && results.length > 0) { 
     var currency = results[0].selectSingleNode('./transactioncurrencyid'); 
     // checking if the same currency is already selected, then there is no need to change anything. 
     if (crmForm.all.transactioncurrencyid.DataValue == null || 
      crmForm.all.transactioncurrencyid.DataValue[0].id != currency.text) { 
      var currencyName = results[0].selectSingleNode('./currencyname').text; 
      // defining the variable to hold the actual symbol. 
      var currencySymbol = results[0].selectSingleNode('./' + currencySymbolName).text; 
      var lookupData = new Array(); 
      var lookupItem = new Object(); 
      // setting the id, typename, and name properties to the object. 
      lookupItem.id = currency.text; 
      lookupItem.name = currencyName; 
      lookupItem.typename = 'transactioncurrency'; 

      // adding the object to the array. 
      lookupData[0] = lookupItem; 
      // setting the value of the lookup field to the value of the array. 
      crmForm.all.transactioncurrencyid.DataValue = lookupData; 
      crmForm.all.transactioncurrencyid.ForceSubmit = true; 

      // looping through all controls on the form and sets the currency symbol. 
      var oCtrl; 
      for (var i = 0; i < crmForm.all.length; i++) { 
       oCtrl = crmForm.all[i]; 
       if (oCtrl.tagName == "INPUT" && 
        (oCtrl.className == "ms-crm-Money-CurrencySymbol" || 
        oCtrl.className == "ms-crm-Money-CurrencySymbol ms-crm-ReadOnly")) { 
        oCtrl.value = currencySymbol; 
       } 
      } 
     } 
    } 
} 

function doFetch(fetchXml) { 
    fetchXml = fetchXml.replace(/</g, '&lt;'); 
    fetchXml = fetchXml.replace(/>/g, '&gt;'); 
    // preparing the SOAP message. 
    var xml = "<?xml version='1.0' encoding='utf-8'?>" 
    xml += "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' " 
    xml += "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " 
    xml += "xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"; 
    xml += Xrm.Page.context.getAuthenticationHeader(); 
    xml += "<soap:Body>"; 
    xml += "<Fetch xmlns='http://schemas.microsoft.com/crm/2007/WebServices'><fetchXml>"; 
    xml += fetchXml; 
    xml += "</fetchXml></Fetch>"; 
    xml += "</soap:Body></soap:Envelope>"; 

    // preparing the xmlHttpObject and send the request. 
    var xHReq = new ActiveXObject("Msxml2.XMLHTTP"); 
    xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false); 
    xHReq.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Fetch"); 
    xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); 
    xHReq.setRequestHeader("Content-Length", xml.length); 
    xHReq.send(xml); 

    // capturing the result. 
    var resultXml = xHReq.responseXML; 

    // checking for errors. 
    var errorCount = resultXml.selectNodes('//error').length; 
    if (errorCount != 0) { 
     var msg = resultXml.selectSingleNode('//description').nodeTypedValue; 
     alert(msg); 
    } 
    // process and display the results. 
    else { 
     // capturing the result and UnEncode it. 
     var resultSet = new String(); 
     resultSet = resultXml.text; 
     resultSet.replace('&lt;', '<'); 
     resultSet.replace('&gt;', '>'); 

     // creating an XML document that you can parse. 
     var oXmlDoc = new ActiveXObject("Microsoft.XMLDOM"); 
     oXmlDoc.async = false; 

     // loading the XML document that has the UnEncoded results. 
     oXmlDoc.loadXML(resultSet); 

     // displaying the results. 
     var results = oXmlDoc.getElementsByTagName('result'); 
     return results; 
    } 
} 

用法:

changeCurrencySymbolFetchXML('EUR'); 

回复来源:

+1

注意,这个代码是不是跨浏览器兼容,它使用的CRM 4.0终点时,从CRM 2011在线情况下(除去与Office 365身份验证) – 2013-05-13 16:53:12

+0

不确定跨浏览器兼容性,但它在IE中正常工作。此外,我已经测试了这个代码,现在在我的CRM 2011项目中使用它,完美地工作。 – 2013-05-14 05:43:30

+0

现在我添加了另一种使用OData来解决它的方法。 – 2013-05-14 07:18:43