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

how do i bulkify this trigger
Hi all, I am a bit new to salesforce and i succesfully wrote this trigger. But i am lack of ideas on bulkifying it. The below trigger is a basic trigger to check if a contact is linked to a program or opportunity or account.
trigger CheckLinktoPrograms on Contact (after update,before delete) {
if(trigger.isupdate){
List<ProgramContactRole__c> lstConRole = new List<ProgramContactRole__c>();
List<AccountContactRole> lstConRole3 = new List<AccountContactRole>();
List<OpportunityContactRole> lstConRole4 = new List<OpportunityContactRole>();
Set<Id> setContactId = new Set<Id>();
for(Contact c:trigger.new){
setContactId.add(c.id);
}
lstConRole=[select id,name,ContactId__c,ProgramId__c,ProgramId__r.name from ProgramContactRole__c where ContactId__c IN:setContactId];
lstConRole3=[select id,ContactId,AccountId,Account.name from AccountContactRole where ContactId IN: setContactId];
lstConRole4=[select id,ContactId,OpportunityId,Opportunity.name from OpportunityContactRole where ContactId IN: setContactId];
for(integer i=0;i<trigger.new.size();i++){
if(trigger.new[i].Active__c==false ){
for(ProgramContactRole__c pcr:lstConRole){
if(pcr.ContactId__c==trigger.new[i].id){
trigger.new[0].addError('Cannot inactivate Contact if its linked to a Program: '+pcr.ProgramId__r.name);
}
}
for(AccountContactRole acr:lstConRole3){
if(acr.ContactId==trigger.new[i].id){
trigger.new[0].addError('Cannot inactivate Contact if its linked to an Account: '+acr.Account.name);
}
}
for(OpportunityContactRole ocr:lstConRole4){
if(ocr.ContactId==trigger.new[i].id){
trigger.new[0].addError('Cannot inactivate Contact if its linked to an Opportunity: '+ocr.Opportunity.name);
}
}
}
if(trigger.new[i].To_be_deleted__c == true){
for(ProgramContactRole__c pcr:lstConRole){
if(pcr.ContactId__c==trigger.new[i].id){
trigger.new[0].addError('Cannot mark as delete if its linked to a Program: '+pcr.ProgramId__r.name);
}
}
for(AccountContactRole acr:lstConRole3){
if(acr.ContactId==trigger.new[i].id){
trigger.new[0].addError('Cannot mark as delete if its linked to an Account: '+acr.Account.name);
}
}
for(OpportunityContactRole ocr:lstConRole4){
if(ocr.ContactId==trigger.new[i].id){
trigger.new[0].addError('Cannot mark as delete if its linked to an Opportunity: '+ocr.Opportunity.name);
}
}
}
}
}
if(trigger.isdelete){
List<AccountContactRole> lstConRole1 = new List<AccountContactRole>();
List<OpportunityContactRole> lstConRole2 = new List<OpportunityContactRole>();
Set<Id> setContactId = new Set<Id>();
for(Contact c:trigger.old){
setContactId.add(c.id);
}
lstConRole1=[select id,ContactId,AccountId,Account.name from AccountContactRole where ContactId IN: setContactId];
lstConRole2=[select id,ContactId,OpportunityId,Opportunity.name from OpportunityContactRole where ContactId IN: setContactId];
for(integer i=0;i<trigger.old.size();i++){
for(AccountContactRole acr:lstConRole1){
if(acr.ContactId==trigger.old[i].id){
string accountname='<html><bold>';
accountname+=acr.Account.name;
accountname+='</body></html>';
trigger.old[0].addError('Cannot delete Contact if its linked to an Account: '+accountname+' with a Role ( Contact Role ) ');
}
}
for(OpportunityContactRole ocr:lstConRole2){
if(ocr.ContactId==trigger.old[i].id){
trigger.old[0].addError('Cannot delete Contact if its linked to an Opportunity: '+ocr.Opportunity.name+' with a Role ( Contact Role ) ');
}
}
}
}
}
It seems your code is already bulkified , because all your SOQL is outside for loop and you have used SOQL 5 times.
However you can do some modifications on total numbers of scripts executed.
Are you getting any error ?