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
Preeti ShettyPreeti Shetty 

Update a number field based on a date/ time field

I have a custom Object with field "date/time of call" I want another number field to populate based on the time in this field irrespective of the date. For eg - Date / time of Call is 26/6/2016 8.00 AM it should populate 1 for 9 am - 2 10am - 3 .... 5 pm - 10 and so on Attached below for your reference -
User-added image

So if the Date/Time field is 26/5/2016 4.30 PM , Time of Day field should return 9.
Best Answer chosen by Preeti Shetty
Nayana KNayana K
class :

public class ContactTriggerHandler 
{
    private Map<Integer,Integer> mapTimeToTimeOfDay = new Map<Integer,Integer>{6 => 1, 7 => 1, 8 => 1, 9 => 2, 10 => 3, 11 => 4,
                                                                                12 => 5, 13 => 6, 14 => 7, 15 => 8, 16 => 9, 17 => 10, 18 => 11
                                                                                };
    

    public ContactTriggerHandler()
    {
        
    }
    
    public void onBeforeInsert(List<Contact> lstNewContact)
    {
        populateTimeOfDay(new Map<Id, Contact>(), lstNewContact);
    }
    
    public void onBeforeUpdate(Map<Id, Contact> mapOldContact, Map<Id, Contact> mapNewContact)
    {
        populateTimeOfDay(mapOldContact, mapNewContact.values());
    }
    
    private void populateTimeOfDay(Map<Id, Contact> mapOldContact, List<Contact> lstNewContact)
    {
        Integer intHour;
        for(Contact objContact : lstNewContact)
        {
            system.debug('====objContact.date_time_of_call__c==='+ objContact.date_time_of_call__c.hour());
            
            /**On insert 
             * OR
             * On update but if only date_time_of_call__c is changed.
             * **/
            if( objContact.date_time_of_call__c != null 
                    && 
                    (
                        Trigger.isInsert 
                            || 
                        (Trigger.isUpdate && objContact.date_time_of_call__c != mapOldContact.get(objContact.Id).date_time_of_call__c)
                    )
                )
            {
                 intHour = objContact.date_time_of_call__c.hour();
                 
                 if(mapTimeToTimeOfDay.containsKey(intHour))
                 {
                     objContact.Time_of_day__c = mapTimeToTimeOfDay.get(intHour);
                 }
                 
                 // after 7PM or before 6AM ====> 12
                 else if(intHour > 18 || intHour < 6)
                 {
                     objContact.Time_of_day__c = 12;
                 }
                 else
                 {
                     objContact.Time_of_day__c = null;
                 }
                 
            }
            else if(objContact.date_time_of_call__c == null)
            {
                objContact.Time_of_day__c = null;
            }
        }
    }
    
}

trigger :
trigger ContactTrigger on Contact (before insert, before update) 
{
    ContactTriggerHandler objHandler = new ContactTriggerHandler();
    
    if(Trigger.isBefore)
    {
        if(Trigger.isInsert)
        {
            objHandler.onBeforeInsert(Trigger.new);
        }
        else if(Trigger.isUpdate)
        {
            objHandler.onBeforeUpdate(Trigger.oldMap, Trigger.newMap);
        }
    }
}

 

All Answers

Nayana KNayana K
class :

public class ContactTriggerHandler 
{
    private Map<Integer,Integer> mapTimeToTimeOfDay = new Map<Integer,Integer>{6 => 1, 7 => 1, 8 => 1, 9 => 2, 10 => 3, 11 => 4,
                                                                                12 => 5, 13 => 6, 14 => 7, 15 => 8, 16 => 9, 17 => 10, 18 => 11
                                                                                };
    

    public ContactTriggerHandler()
    {
        
    }
    
    public void onBeforeInsert(List<Contact> lstNewContact)
    {
        populateTimeOfDay(new Map<Id, Contact>(), lstNewContact);
    }
    
    public void onBeforeUpdate(Map<Id, Contact> mapOldContact, Map<Id, Contact> mapNewContact)
    {
        populateTimeOfDay(mapOldContact, mapNewContact.values());
    }
    
    private void populateTimeOfDay(Map<Id, Contact> mapOldContact, List<Contact> lstNewContact)
    {
        Integer intHour;
        for(Contact objContact : lstNewContact)
        {
            system.debug('====objContact.date_time_of_call__c==='+ objContact.date_time_of_call__c.hour());
            
            /**On insert 
             * OR
             * On update but if only date_time_of_call__c is changed.
             * **/
            if( objContact.date_time_of_call__c != null 
                    && 
                    (
                        Trigger.isInsert 
                            || 
                        (Trigger.isUpdate && objContact.date_time_of_call__c != mapOldContact.get(objContact.Id).date_time_of_call__c)
                    )
                )
            {
                 intHour = objContact.date_time_of_call__c.hour();
                 
                 if(mapTimeToTimeOfDay.containsKey(intHour))
                 {
                     objContact.Time_of_day__c = mapTimeToTimeOfDay.get(intHour);
                 }
                 
                 // after 7PM or before 6AM ====> 12
                 else if(intHour > 18 || intHour < 6)
                 {
                     objContact.Time_of_day__c = 12;
                 }
                 else
                 {
                     objContact.Time_of_day__c = null;
                 }
                 
            }
            else if(objContact.date_time_of_call__c == null)
            {
                objContact.Time_of_day__c = null;
            }
        }
    }
    
}

trigger :
trigger ContactTrigger on Contact (before insert, before update) 
{
    ContactTriggerHandler objHandler = new ContactTriggerHandler();
    
    if(Trigger.isBefore)
    {
        if(Trigger.isInsert)
        {
            objHandler.onBeforeInsert(Trigger.new);
        }
        else if(Trigger.isUpdate)
        {
            objHandler.onBeforeUpdate(Trigger.oldMap, Trigger.newMap);
        }
    }
}

 
This was selected as the best answer
Nayana KNayana K
I have also tried with formula number field :

CASE(
VALUE(LEFT(RIGHT(text( CreatedDate ),9),2)),
6,1,
7,1,
8,1,
9,2,
10,3,
11,4,
12,5,
13,6,
14,7,
15,8,
16,9,
17,10,
18,11,
12
)

But I don't know how it will react with timezone.

Referring this link I found trigger is usefull than formula since manual effort is need in forula to adjust the timezone : 
http://salesforce.stackexchange.com/questions/17947/date-is-showing-a-different-date-than-the-date-time-field

But hour() method in apex returns value in local time zone which is usefull here.