BenPatterson1
BenPatterson1

Reputation: 1482

CRM 2011 - Supported version of javascript function to change currency symbol

I have this nice little javascript snippet for CRM 4.0 to update a form's Money fields to use the currency symbol I pass in as a string. (source)

UpdateCurrencySymbolInUI: function (_curr) {
    // BTP: fail to update currency symbol if using browser other than IE (crmForm will not be defined)
    // (set currency not supported via Xrm.Page CRM 2011)
    if (!IsNull(crmForm)) {
        var iLen = crmForm.all.length;
        var oCtrl, i;

        for (i = 0; i < iLen; i++) {
            oCtrl = crmForm.all[i];
            if (!IsNull(oCtrl.IsMoney) && !oCtrl.IsBaseCurrency) {
                oCtrl.CurrencySymbol = _curr;
            }
        }
    }
}

It works perfectly, but for the coming change to multi-browser support (scheduled for 2012Q4 now, right?) Microsoft's custom code validaiton tool suggests this code will not function on any browser other than IE because the crmForm is no longer supported.

After some googling I don't see a supported alternative to setting the currency sybmol. Am I missing something in the API or did they just flat out remove this functionality to assign the currency symbol (through the API)? This post suggests I use document.getElementById and add "_sym" to the fieldname, but I assume this is unsupported. I'd prefer a supported solution.

Upvotes: 2

Views: 1616

Answers (1)

Greg Owens
Greg Owens

Reputation: 3878

Edited: not immediately obvious from the OP's question, but this behaviour (currency code not changed) is only exhibited if the transactioncurrencyid attribute is changed via JScript.

So this is what MS is still doing under the hood in CRM Online and On Premise UR5 (img.lu.transcur.htc). Looks pretty similar to me. Can't see anything here browser specific though... :

function updateCurrencySymbolInUI(){
    if(!IsNull(_oForm))
        for(var iLen=_oForm.all.length,oCtrl,i=0;i<iLen;i++){
            oCtrl=_oForm.all[i];
            if(!IsNull(oCtrl.IsMoney)&&!oCtrl.IsBaseCurrency){
                oCtrl.CurrencySymbol=_sCurSym;
               oCtrl.CurrencyPrecision=_iCurPre
            }        
        }
}

I think the issue with the code validation tool is because as you say crmForm is deprecated. Pass it a reference to the page's FORM object (document.all['crmForm']) in place of crmForm. I reckon that'll work nicely.

Why do you need this code at all? If you change the transaction currency on a form that contains Money fields, the currency symbol in the control automatically changes to the symbol defined in Business Management > Currencies.

If the symbol isn't the one you want to display, change it there :)

Upvotes: 1

Related Questions