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
GMASJGMASJ 

Flow Error from a trigger

Hi, 

  I have a trigger below which is on opporunity which is working without any issue now I have a problem there is a process builder which update a custom date based on a stage change when this process fires  I am getting below error 

Error element myRule_1_A1 (FlowRecordUpdate).
The flow tried to update these records: null. This error occurred: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY: convertToUSD: System.LimitException: Too many query rows: 50001. For details, see API Exceptions.​
rigger convertToUSD on Opportunity (before update){
     
  Try 
   {
   List<String> oppiso = new List<String>();   
   List<Id> oppid = new List<Id>(); 
   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) 
    {
     oppid.add(o.id);
     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 (Exception e)
{
  system.debug(e);
     for(Opportunity ops : trigger.new) 
   {
    ops.CURRENCY_RATE__c = null;
    }
  }  
}

Please suggest me how to fix this issue in code or process builder
Deepak Pandey 13Deepak Pandey 13
Try this Sudhir,

rigger 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 (Exception e)
    {
        system.debug(e);
        for(Opportunity ops : trigger.new) 
        {
        ops.CURRENCY_RATE__c = null;
        }
    }  
}
GMASJGMASJ
Thanks Deepak for you reply oppid which was unused int he code is causing the issue ? 
Deepak Pandey 13Deepak Pandey 13
No actually you querying record in loop with inner loop. So the apex has limitation it's retrieve only 50,000 record and your exception showing that it's retrieving 50,000 plus record. I just customize your code.And try to avoid inner loop in apex class.
GMASJGMASJ
Hi Deepak,

  I am still getting the same error below is the details of error email I received

Error element myRule_1_A1 (FlowRecordUpdate).
The flow tried to update these records: null. This error occurred: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY: convertToUSD: System.LimitException: Too many query rows: 50001. For details, see API Exceptions.

This report lists the elements that the flow interview executed. The report is a beta feature.
We welcome your feedback on IdeaExchange.

Flow Details
Flow Name: Opportunity_Forecast_Category_Change
Type: Workflow
Version: 3
Status: Active

Flow Interview Details
Interview Label: Opportunity_Forecast_Category_Change-3_InterviewLabel
Current User: Tom Nash (00580000006Fns6)
Start time: 10/10/2017 10:04
Duration: 0 seconds

How the Interview Started
Tom Nash (00580000006Fns6) started the flow interview.
Some of this flow's variables were set when the interview started.
myVariable_old = 00634000019252gAAA
myVariable_current = 00634000019252gAAA

ASSIGNMENT: myVariable_waitStartTimeAssignment
{!myVariable_waitStartTimeVariable} Equals {!Flow.CurrentDateTime}
Result
{!myVariable_waitStartTimeVariable} = "10/10/2017 10:04"

DECISION: isChangedDecision2_myRule_1_ForecastCategory
Executed this outcome: isChangedRule_2_myRule_1_ForecastCategory
Outcome conditions: and
1. {!myVariable_old} (00634000019252gAAA) Is null false
2. {!myVariable_old.ForecastCategory} (Forecast) Does not equal {!myVariable_current.ForecastCategory} (Closed)
Logic: All conditions must be true (AND)

DECISION: myDecision
Executed this outcome: myRule_1
Outcome conditions: and
1. {!isChangedRule_2_myRule_1_ForecastCategory} (true) Equals true
Logic: All conditions must be true (AND)

RECORD UPDATE: myRule_1_A1
Find all Opportunity records where:
Id Equals {!myVariable_current.Id} (00634000019252gAAA)
Update the records’ field values.
Forecast_Category_Change_Date__c = {!formula_3_myRule_1_A1_5674504787} (10 October 2017)
Result
Failed to update records that meet the filter criteria.

Error Occurred: The flow tried to update these records: null. This error occurred: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY: convertToUSD: System.LimitException: Too many query rows: 50001. For details, see API Exceptions.

Thanks
Sudhir