+1 vote
by (163k points)

I have written a simple apex class/trigger. The before update trigger calls a handler class which does some processing (needs to set a field to a specified value). However, I have a for loop inside of the SOQL. Does anyone know how to remove this SOQL from inside the for loop that is in the apex handler? Thank you

Apex trigger calls this handler class:

public static void onBeforeUpdate( List< Opportunity > opportunities , Map<ID , Opportunity > l_old_map ){
List<Opportunity> opps2Update = new List<Opportunity>();
if(opp.Field1__c != l_old_map.get( opp.id ).Field1__c){

the handler apex:

public with sharing class LPP_OpportunityHandler 
public static void setFieldValue(List<Opportunity> opps2Update){
        for(Opportunity opp : opps2Update){
            if(opp.Field2__c == null){
                opp.Field_Status__c = 'Invalid';
            }else {
                String status = [select id, Status__c from LookUpObject_Status__c WHERE Id =:opp.Field1__c].LookupObject_Status__c;
                opp.Field_Status__c = status;

1 Answer

Welcome to Memory Exceeded, where you can ask questions and receive answers from other members of the community.