0

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 records that have been updated since I made this trigger.

Here is my trigger:

 trigger RequirementTrigger on Requirement__c (before insert, before update) {

    RequirementTriggerHandler.calculerDelai(Trigger.new);

}

Here is my updated 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;
            }


        }
    }

}

}

  • Are you sure it is working for the old records ? Looking at the code, it will work only when you insert the record i.e. on before insert and not when you update the record i.e it won't work in before update context because of this condition oldMap == null – Vijay Ganji Mar 14 at 10:20
  • thank you for your reply ! I removed the oldMap == null and it still doesn't update the new records. And it does work on old records – tiph Mar 14 at 10:39
0

It doesn't work on updates because of oldMap == null. If you want it to work on insert and update, remove that condition from your if statement. Also, it may not be working as you expect because you're using if-else-if-else-if, so only one of the three possible fields can be updated at once. I suspect you may want to remove the "else" conditions. Finally, the BusinessHoursTMA variable can't be null (because you would get a QueryException error in your static block if this could be true).

You can also optimize your code by combining common conditions:

    for(Requirement__c rq : newRequirement){
        if(rq.Date_d_but_statut_DMR__c != NULL)
            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;
            }
            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;
            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;

            }
        }
    }
  • Thank you for your quick reply ! I removed oldMap == null from my if statement and replace the else-if by if but it still doesn't update the new records... – tiph Mar 14 at 10:36
  • after some test, your solution does work ! thank you very much – tiph Mar 15 at 10:25
0

The trigger seems fine to me. I would suggest either some debug statements throughout your code:
ex: System.debug('Updating: Delai_DMC__c to ' + rq.Delai_DMC__c);

Then create a new record and check the log to get a better understanding of the reason the expected result is not achieved.

You can also make a test class/method that inserts a Requirement__c record. After you run the test, you can open your RequirementTriggerHandler class in the dev console and click on Code Coverage (top left corner). This will highlight which lines of code have been run in your test. This sometimes helps me pinpoint the line which I need to fix.

enter image description here

for ex: I see at line 504 that the if statement was evaluated to be false, so line 505 was not executed.

enter image description here

I know its not a direct answer to your question, but hopefully with a little work you will be able to get this working correctly.

  • thank you. I have a test class with 100% coverage, and I also use some debug statements. The only issue I've got is with records that have been updated since I've made this trigger. The field that should be updated by the trigger isn't and I really don't understand why – tiph Mar 14 at 16:19
  • So if I understand correctly, your functionality works on the insertion of new Requirement__c records, but not when you update existing ones? Perhaps there is some conflicting logic elsewhere?(i.e, another trigger / process on Requirement__c) did the field values you use in the .diff() change in the update as well? – Z33dawg Mar 14 at 18:28
  • hi, it works now if I need to update existing or new records with the change I've made. Now I just need to do a batch to update existing records that were updated before I fixed the trigger – tiph Mar 15 at 11:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.