From 59409d08e0e2c89d7aeb9685b24b35af64cca51a Mon Sep 17 00:00:00 2001 From: Andrew Fawcett Date: Sat, 27 May 2017 20:49:16 -0700 Subject: [PATCH] Fixed issue using currency rollups in a community context https://github.com/afawcett/declarative-lookup-rollup-summaries/issues/4 54 --- rolluptool/src/classes/LREngine.cls | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/rolluptool/src/classes/LREngine.cls b/rolluptool/src/classes/LREngine.cls index db00e261..0d5a894d 100644 --- a/rolluptool/src/classes/LREngine.cls +++ b/rolluptool/src/classes/LREngine.cls @@ -67,13 +67,22 @@ public class LREngine { // Currency lookup static final Map currencyConversionMap = new Map(); private static Decimal convertCurrency(String iso, Decimal val) { - if(currencyConversionMap.isEmpty()){ - String query = 'select IsoCode, ConversionRate from CurrencyType where IsActive = true'; - for(sObject ct : Database.query(query)) - currencyConversionMap.put((String)ct.get('IsoCode'), (Decimal)ct.get('ConversionRate')); - } - return val!=null ? val * currencyConversionMap.get(iso) : 0; + return new ConvertCurrencyHelper().convertCurrencyWithoutSharing(iso, val); } + + /** + * See here for the reason why this has to run "without sharing" https://github.com/afawcett/declarative-lookup-rollup-summaries/issues/454 + **/ + private without sharing class ConvertCurrencyHelper { + private Decimal convertCurrencyWithoutSharing(String iso, Decimal val) { + if(currencyConversionMap.isEmpty()){ + String query = 'select IsoCode, ConversionRate from CurrencyType where IsActive = true'; + for(sObject ct : Database.query(query)) + currencyConversionMap.put((String)ct.get('IsoCode'), (Decimal)ct.get('ConversionRate')); + } + return val!=null ? val * currencyConversionMap.get(iso) : 0; + } + } /** Key driver method that rolls up lookup fields based on the context. This is specially useful in Trigger context.