You need to sign in to do that
Don't have an account?
Tiph
Before trigger doesn't update field on new records
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:
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 !
You code is looks good to me. Trigger is executing on before insert and before update event. Can you create new record with all the condition required.
It will work.
Thanks for your reply. I'll try to do that then. But is it normal that the records that have been updated since I made the trigger are not impacted by my trigger ? How can my trigger also update them ?
No those are not formula fields. There are date fields and my trigger need to update a number field.