CRM 4 Javascript Error: A currency is required if a value exists in a money field

Posted on Posted in MS CRM 4, MS Dynamics, MS Dynamics CRM

How to use money type of attribute as a lookup in CRM 4.0

Sometimes we need to load a value in an attribute of type money on CRM form using JavasScript

Usually we can use script below to assign values

crmForm.all.myfieldname.DataValue = 100;

But in above case a typical error gets thrown that a currency is required in a money field.

Easiest option to solve this problem is use code below to assign data

crmForm.all.mymoneyfield.CurrencySymbol= ‘£’; //currency symbol

crmForm.all. mymoneyfield.DataValue =99999; //amount

Above code should be enough to solve your issue….

In some cases if you have multiple currencies then you can find the one you need and get the symbol and then assign it

var currency = crmForm.all.transactioncurrencyid;

 

if (currency == null)

{

var lookupData = new Array();

var lookupItem = new Object();

GetCurrency(); // write a small function to fetch your currency and then assign values to your global variables

lookupItem.id = currencyId;

lookupItem.name = currencyName;

lookupItem.typename = ‘transactioncurrency’;

 

lookupData[0] = lookupItem;

 

currency.DataValue = lookupData;

}

 

A sample query to fetch currencies – you can modify it as required

var xml = “” +

“<?xml version=\”1.0\” encoding=\”utf-8\”?>” +

“<soap:Envelope xmlns:soap=\”http://schemas.xmlsoap.org/soap/envelope/\” xmlns:xsi=\”http://www.w3.org/2001/XMLSchema-instance\” xmlns:xsd=\”http://www.w3.org/2001/XMLSchema\”>” +

GenerateAuthenticationHeader() +

” <soap:Body>” +

” <RetrieveMultiple xmlns=\”http://schemas.microsoft.com/crm/2007/WebServices\”>” +

” <query xmlns:q1=\”http://schemas.microsoft.com/crm/2006/Query\” xsi:type=\”q1:QueryExpression\”>” +

” <q1:EntityName>transactioncurrency</q1:EntityName>” +

” <q1:ColumnSet xsi:type=\”q1:ColumnSet\”>” +

” <q1:Attributes>” +

” <q1:Attribute>transactioncurrencyid</q1:Attribute>” +

” <q1:Attribute>currencysymbol</q1:Attribute>” +

” <q1:Attribute>currencyname</q1:Attribute>” +

” </q1:Attributes>” +

” </q1:ColumnSet>” +

” <q1:Distinct>false</q1:Distinct>” +

” </query>” +

” </RetrieveMultiple>” +

” </soap:Body>” +

“</soap:Envelope>” +

“”;

 

// make Web Service call

var xmlHttpRequest = new ActiveXObject(“Msxml2.XMLHTTP”);

 

xmlHttpRequest.Open(“POST”, “/mscrmservices/2007/CrmService.asmx”, false);

xmlHttpRequest.setRequestHeader(“SOAPAction”, “http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple”);

xmlHttpRequest.setRequestHeader(“Content-Type”, “text/xml; charset=utf-8”);

xmlHttpRequest.setRequestHeader(“Content-Length”, xml.length);

xmlHttpRequest.send(xml);

 

// catch return from Web Service

var resultXml = xmlHttpRequest.responseXML;

 

var entityNodes = resultXml.selectNodes(“//RetrieveMultipleRe