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 use my trigger n write handler cls...

hi Nayana K...

 
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
Amit Chaudhary 8Amit Chaudhary 8
try to update your code like below

Handler class like below
public with sharing class EventTriggerHandler 
{
    public void OnAfterInsert( List<Event> newEvent)
    {
        updateContactOnInsert(newEvent);
    }

    public void OnAfterUpdate( List<Event> newEvent, List<Event> oldEvent, Map<ID, Event> newEventMap , Map<ID, Event> oldEventMap )
    {
        updateContactOnUpdate(newEvent,oldEventMap);
    }

	public void updateContactOnUpdate( List<Event> newEvent , Map<ID, Event> oldEventMap )
    {
		for(Event e : newEvent)
        {
			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 : newEvent)
		{
			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;
		}
	}
	
	public void updateContactOnInsert( List<Event> newEvent)
    {
	
		Set<Id> set_ContactId = new Set<Id>();
		List<Contact> conList = new List<Contact>(); 
		for(Event e : newEvent )
		{
			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 : newEvent )
		{
           
            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());
            }
        }
		
        if(conList!=null && conList.size()>0)
		{
             update conList;
		}	
	}
}



Trigger
trigger eventTriggerContact on Event (after insert, after update) {

	EventTriggerHandler handler = new EventTriggerHandler();

	If(Trigger.isAfter)
	{
		if(Trigger.isInsert)
		{
			handler.OnAfterInsert(trigger.New);
		}
		else if (Trigger.isUpdate)
		{
			handler.OnAfterUpdate(trigger.New ,trigger.Old,Trigger.NewMap,Trigger.OldMap);
		}
	}

}

Trigger Best Practices | Sample Trigger Example | Implementing Trigger Framework
http://amitsalesforce.blogspot.com/2015/06/trigger-best-practices-sample-trigger.html

Let us know if this will help you
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Hi,
I will recommend you to start using trailhead to learn about test classes
1) https://trailhead.salesforce.com/modules/apex_testing

Also please check below post
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm

Pleasse check below post sample test class
1) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html

You write a test class for this the same way that you would any other:

- Set up some data for the Trigger to access (Like Contact and Event)
- Instantiate the Trigger- by insert event and update event
- Execute a method/methods
- Verify the behaviour with asserts.

Please try below test class
@isTest 
private class eventTriggerContactTest 
{
    static testMethod void testMostRecentUpdation() 
	{
		Contact objContact = new Contact();
		objContact.LastName = 'TestCon';
		objContact.FirstName = 'TestCon';
		insert objContact;
		
		Event objEvent = new Event();
		objEvent.Type = 'Consult';
		objEvent.Description = 'Test Desc'; 
		objEvent.whoId = objContact.Id; 
		objEvent.StartDateTime = System.today();
		objEvent.EndDateTime = System.today();
		objEvent.Subject  = 'Test Event';
		
		// Add all required field
		insert objEvent;

		System.assertEquals(objEvent.Id, [SELECT Most_Recent_Event_Id__c FROM Contact WHERE Id =: objContact.Id].Most_Recent_Event_Id__c);
		
		Event objEvent2 = new Event();
		objEvent2.Type = 'Consult';
		objEvent2.Description = 'Test Desc'; 
		objEvent2.whoId = objContact.Id;
		objEvent2.StartDateTime = System.today();
		objEvent2.EndDateTime = System.today();
		objEvent2.Subject  = 'Test Event';
		
		// Add all required field
		insert objEvent2;
		System.assertEquals(objEvent2.Id, [SELECT Most_Recent_Event_Id__c FROM Contact WHERE Id =: objContact.Id].Most_Recent_Event_Id__c);
		
		update objEvent;
	}
	
}


Let us know if this will help you
 
MS123456MS123456
Hi amit can u try my trigger in handler cls....
Amit Chaudhary 8Amit Chaudhary 8
try to update your code like below

Handler class like below
public with sharing class EventTriggerHandler 
{
    public void OnAfterInsert( List<Event> newEvent)
    {
        updateContactOnInsert(newEvent);
    }

    public void OnAfterUpdate( List<Event> newEvent, List<Event> oldEvent, Map<ID, Event> newEventMap , Map<ID, Event> oldEventMap )
    {
        updateContactOnUpdate(newEvent,oldEventMap);
    }

	public void updateContactOnUpdate( List<Event> newEvent , Map<ID, Event> oldEventMap )
    {
		for(Event e : newEvent)
        {
			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 : newEvent)
		{
			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;
		}
	}
	
	public void updateContactOnInsert( List<Event> newEvent)
    {
	
		Set<Id> set_ContactId = new Set<Id>();
		List<Contact> conList = new List<Contact>(); 
		for(Event e : newEvent )
		{
			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 : newEvent )
		{
           
            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());
            }
        }
		
        if(conList!=null && conList.size()>0)
		{
             update conList;
		}	
	}
}



Trigger
trigger eventTriggerContact on Event (after insert, after update) {

	EventTriggerHandler handler = new EventTriggerHandler();

	If(Trigger.isAfter)
	{
		if(Trigger.isInsert)
		{
			handler.OnAfterInsert(trigger.New);
		}
		else if (Trigger.isUpdate)
		{
			handler.OnAfterUpdate(trigger.New ,trigger.Old,Trigger.NewMap,Trigger.OldMap);
		}
	}

}

Trigger Best Practices | Sample Trigger Example | Implementing Trigger Framework
http://amitsalesforce.blogspot.com/2015/06/trigger-best-practices-sample-trigger.html

Let us know if this will help you
 
This was selected as the best answer