function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Daniel AhlDaniel Ahl 

Lightning:datatable currency field in multiple-currencies org.

I have a question that I'm having a hard time explaining and solving.
So for starters, I've built an aura component where I want to use Lightning:datatable to enter the Amount of an Opportunity that looks like this:

(This is not the actual component I want to use, but for this one I've built an example for better explanation.)

Opportunity.cmp

<aura:component controller="testController" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    <aura:attribute name="recordId" type="String"/>
    <aura:attribute name="data" type="Object"/>
    <aura:attribute name="columns" type="List"/>
    <aura:attribute name="errors" type="Object" default="[]"/>
    <aura:attribute name="draftValues" type="Object" default="[]"/>

    <aura:handler name="init" value="{!this}" action="{!c.init}" />
    <div style="height: 300px">
        <lightning:datatable
            columns="{! v.columns }"
            data="{! v.data }"
            keyField="Id"
            errors="{! v.errors }"
            draftValues="{! v.draftValues }"
            onsave="{! c.handleSaveEdition }"
        />
    </div>
</aura:component>


OpportunityController.js:
({
	init : function(cmp, event, helper) {
        console.log('no');
		cmp.set('v.columns', [
            {label: 'Opp Name', fieldName: 'Name', type: 'Text', editable: false },
            {label: 'Amount', fieldName: 'Amount', type: 'currency', typeAttributes: {currencyCode: { fieldName: 'CurrencyIsoCode' }}, editable: true}
        ]);
        
        helper.fetchOpps(cmp);
	},
    
    handleSaveEdition: function (cmp, event, helper) {
        var draftValues = event.getParam('draftValues');
		console.log(draftValues);
        helper.saveEdition(cmp, draftValues);
    }
})


OpportunityHelper.js:
({
    fetchOpps : function(cmp) {
        let getOpp = cmp.get('c.getOpp');
        getOpp.setParams({OppId : cmp.get('v.recordId')});
        getOpp.setCallback(this, function(response){
            if(response.getState() == 'SUCCESS'){
                console.log(response.getReturnValue());
                cmp.set('v.data', response.getReturnValue());
            }
        });
        $A.enqueueAction(getOpp);
    },
    
    saveEdition : function(cmp, draftValues) {
        var self = this;
        console.log(draftValues);
        let saveOpps = cmp.get('c.saveOpps');
        saveOpps.setParams({opps : draftValues});
        saveOpps.setCallback(this, function(res){
            if(res.getState() == 'SUCCESS'){
                cmp.set('v.errors', []);
                cmp.set('v.draftValues', []);
                self.fetchOpps(cmp);
            } else if (res.getState() === "ERROR") {
                var errors = response.error;
                console.error(errors);
            }
        });
        $A.enqueueAction(saveOpps);
    }
})



Apex Controller = testController.apxc
public class testController {
    @AuraEnabled
    public static List<Opportunity> getOpp(String OppId){
        return [SELECT Id, Name, CurrencyIsoCode, FORMAT(Amount) FROM Opportunity WHERE Id =: OppId];
    }
    
    @AuraEnabled
    public static void saveOpps(List<Opportunity> opps){
        System.debug(opps);
        update opps;
    }
}


Organization information:
Currency: SEK

Enabled currencies: SEK and USD, both having 2 decimal places.

So the issue I have is that this component is behaving odd.
My locale is Swedish, the Opportunity Currency is set to USD.


When I edit the amount field to be 123,45 and save, the saved value is: 12 345,00 US$.

Why is that?
One more question, if I set the value to 123,4 it gets turned into 123,40 US$ but the value is saved as 12,00 US$.


The reason to why I'm using FORMAT(Amount) is because I would like to have the value to be the local currency setting for the record, even when using multi-currencies, but for now this won't let me use decimals for this.

 

Images for trying to explain further:

The amount start at 12,00 US$
So the Opporunity starts with an Amount of 12,00 US$.
I want to set it to 12,34:

User-added image
After saving and retrieving record from database again:
User-added image

Best Answer chosen by Daniel Ahl
Daniel AhlDaniel Ahl
Solved it by sending as a string to Apex-class, then deserializing it into a list of Accounts.
@AuraEnabled
public static void saveAccounts(String AccountList){
  List<Account> Accounts = (List<Account>)JSON.deserialize(AccountList, List<Account>.class);
  update Accounts;
}

 

All Answers

Caleb Kuester 35Caleb Kuester 35

It might have something to do with the fact that, in the United States, $123,45 is going to be represented as $123.45 (not a comma, but a period). I don't know if this can be changed in your org's localle settings.

For your "one more question," check to make sure your field (back-end) is capable of storing decimals. You would go to your sobject definition page and then go to the field itself. Check to make sure it's something like 9,2 and not 9,0. (nine integer places and two decimal places).

Daniel AhlDaniel Ahl

Yes, I know that, but I built this solution and tested it. And when I did, it worked.

Now it has stopped working for some reason, and I don't know why, even if the Opportunity has Swedish currency, and I have Swedish Locale on my account, the decimals doesn't work.

The field itself is set to be a currencyfield with 16,2 in length. (2 decimal places) :(

Daniel AhlDaniel Ahl
Solved it by sending as a string to Apex-class, then deserializing it into a list of Accounts.
@AuraEnabled
public static void saveAccounts(String AccountList){
  List<Account> Accounts = (List<Account>)JSON.deserialize(AccountList, List<Account>.class);
  update Accounts;
}

 
This was selected as the best answer