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
MS123456MS123456 

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;
   
  }        
}
Best Answer chosen by MS123456
Nayana KNayana K
trigger eventTriggerContact on Event (after insert, after update) 
{
	Set<Id> set_ContactId = new Set<Id>();
	Map<Id,Contact> conMap = new Map<Id,Contact>();
	
	if(Trigger.isAfter && (Trigger.isInsert || 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);
			}
		}
		if(!set_ContactId.isEmpty())
		{
			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)
		{
			system.debug('@@@@@@'+ e);
			if(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;
			   
			}
		}

		system.debug('!!!!!!'+ conMap);
		if(!conMap.isEmpty())
			update conMap.values();  
	}
}

 

All Answers

Nayana KNayana K
trigger eventTriggerContact on Event (after insert, after update) 
{
	Set<Id> set_ContactId = new Set<Id>();
	Map<Id,Contact> conMap = new Map<Id,Contact>();
	
	if(Trigger.isAfter && (Trigger.isInsert || 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);
			}
		}
		if(!set_ContactId.isEmpty())
		{
			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)
		{
			system.debug('@@@@@@'+ e);
			if(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;
			   
			}
		}

		system.debug('!!!!!!'+ conMap);
		if(!conMap.isEmpty())
			update conMap.values();  
	}
}

 
This was selected as the best answer
MS123456MS123456
how to write above code test class can u explain
 
Nayana KNayana K

Hey my bad, 
I have made things complex when your requirement is simple. I am sorry.
Below is the optimized code:
trigger eventTriggerContact on Event (after insert, after update) 
{
	Map<Id,Contact> conMap = new Map<Id,Contact>();
	Contact objCon;
	
	for(Event e : Trigger.new)
	{
		/** What if in future salesforce changes prefix from 003 to 999 after some days?!! Old code doesn't work. Let's make it dynamic */
		/* 	1. On Insert
				OR
			2. On Update, only if specific field - values are changes 
		*/
		if(Trigger.isAfter && e.whoId != null && e.whoId.getSObjectType() == Contact.SobjectType && e.Type == 'Consult'
			&& 
			(	Trigger.isInsert 
				||
				(Trigger.isUpdate && 
					(
						e.StartDateTime__c != Trigger.oldMap.get(e.Id).StartDateTime__c
						||
						e.Start_Time__c != Trigger.oldMap.get(e.Id).Start_Time__c
						||
						e.User_Attorney_Profile_Page__c != Trigger.oldMap.get(e.Id).User_Attorney_Profile_Page__c
						||
						e.User_First_Name__c != Trigger.oldMap.get(e.Id).User_First_Name__c
						||
						e.User_Google_Map_Link__c != Trigger.oldMap.get(e.Id).User_Google_Map_Link__c
						||
						e.User_Office_Website__c != Trigger.oldMap.get(e.Id).User_Office_Website__c
						||
						e.User_Office_Phone__c != Trigger.oldMap.get(e.Id).User_Office_Phone__c
						||
						e.Phone_Number_Text__c != Trigger.oldMap.get(e.Id).Phone_Number_Text__c
						||
						e.Great_Hands_Text__c != Trigger.oldMap.get(e.Id).Great_Hands_Text__c
						||
						e.One_of_Our_Attorneys__c != Trigger.oldMap.get(e.Id).One_of_Our_Attorneys__c
					)
				)
			)
				
		  )
		{	
			objCon = new Contact(Id = e.whoId);
			objCon.StartDateTime__c = e.StartDateTime__c;
			objCon.Start_Time__c = e.Start_Time__c;
			objCon.User_Attorney_Profile_Page__c = e.User_Attorney_Profile_Page__c;
			objCon.User_First_Name__c = e.User_First_Name__c;
			objCon.User_Last_Name__c = e.User_Last_Name__c;
			objCon.User_Office_Google_Map_Link__c = e.User_Google_Map_Link__c;
			objCon.User_Office_Phone__c = e.User_Office_Phone__c;
			objCon.User_Office_Website__c = e.User_Office_Website__c ;
			objCon.Phone_Number_Text__c = e.Phone_Number_Text__c;
			objCon.Great_Hands_Text__c = e.Great_Hands_Text__c;
			objCon.One_of_Our_Attorneys__c = e.One_of_Our_Attorneys__c;
			objCon.Most_Recent_Event_Id__c = e.id;
			conMap.put(e.whoId, objCon);
		}
	}

	system.debug('!!!!!!'+ conMap);
	if(!conMap.isEmpty())
		update conMap.values();
}

Below is just a pseudo code. Please try yourself as per instructions (For update use case)
 
@isTest 
private class eventTriggerContactTest 
{
    static testMethod void testMostRecentUpdation() 
	{
		/* Prepare Test Data - START */
		Contact objContact = new Contact(LastName = 'TestCon' /* Fill other necessary field-value pairs here*/);
		insert objContact;
		/* Prepare Test Data - END */
		
		// insert event
		Event objEvent = new Event();
		objEvent.Type = 'Consult';
		objEvent.Description = 'Test Desc'; 
		objEvent.WhatId = objContact.Id; 
		/* Here you have to populate a field which you can update later. Say objEvent.ABC__c = 'DUMMY';*/
		insert objEvent;
		// Test that the trigger correctly updated the Most_Recent_Event_Id__c
		System.assertEquals(objEvent.Id, [SELECT Most_Recent_Event_Id__c FROM Contact WHERE Id =: objContact.Id].Most_Recent_Event_Id__c);
		
		// insert one more event
		Event objEvent2 = new Event();
		objEvent2.Type = 'Consult';
		objEvent2.Description = 'Test Desc'; 
		objEvent2.WhatId = objContact.Id; 
		insert objEvent2;
		// Test that the trigger correctly updated the Most_Recent_Event_Id__c
		System.assertEquals(objEvent2.Id, [SELECT Most_Recent_Event_Id__c FROM Contact WHERE Id =: objContact.Id].Most_Recent_Event_Id__c);
		
		/* Update specific field on the first event (The one which are not formula but used inside the formula which causes the control to go inside update logic).
			Say, objEvent.ABC__c = 'CHANGED';*/
		update objEvent;
		// Test that the trigger correctly updated the Most_Recent_Event_Id__c
		System.assertEquals(objEvent.Id, [SELECT Most_Recent_Event_Id__c FROM Contact WHERE Id =: objContact.Id].Most_Recent_Event_Id__c);
}
}

 
Nayana KNayana K
Please go through trailhead.. You can learn. It wouldn't be possible to be available in weekends due to personal commitments.  I apologize.