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
MaheemSamMaheemSam 

Trigger causing 50k limit exception

Hi,

 Please help to change below trigger this is cauing system.limit.exception not sure where the issue is but still I am getting this error please suggest me.
 
Trigger convertToUSD on Opportunity (before update){
     
  Try 
   {
       List<String> oppiso = new List<String>();  
       List<date> cdate = new List<date>();
      
        List<DatedConversionRate> DatedConversion = [SELECT isocode,startdate,ConversionRate FROM DatedConversionRate] ;
   
        Opportunity conopp = [select  CurrencyIsoCode,amount,closedate  from opportunity where id = :trigger.newmap.keyset() ];  
   
        for(Opportunity o : trigger.new) 
        {
         oppiso.add(o.CurrencyIsoCode);
         cdate.add(o.closedate);
        }
    
        Double cts = [SELECT ConversionRate FROM DatedConversionRate 
                 where isocode IN :oppiso and 
                       startdate <= :cdate 
                 order by startdate desc limit 1].conversionRate;
                 
                  
       for(Opportunity ops : trigger.new) 
       {
        ops.CURRENCY_RATE__c = cts;
       }
    }
    
     catch (System.LimitException e) {
        system.debug('Exception going more than 50k+');
      }
    catch (Exception e)
    {
        system.debug(e);
        for(Opportunity ops : trigger.new) 
        {
        ops.CURRENCY_RATE__c = null;
        }
    }  
}

Thanks
Sudhir
 
Best Answer chosen by MaheemSam
Amit Chaudhary 8Amit Chaudhary 8
There are two SOQL in your code which is not in uSE so i commented same. Please below code and let us know if this will help you

Please update your code like below
Trigger convertToUSD on Opportunity (before update){
     
  Try 
   {
       List<String> oppiso = new List<String>();  
       List<date> cdate = new List<date>();
      
        //List<DatedConversionRate> DatedConversion = [SELECT isocode,startdate,ConversionRate FROM DatedConversionRate] ;
   
        //Opportunity conopp = [select  CurrencyIsoCode,amount,closedate  from opportunity where id = :trigger.newmap.keyset() ];  
   
        for(Opportunity o : trigger.new) 
        {
         oppiso.add(o.CurrencyIsoCode);
         cdate.add(o.closedate);
        }
    
        Double cts = [SELECT ConversionRate FROM DatedConversionRate 
                 where isocode IN :oppiso and 
                       startdate <= :cdate 
                 order by startdate desc limit 1].conversionRate;
                 
                  
       for(Opportunity ops : trigger.new) 
       {
			ops.CURRENCY_RATE__c = cts;
       }
    }
    catch (System.LimitException e) {
        system.debug('Exception going more than 50k+');
    }
    catch (Exception e)
    {
        system.debug(e);
        for(Opportunity ops : trigger.new) 
        {
			ops.CURRENCY_RATE__c = null;
        }
    }  
}

 

All Answers

Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Sudhir Narayanaswamy,

May I suggest you please refer the below link for reference with similar kind of issue. Hope it will be helpful.

Please mark it as best answer if the information is informative.so that question is removed from an unanswered question and appear as a proper solution.

Thanks
Rahul Kumar

 
Amit Chaudhary 8Amit Chaudhary 8
There are two SOQL in your code which is not in uSE so i commented same. Please below code and let us know if this will help you

Please update your code like below
Trigger convertToUSD on Opportunity (before update){
     
  Try 
   {
       List<String> oppiso = new List<String>();  
       List<date> cdate = new List<date>();
      
        //List<DatedConversionRate> DatedConversion = [SELECT isocode,startdate,ConversionRate FROM DatedConversionRate] ;
   
        //Opportunity conopp = [select  CurrencyIsoCode,amount,closedate  from opportunity where id = :trigger.newmap.keyset() ];  
   
        for(Opportunity o : trigger.new) 
        {
         oppiso.add(o.CurrencyIsoCode);
         cdate.add(o.closedate);
        }
    
        Double cts = [SELECT ConversionRate FROM DatedConversionRate 
                 where isocode IN :oppiso and 
                       startdate <= :cdate 
                 order by startdate desc limit 1].conversionRate;
                 
                  
       for(Opportunity ops : trigger.new) 
       {
			ops.CURRENCY_RATE__c = cts;
       }
    }
    catch (System.LimitException e) {
        system.debug('Exception going more than 50k+');
    }
    catch (Exception e)
    {
        system.debug(e);
        for(Opportunity ops : trigger.new) 
        {
			ops.CURRENCY_RATE__c = null;
        }
    }  
}

 
This was selected as the best answer
MaheemSamMaheemSam
Thanks Amit you solved my soloution