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
sumit dsumit d 

correct data is not showing in the field First_Response__c

this is my helper class:-
public class CaseTriggerHelper {
    public static List<Case> newCases = new List<Case>();
    public static List<Case> oldCases = new List<Case>();
    public static Map<Id, Case> newMapCases = new Map<Id, Case>();
    public static Map<Id, Case> oldMapCases = new Map<Id, Case>(); 
    
    public static void firstResponce(){
        
        BusinessHours defaultBH = [ SELECT Id 
                                    FROM BusinessHours 
                                    WHERE IsDefault = true  Limit : 1
                                  ];
        
       
        for(Case caseObj : newCases ){
           
            if( (caseObj.First_Response__c == Null)
                && UserInfo.getUserId() == caseObj.OwnerId ){
                     
                    Decimal result = BusinessHours.diff(defaultBH.Id, caseObj.CreatedDate, caseObj.LastModifiedDate );
                    
                    Decimal resultingHours = result/(60*60*1000);
                    caseObj.Helper_First_Responce__c= resultingHours;
                      
                    Decimal HH = resultingHours.round(System.RoundingMode.DOWN);
                   
                    Decimal tempMM = ( resultingHours - resultingHours.round(System.RoundingMode.DOWN))  * 60;
                    Decimal MM = tempMM.round(System.roundingMode.DOWN);
                    
                    Decimal SS = (( tempMM - tempMM.round(System.RoundingMode.DOWN)) * 60).round(System.roundingMode.DOWN);
                     
                    string finaltime = ''+HH+':'+MM+':'+SS;
                    caseObj.First_Response__c=finaltime;
                     
               } 
                     
        } 
         
    }    now i have a requirement to populate the time between when case is opned and its first responce in the field First_Response__c in HH:MM:SS .now the issue is when i create a new case it shows 0:0:0 in First_Response__c rather than showing some time in the field
like some seconds like0:0:12 .
how i fix this issue?
any suggestions?
Rahul.MishraRahul.Mishra
Hi Sumit,

Are you using the before trigger here, because I do not see you are updating the Case . If you will use the before trigger then CreatedDate and Lastmodified Date would be null, cause of that you are receving  0: 0: 0. Just to verify, I tried of writing sample code and checking the data using debug log and I have got the correct date time in my logs, refer the following code to update your class:
 
trigger changeAssignee on Case (after insert, after update) {

   BusinessHours defaultBH = [ SELECT Id 
                                    FROM BusinessHours 
                                    WHERE IsDefault = true  Limit : 1
                                  ];
                                  
    Map<Id, String> mapOfCaseAndTime = new Map<Id, String>();
    List<Case> lstCaseToUpdate = new List<Case>();
     
     System.debug('Business Hours Are'+defaultBH);                             
    for (Case caseObj : Trigger.New) {
            if( (caseObj.First_Response__c == Null)
                && UserInfo.getUserId() == caseObj.OwnerId ){
                     
                    Decimal result = BusinessHours.diff(defaultBH.Id, caseObj.CreatedDate, caseObj.LastModifiedDate );
                    system.debug('Result is '+result);
                    
                    Decimal resultingHours = result/(60*60*1000);
                    
                    System.debug('Resulting Hours Are '+resultingHours);
                                 //    caseObj.Helper_First_Responce__c= resultingHours;
                      
                    Decimal HH = resultingHours.round(System.RoundingMode.DOWN);
                   
                    Decimal tempMM = ( resultingHours - resultingHours.round(System.RoundingMode.DOWN))  * 60;
                    Decimal MM = tempMM.round(System.roundingMode.DOWN);
                    
                    Decimal SS = (( tempMM - tempMM.round(System.RoundingMode.DOWN)) * 60).round(System.roundingMode.DOWN);
                     
                    string finaltime = ''+HH+':'+MM+':'+SS;
                    system.debug('Final Time is'+finaltime);

Mark my anser as solved if it does help you.
sumit dsumit d
where did you use  Map<Id, String> mapOfCaseAndTime = new Map<Id, String>(); and the list?
Rahul.MishraRahul.Mishra
Sumit, I did not complete the trigger, you can use them to collect the Case date with time, later put them in list to update.
sumit dsumit d
can you send me the complete trigger?
Rahul.MishraRahul.Mishra
Here is the code:
 
trigger changeAssignee on Case (after update) {
    
    Map<Id, String> mapOfCaseAndTime = new Map<Id, String>();
    List<Case> lstCaseToUpdate = new List<Case>();
    BusinessHours defaultBH = [ SELECT Id 
                                    FROM BusinessHours 
                                    WHERE IsDefault = true  Limit : 1
                                  ];
    for (Case caseObj : Trigger.old) {
            if( (caseObj.First_Response__c == Null)
                && UserInfo.getUserId() == caseObj.OwnerId ){
                     
                    Decimal result = BusinessHours.diff(defaultBH.Id, caseObj.CreatedDate, caseObj.LastModifiedDate );
                    Decimal resultingHours = result/(60*60*1000);
                    Decimal HH = resultingHours.round(System.RoundingMode.DOWN);
                    Decimal tempMM = ( resultingHours - resultingHours.round(System.RoundingMode.DOWN))  * 60;
                    Decimal MM = tempMM.round(System.roundingMode.DOWN);
                    Decimal SS = (( tempMM - tempMM.round(System.RoundingMode.DOWN)) * 60).round(System.roundingMode.DOWN);
                    string finaltime = ''+HH+':'+MM+':'+SS;
                    mapOfCaseAndTime.put(caseObj.Id, finaltime);
                }
                
            if(!mapOfCaseAndTime.isEmpty()) {
                    lstCaseToUpdate.add(new Case(Id = caseObj.Id, Helper_First_Responce__c= resultingHours, First_Response__c = mapOfCaseAndTime.get(caseObj.Id)));
                }}
    if(!mapOfCaseAndTime.isEmpty())
     update lstCaseToUpdate;
}

 
Rahul.MishraRahul.Mishra
Helper_First_Responce__c and First_Response__c fields will only be updated when owner updates the Case record, at the time of creation of case fields will not be updated since craeted date and last modified date would be the same.
sumit dsumit d
its not showing correct time( HH:MM:SS) can you help me out for this?