You need to sign in to do that
Don't have an account?

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;
}
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;
}
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
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..
"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?
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...