-2

I have a new trigger and when I try to deploy I get the above error. I know this due to SELECT statement. I tried with List object but still got that error.

Thanks in advance Jay

trigger TAFieldUpdate on Training_Plan__c (after insert, after update) {

    for (Training_Plan__c TP : Trigger.new) {
        Training_Agreement__c TA = [SELECT Programme__c, Expected_Completion__c, MA_Anticipated_End_Date__c,
                                    Complex_Modern_Apprenticeship_Programme__r.Program__r.Name,
                                    Achieved_Credit_2015__c
                                    From Training_Agreement__c
                                    WHERE ID = :TP.Training_Agreement__c];

        If((TP.Funding__c == 'IT - Industry Training') && (TP.TEC_Status__c == 'Active'
                || TP.TEC_Status__c == 'Complete'
                || TP.TEC_Status__c == 'On Hold'
                || TP.TEC_Status__c == 'Withdrawn'
                || TP.TEC_Status__c == 'Pending Transfer')) {
            //TA.Programme__c = TP.Program_Version__r.Program__r.Name;
            TA.Programme__c = TP.Program_Name__c;
            TA.Expected_Completion__c = TP.Expected_Completion__c;
            TA.Achieved_Credit_2015__c = TP.Achieved_Credit_2015__c;
            Update TA;
        }
        else if (TP.Funding__c == 'MA - Modern Apprenticeship' || TP.Funding__c == 'NZA - New Zealand Apprenticeship') {
            //TA.Programme__c = TA.CMA_Program_Name__c;
            TA.Programme__c = TA.Complex_Modern_Apprenticeship_Programme__r.Program__r.Name;
            TA.Expected_Completion__c = TA.MA_Anticipated_End_Date__c;
            Update TA;
        } else if (TP.Funding__c == 'UF - Un-funded' && TP.Non_TEC_Funded_Status__c == 'Active') {
            TA.Programme__c = TP.Program_Name__c;
            //TA.Programme__c = TP.Program_Version__r.Program__r.Name;
            Update TA;
        }
    }
}

3 Answers 3

4

You have a few troubles with your code:

  1. using SOQL inside for loop
  2. using DML inside for loop

Please read this good answer

    trigger TAFieldUpdate on Training_Plan__c (after insert, after update) {

        Set<Id> taIds = new Set<Id>();
        for (Training_Plan__c tp : Trigger.new) {
            taIds.add(tp);
        }
        System.debug(LoggingLevel.DEBUG, 'taIds: ' + taIds);
        List<Training_Agreement__c> trainingAgreements =
            [SELECT Id, Programme__c, Expected_Completion__c, MA_Anticipated_End_Date__c,
                    Complex_Modern_Apprenticeship_Programme__r.Program__r.Name,
                    Achieved_Credit_2015__c
             FROM Training_Agreement__c
             WHERE Id IN: taIds];
        Map<Id, Training_Agreement__c> trainingAgreementByIds = new Map<Id, Training_Agreement__c>(trainingAgreements);
        List<Training_Agreement__c> taForUpdate = new List<Training_Agreement__c>();

        for (Training_Plan__c trainingPlan : Trigger.new) {
            Training_Agreement__c trainingAgrement = trainingAgreementByIds.get(trainingPlan.Training_Agreement__c);
            System.debug(LoggingLevel.DEBUG, 'trainingAgrement='+trainingAgrement);
            if (trainingAgrement != null) {
                if((trainingPlan.Funding__c == 'IT - Industry Training') && (trainingPlan.TEC_Status__c == 'Active'
                        || trainingPlan.TEC_Status__c == 'Complete'
                        || trainingPlan.TEC_Status__c == 'On Hold'
                        || trainingPlan.TEC_Status__c == 'Withdrawn'
                        || trainingPlan.TEC_Status__c == 'Pending Transfer')) {
                    trainingAgrement.Programme__c            = trainingPlan.Program_Name__c;
                    trainingAgrement.Expected_Completion__c  = trainingPlan.Expected_Completion__c;
                    trainingAgrement.Achieved_Credit_2015__c = trainingPlan.Achieved_Credit_2015__c;
                    taForUpdate.add(trainingAgrement);
                } else if (trainingPlan.Funding__c == 'MA - Modern Apprenticeship' || trainingPlan.Funding__c == 'NZA - New Zealand Apprenticeship') {
                    trainingAgrement.Programme__c           = trainingAgrement.Complex_Modern_Apprenticeship_Programme__r.Program__r.Name;
                    trainingAgrement.Expected_Completion__c = trainingAgrement.MA_Anticipated_End_Date__c;
                    taForUpdate.add(trainingAgrement);
                } else if (trainingPlan.Funding__c == 'UF - Un-funded' && trainingPlan.Non_TEC_Funded_Status__c == 'Active') {
                    trainingAgrement.Programme__c = trainingPlan.Program_Name__c;
                    taForUpdate.add(trainingAgrement);
                }
            }
        }
        if ( !taForUpdate.isEmpty()) {
            System.debug(LoggingLevel.DEBUG, taForUpdate.size() + ' records were prepared for update');
            update taForUpdate;
        } else {
            System.debug(LoggingLevel.DEBUG, 'Nothing to update.);
        }

    }
2
  • Hi, there are no compile errors. But when I run the trigger fields do not get updated on the parent record. Anything missing??, THANKS
    – Jay Weera
    Commented Feb 16, 2015 at 5:24
  • @JayWeera please check the code above, I've updated it with logging statements. Please run code and check logs in developer console. Post logs if any further issues. Commented Feb 16, 2015 at 6:51
2

Your query is inside for loop, that's why limit exception is occurring in your code. Take a list outside for loop, fetch query result in it and then loop over that list. Also, your update statement is in the loop. You may take another list, add updated records in it and update it outside for loop.

1
  • Thank you very much your early reply. Can you please edit this trigger as I am very new to triggers. That would be very helpful me to understand triggers and to work on other triggers as well
    – Jay Weera
    Commented Feb 15, 2015 at 4:59
1

Error is due to using SOQL query inside the for loop. Your Trigger is not buklified.

Try below link to learn bulkification

https://developer.salesforce.com/page/Apex_Code_Best_Practicesenter link description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .