Some of you might have faced this issue in Microsoft Dynamics CRM 2011 but today when i was assigned the task to change the Currency
lookup value dynamically to match the account currency in case of any difference. I did the same simply by calling my setLookupValue
JavaScript function to set the currency value according to account currency.
setLookupValue("transactioncurrencyid", "transactioncurrency", accountCurrency.Id, accountCurrency.Name);
After doing the unit test, the Currency lookup value was changing perfectly but i observed that currency symbols of all the fields defined with currency datatype on the form were not changing to destination currency which in my case was accountCurrency
.
e.g. Currency
field was changed from US Dollar (USD) to Euro (EUR) but all the fields were showing USD
as a prefix.
After spending some hours on the web i gathered some useful information in chunks from different sources with respect to this problem.
I have managed to device two ways and tested both ways on IE browser for Microsoft Dynamics CRM 2011 to change the currency symbol of every currency field on the form dynamically.
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;
}
}
}
}
Usage:
changeCurrencySymbolOData(accountCurrency.Id);
where accountCurrency
referring to Euro (EUR) currency.
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, '<');
fetchXml = fetchXml.replace(/>/g, '>');
// 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('<', '<');
resultSet.replace('>', '>');
// 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;
}
}
Usage:
changeCurrencySymbolFetchXML('EUR');
Resources: