5

Trying to figure out how to handle records with multiple currency. The dataTable definitions are for the column. I do not see anywhere in the documentation where the currency code of an individual row can be specified dynamically based off the currency code field (either custom or standard)

the documentation for a formattedNumber component used by the dataTable states

"The locale set in the app's user preferences determines how numbers are formatted."

So if the row has a different currency it will be displayed with the wrong symbol AND the amount will be incorrect.

I.e. if the amount is CAD200.00 it is displayed to the user as $200.00

No code to show as I cannot see a way to do it currently

Has anyone been able to use the dataTable to display rows with differing currencies?

I tested in an org with multi-currency enabled and it did not show the correct symbol or currency despite the record being set to a different currency.

Would appreciate any links explaining? Maybe I am missing something small or a setting somewhere to allow the table to show differing currencies in the rows...

Here is what I came up with (Open to other ideas)

I am sure there are better ways but in a pinch....

Once I get the data back from the server I iterate over it. It contains an Amount field and a currency code field. Since I cannot seem to display the correct amount in the dataTable I am just going to display as an example USD222.00

Problem is that number format if 2222.00 shows as 2222. So I need to add back in the commas and decimals. 1.10 would show as 1.1 and 1234.20 would show as 1234.2 so

for(var x=0;x<data.length;x++){
    //Despite setting the min max precision, localization does not return any trailing zeros
    var formattedAsString =  $A.localizationService.formatNumber(data[x].ns__Amount__c,2,2);
    //get the current precision value adding in trailing zeros . If none set to .00
    var precisionValue = formattedAsString.indexOf('.') === -1 ? '.00' : formattedAsString.substring(formattedAsString.indexOf('.')).concat('00').substring(0,3);

    //Combine it all back together replacing the precision    
    formattedAsString = formattedAsString.substring(0,formattedAsString.indexOf('.') === -1 ? formattedAsString.length : formattedAsString.indexOf('.')).concat(precisionValue);

    //Set the value of the field to the text representation in local format (sans decimal if local is comma, oh well)    
    data[x].ns__Amount__c =    
       data[x].ns__Currency_Code__c.concat(formattedAsString);
}

Not pretty but the following is what shows

Original Value => After localization => final output

USD 1234.00 => 1,234 => USD1,234.00

CAD 234.10 => 234.1 => CAD 234.10

Despite using the min max precision in the localization it drops all precision if the value is a 0 :(

Note I am not sending the data back to the server so populating a number field with text in this case will not have any ill effects. If I had to send it back to the server that would be a pain...

  • Have you tried using SOQL convertCurrency? Or is the goal to show it in native currency value from the record's perspective regardless? – sfdcfox Aug 21 '18 at 18:39
  • @sfdcfox - I need to show the currency the amount is for....Right now I am trying to concat the currency code with the amount but I either can get the decimals to appear or the commas but not both when displaying a string and localization (side issue where localization.formatNumber does not return a proper string when setting min fractions)......I though displaying the amount with the appropriate currency would not be difficult but alas..... – Eric Aug 21 '18 at 18:40
  • While I appreciate what they tried to with lightning:dataTable, I think it would have been a lot better to act like an iterator where you could just do your own thing inside a body template. As it stands, I've never been able to use this component because it lacks one thing or another. You might be better of copy-pasting from slds directly and using an iterator to display the rows yourself. – sfdcfox Aug 21 '18 at 18:43
  • @sfdcfox - I would if it was not for the infinite loading. I really need that. Maybe it is trivial to implement......I may have to go that way in the next version.... – Eric Aug 21 '18 at 18:45
1

You can dynamically specify the currency code in this manner:

{
    label: "Amount", 
    fieldName: "Amount", 
    type: "currency", 
    typeAttributes: { currencyCode: { fieldName: 'CurrencyIsoCode' }}
}

I've put a basic example in an LWC playground but it works the same in Aura.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.