• Tiph
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
Hi, I'm new to apex and trigger and have an issue. I need to update a field on a record when 2 other fields are updated. This field is supposed to calculate the time between 2 dates based on the business hours. My trigger work with old records but not with record that are newly updated.
Here is my trigger:
trigger RequirementTrigger on Requirement__c (before insert, before update) {
    RequirementTriggerHandler.calculerDelai(Trigger.new);
}
Here is my handler:
public with sharing class RequirementTriggerHandler {

private static String businessHoursTMA;

//Récupère BusinessHours TMA
static{
    BusinessHours businessHours = [SELECT Id FROM BusinessHours WHERE isDefault = true];
    businessHoursTMA = businessHours.Id;

}

//Calcul délais entre changement de statuts en heure
public static void calculerDelai(List<Requirement__c> newRequirement){
    for(Requirement__c rq : newRequirement){
        if(rq.Date_d_but_statut_DMR__c  != NULL){
            // Délai moyen de prise en charge DMC (Date début "statut DMR"– Date début prise en charge) en heures  
            if(rq.Date_debut_prise_en_charge__c != NULL){
                rq.Delai_DMC__c = BusinessHours.diff(businessHoursTMA,
                                                 rq.Date_debut_prise_en_charge__c,
                                                 rq.Date_d_but_statut_DMR__c)/ 3600000;
            }
            // Délai moyen de qualification/assignation DMA (Date début "statut DMR"– Date début "statut DMA") en heures
            if(rq.Date_d_but_statut_DMA__c != NULL){
                rq.Delai_DMA__c = BusinessHours.diff(businessHoursTMA, 
                                                 rq.Date_d_but_statut_DMA__c, 
                                                 rq.Date_d_but_statut_DMR__c)/ 3600000;
            }
            // Délai moyen de réalisation DMR (Date de résolution du ticket – Date début "statut DMR") en heures
            if(rq.Date_de_resolution_ticket__c != NULL){
                 rq.Delai_de_realisation__c = BusinessHours.diff(businessHoursTMA, 
                                                            rq.Date_d_but_statut_DMR__c,
                                                            rq.Date_de_resolution_ticket__c)/ 3600000;
            }


        }
    }

}
Hope you can help me, thank you ! 
  • March 14, 2019
  • Like
  • 0
Hi, I'm new to apex and trigger and have an issue. I need to update a field on a record when 2 other fields are updated. This field is supposed to calculate the time between 2 dates based on the business hours. My trigger work with old records but not with record that are newly updated.
Here is my trigger:
trigger RequirementTrigger on Requirement__c (before insert, before update) {
    RequirementTriggerHandler.calculerDelai(Trigger.new);
}
Here is my handler:
public with sharing class RequirementTriggerHandler {

private static String businessHoursTMA;

//Récupère BusinessHours TMA
static{
    BusinessHours businessHours = [SELECT Id FROM BusinessHours WHERE isDefault = true];
    businessHoursTMA = businessHours.Id;

}

//Calcul délais entre changement de statuts en heure
public static void calculerDelai(List<Requirement__c> newRequirement){
    for(Requirement__c rq : newRequirement){
        if(rq.Date_d_but_statut_DMR__c  != NULL){
            // Délai moyen de prise en charge DMC (Date début "statut DMR"– Date début prise en charge) en heures  
            if(rq.Date_debut_prise_en_charge__c != NULL){
                rq.Delai_DMC__c = BusinessHours.diff(businessHoursTMA,
                                                 rq.Date_debut_prise_en_charge__c,
                                                 rq.Date_d_but_statut_DMR__c)/ 3600000;
            }
            // Délai moyen de qualification/assignation DMA (Date début "statut DMR"– Date début "statut DMA") en heures
            if(rq.Date_d_but_statut_DMA__c != NULL){
                rq.Delai_DMA__c = BusinessHours.diff(businessHoursTMA, 
                                                 rq.Date_d_but_statut_DMA__c, 
                                                 rq.Date_d_but_statut_DMR__c)/ 3600000;
            }
            // Délai moyen de réalisation DMR (Date de résolution du ticket – Date début "statut DMR") en heures
            if(rq.Date_de_resolution_ticket__c != NULL){
                 rq.Delai_de_realisation__c = BusinessHours.diff(businessHoursTMA, 
                                                            rq.Date_d_but_statut_DMR__c,
                                                            rq.Date_de_resolution_ticket__c)/ 3600000;
            }


        }
    }

}
Hope you can help me, thank you ! 
  • March 14, 2019
  • Like
  • 0