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
daniel.knecht1.396874461719167E12daniel.knecht1.396874461719167E12 

How to bulkify trigger to loop through related records

Hi everyone,

I have been putting together a trigger to update all related child objects (ts2__Applications__c) if a change occurs on the parent (ts2__Project_Job__c). If a project is being set to "Cancelled" or "Lost", all related Applications should be set to "Not selected" status. The below trigger has worked flawlessly in our sandbox and got me 100% coverage. Yet, I realized that I need to bulkify it in order to get it into production. Being new to Apex programming, could anyone give me a hand to get the below trigger to work properly?

Many thanks and best regards,
Daniel

Trigger ProjectStatusChangeLostCancelled on ts2__Project_Job__c (after update){
List<ts2__Application__c> lstToUpdate = new List<ts2__Application__c>();
for(ts2__Application__c StatusChangePOT :[select Staffing_Status__c from ts2__Application__c  where  ts2__Project__c in : trigger.new AND ts2__Project__r.Status__c IN ('Cancelled', 'Lost')]){
if (
StatusChangePOT.Staffing_Status__c == 'IP proposed'
||
StatusChangePOT.Staffing_Status__c == 'Alternative IP proposed'
||
StatusChangePOT.Staffing_Status__c == 'IP contacted')
{
StatusChangePOT.Staffing_Status__c = 'IP not selected';
}
lstToUpdate.add(StatusChangePOT);
}
if(!lstToUpdate.isEmpty())
update lstToUpdate;
}
Best Answer chosen by daniel.knecht1.396874461719167E12
Arunkumar RArunkumar R
Hi Daniel,

Your trigger code looks good and this codes will handle bulk records..

Some of the points about your code,

1. You are used SOQL Query in for loop to process records. It's best process because each time the record will handle 200 records.

2. You have updating values by adding to list and then updating.

So No need hesitation to move this trigger.

Thanks..

All Answers

Arunkumar RArunkumar R
Hi Daniel,

Your trigger code looks good and this codes will handle bulk records..

Some of the points about your code,

1. You are used SOQL Query in for loop to process records. It's best process because each time the record will handle 200 records.

2. You have updating values by adding to list and then updating.

So No need hesitation to move this trigger.

Thanks..
This was selected as the best answer
daniel.knecht1.396874461719167E12daniel.knecht1.396874461719167E12
Thanks Arunkumar! It is good to have some affirmation that my coding is not completely off. Only when I looked at the error log another time, I realized that the issue is actually with the test class. Working like a charm now...
Arunkumar RArunkumar R
Welcome Daniel....
daniel.knecht1.396874461719167E12daniel.knecht1.396874461719167E12
Hmm, indeed I my joy came too early. I still encounter the test failures during deployment. All the errors occur with test classes of other items (7 items). The error I get is always the same: 

"System.LimitException: Too many SOQL queries: 101
Stack Trace: Class.ProjRolePipHandler.calculateRoleProjectCurrFields: line 165, column 1 Trigger.ProjRolePipAmountAndCurrenciesOnPro: line 5, column 1"

However, the trigger referenced in the error is one that is not subject to the deployment, but that is already active from a previous deployment through an independent developer of ours. Is the cause of the error likely to be within this other trigger?
Arunkumar RArunkumar R
1. I think Your trigger will not affect any other class. If the existing class fails means the corresponding class does not handling Bulk of records. So please change the rewrite the failed class codes and deploy to production.

2. While deploying class to production all existing and new test classes will execute. If any one of the test class fails deployment will get failed.

Thanks...