You need to sign in to do that
Don't have an account?
MS123456
pls tell me My logic is correct or not
Requirment is: if event is insert then contact will updated some field and if some chenges insterted event the contact will also updated n mos recent event id will be this contact .
trigger eventTriggerContact on Event (after insert, after update) { Set<Id> set_ContactId = new Set<Id>(); List<Contact> conList = new List<Contact>(); if(Trigger.isAfter && Trigger.isInsert) { for(Event e : Trigger.new) { if(e.whoId!=null && String.valueOf(e.whoId).startsWith('003') && (e.Type == 'Consult') ) { set_ContactId.add(e.whoId); } } Map<Id,Contact> conMap = new Map<Id,Contact>([Select Id,StartDateTime__c,Start_Time__c,User_Attorney_Profile_Page__c,User_First_Name__c, User_Last_Name__c,User_Office_Google_Map_Link__c,User_Office_Phone__c,Phone_Number_Text__c, User_Office_Website__c,One_of_Our_Attorneys__c,Most_Recent_Event_Id__c from contact where Id IN:set_ContactId]); for(Event e : Trigger.new) { if(conMap!=null && conMap.containsKey(e.whoId)) { conMap.get(e.whoId).StartDateTime__c= e.StartDateTime; conMap.get(e.whoId).Start_Time__c= e.Start_Time__c; conMap.get(e.whoId).User_Attorney_Profile_Page__c= e.User_Attorney_Profile_Page__c; conMap.get(e.whoId).User_First_Name__c= e.User_First_Name__c; conMap.get(e.whoId).User_Last_Name__c= e.User_Last_Name__c; conMap.get(e.whoId).User_Office_Google_Map_Link__c= e.User_Google_Map_Link__c; conMap.get(e.whoId).User_Office_Phone__c= e.User_Office_Phone__c; conMap.get(e.whoId).User_Office_Website__c = e.User_Office_Website__c ; conMap.get(e.whoId).Phone_Number_Text__c= e.Phone_Number_Text__c; conMap.get(e.whoId).Great_Hands_Text__c= e.Great_Hands_Text__c; conMap.get(e.whoId).One_of_Our_Attorneys__c= e.One_of_Our_Attorneys__c; conMap.get(e.whoId).Most_Recent_Event_Id__c=e.id; conList.add(conMap.values()); } } system.debug('!!!!!!'+ conList); if(conList!=null && conList.size()>0) update conList; } if(Trigger.isAfter && Trigger.isUpdate) { for(Event e : Trigger.new) { if(e.whoId!=null && String.valueOf(e.whoId).startsWith('003') && (e.Type == 'Consult')) { set_ContactId.add(e.whoId); } } Map<Id,Contact> conMap = new Map<Id,Contact>([Select Id,StartDateTime__c,Start_Time__c,User_Attorney_Profile_Page__c,User_First_Name__c, User_Last_Name__c,User_Office_Google_Map_Link__c,User_Office_Phone__c,Phone_Number_Text__c, User_Office_Website__c,One_of_Our_Attorneys__c,Most_Recent_Event_Id__c from contact where Id IN:set_ContactId]); for(Event e : Trigger.new) { if(conMap!=null && conMap.containsKey(e.whoId) && e.id == conMap.get(e.whoId).Most_Recent_Event_Id__c) { conMap.get(e.whoId).StartDateTime__c= e.StartDateTime; conMap.get(e.whoId).Start_Time__c= e.Start_Time__c; conMap.get(e.whoId).User_Attorney_Profile_Page__c= e.User_Attorney_Profile_Page__c; conMap.get(e.whoId).User_First_Name__c= e.User_First_Name__c; conMap.get(e.whoId).User_Last_Name__c= e.User_Last_Name__c; conMap.get(e.whoId).User_Office_Google_Map_Link__c= e.User_Google_Map_Link__c; conMap.get(e.whoId).User_Office_Phone__c= e.User_Office_Phone__c; conMap.get(e.whoId).User_Office_Website__c = e.User_Office_Website__c ; conMap.get(e.whoId).Phone_Number_Text__c= e.Phone_Number_Text__c; conMap.get(e.whoId).Great_Hands_Text__c= e.Great_Hands_Text__c; conMap.get(e.whoId).One_of_Our_Attorneys__c= e.One_of_Our_Attorneys__c; conList.add(conMap.values()); } } system.debug('!!!!!!'+ conList); if(conList!=null && conList.size()>0) update conList; } }
All Answers
Hey my bad,
I have made things complex when your requirement is simple. I am sorry.
Below is the optimized code:
Below is just a pseudo code. Please try yourself as per instructions (For update use case)