From 08125d955f7f5b27fbb97d2497a3c253c40ae77b Mon Sep 17 00:00:00 2001 From: Novarg1 Date: Wed, 24 Feb 2021 12:03:01 +0100 Subject: [PATCH] Added multicurrency support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added a query with `convertCurrency` function and replaced currency fields with these new values to show currency fields in the user's currency in the datatable. Example: we had an opportunity with amount 100k and currency RUB. When viewing it as a user with EUR currency, we would see amount as "RUB 100,000.00 (EUR 1,106.31)", but when seeing this opp in the DatatableV2, we would see the Amount as "€100,000.00". After this change, the DatatableV2 shows the amount as "€1,106.31" --- .../default/classes/SObjectController2.cls | 33 +++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/force-app/main/default/classes/SObjectController2.cls b/force-app/main/default/classes/SObjectController2.cls index 99a8914..780076d 100644 --- a/force-app/main/default/classes/SObjectController2.cls +++ b/force-app/main/default/classes/SObjectController2.cls @@ -59,8 +59,9 @@ public with sharing class SObjectController2 { Map> picklistFieldMap; // Field API Name, List percentFieldList; List noEditFieldList; - list timeFieldList; - list picklistFieldList; + List timeFieldList; + List picklistFieldList; + List currencyFieldList; String objectName; String objectLinkField; String timezoneOffset; @@ -81,6 +82,7 @@ public with sharing class SObjectController2 { curRR.noEditFieldList = emptyList; curRR.timeFieldList = emptyList; curRR.picklistFieldList = emptyList; + curRR.currencyFieldList = emptyList; curRR.rowData = records; curRR.objectName = 'EmptyCollection'; curRR.objectLinkField = ''; @@ -88,7 +90,7 @@ public with sharing class SObjectController2 { String objName = records[0].getSObjectType().getDescribe().getName(); curRR = getColumnData(curRR, fieldNames, objName); curRR = getLookupData(curRR, records, curRR.lookupFieldList, objName); - curRR = getRowData(curRR, records, curRR.dataMap, curRR.objNameFieldMap, curRR.lookupFieldList, curRR.percentFieldList, objName, curRR.noEditFieldList); + curRR = getRowData(curRR, records, curRR.dataMap, curRR.objNameFieldMap, curRR.lookupFieldList, curRR.percentFieldList, objName, curRR.noEditFieldList, curRR.currencyFieldList); curRR.objectName = objName; } curRR.timezoneOffset = getTimezoneOffset().format(); @@ -114,6 +116,7 @@ public with sharing class SObjectController2 { List noEditFields = new List(); List timeFields = new List(); List picklistFields = new List(); + List currencyFields = new List(); Map> picklistFieldLabels = new Map>(); String objectLinkField = getNameUniqueField(objName); // Name (link) Field for the Datatable SObject System.debug('*** OBJ/LINK' + objname + '/' + objectLinkField); @@ -161,7 +164,10 @@ public with sharing class SObjectController2 { noEditFields.add(fieldName); } } - when 'CURRENCY', 'DECIMAL', 'DOUBLE', 'INTEGER', 'LONG' { + when 'CURRENCY' { + currencyFields.add(fieldName); + } + when 'DECIMAL', 'DOUBLE', 'INTEGER', 'LONG' { // *** create scale attrib in datatableColumnFieldDescriptor and pass the getScale() values in that way. *** } when 'TIME' { @@ -193,6 +199,7 @@ public with sharing class SObjectController2 { curRR.picklistFieldList = picklistFields; curRR.picklistFieldMap = picklistFieldLabels; curRR.objectLinkField = objectLinkField; + curRR.currencyFieldList = currencyFields; return curRR; } @@ -236,13 +243,27 @@ public with sharing class SObjectController2 { } @AuraEnabled - public static ReturnResults getRowData(ReturnResults curRR, List records, Map> dataMap, Map objNameFieldMap, List lookupFields, List percentFields, String objName, List noEditFields) { + public static ReturnResults getRowData(ReturnResults curRR, List records, Map> dataMap, Map objNameFieldMap, List lookupFields, List percentFields, String objName, List noEditFields, List currencyFields) { // Update object to include values for the "Name" field referenced by Lookup fields String lookupFieldData = ''; Map firstRecord = new Map(); for(String lf : lookupFields) { firstRecord.put(lf,true); } + + String objectName = records[0].getSObjectType().getDescribe().getName(); + String currencyFieldsQuery = 'SELECT Id'; + for (String currField : currencyFields) { + currencyFieldsQuery += ', convertCurrency(' + currField + ')'; + } + currencyFieldsQuery += ' FROM ' + objectName + ' WHERE Id IN :records'; + Map sobjCurrencyFields = new Map(Database.query(currencyFieldsQuery)); + for (SObject so : records) { + SObject converted = sobjCurrencyFields.get(so.Id); + for (String currField : currencyFields) { + so.put(currField, converted.get(currField)); + } + } for(SObject so : records) { @@ -393,4 +414,4 @@ public with sharing class SObjectController2 { return UserInfo.getTimezone().getOffset(dtNow); } -} \ No newline at end of file +}