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

Bulk Transaction trigger and helper class
Hi,
I wrote below trigger and helper class which is working fine need your suggestion how to modify the code to handle bulk transaction please suggest me.
Thanks
Sudhir
I wrote below trigger and helper class which is working fine need your suggestion how to modify the code to handle bulk transaction please suggest me.
Trigger trigger CtapAssessmentTrigger on CTAP_Assessment__c (before Insert, before Update) { if(Trigger.isBefore) { if(Trigger.isInsert) { CtapAssessmentTriggerUtils.processInsert(Trigger.new); } else if(Trigger.isUpdate) { CtapAssessmentTriggerUtils.processUpdate(Trigger.newMap, Trigger.oldMap); } } Helper Class public class CtapAssessmentTriggerUtils { public static void processInsert(List<CTAP_Assessment__c> newLst) { for(CTAP_Assessment__c ctap : newLst){ ctap.Contact__c = CtapAssessmentTriggerUtils.GetContact(ctap.Partner_email__c); ctap.Lead__c = CtapAssessmentTriggerUtils.GetLead(ctap.Partner_email__c); } } public static void processUpdate(Map<id,CTAP_Assessment__c> newMap, Map<id,CTAP_Assessment__c> oldMap) { for(CTAP_Assessment__c ctap : newMap.values()){ ctap.Contact__c = CtapAssessmentTriggerUtils.GetContact(ctap.Partner_email__c); ctap.Lead__c = CtapAssessmentTriggerUtils.GetLead(ctap.Partner_email__c); } } public static String GetContact(String partneremail){ String ContID; list<Contact> con = [select id,email from contact where email = :partneremail]; if( con.size()>0){ ContID = con[0].id; } return ContID; } public static String GetLead(String partneremail){ String LedID; list<Lead> led = [select id, email from lead where email = :partneremail]; if (led.size() > 0){ LedID = led[0].id; } return LedID; } }
Thanks
Sudhir
On update use case, if you want the logic to happen only when contact and Lead changes to avoid unnecessary processing then we have to change the logic a bit.
Please let me know if this helps.
All Answers
Create the key,value map with the filed you want to have. loop throguh it as shown.
I have done only the insert you can do the update as the same way
If this helps you please mark it as the best answer so it could help the community in many ways.
On update use case, if you want the logic to happen only when contact and Lead changes to avoid unnecessary processing then we have to change the logic a bit.
Please let me know if this helps.
Sorry I meant only when "email "changes not "contact and Lead" changes.
Thanks Ruwanthalk and Nayana for you help.
Nayana, I used you code it is working I see one issue during update old values wont clear when email dont match.
Example : Contact and Lead both are updated in insert In update when i change the partner contact only contact match but lead do not match in this case lead must be set to null but now it is still showing the old value.
Please let me know if you have any suggestion to change.
Thanks
Sudhir
Replace these lines with
Thanks
Sudhir