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
Lise-Marie DECLOUXLise-Marie DECLOUX 

Date to exclude holidays

Hi

We have a trigger which calculate a date without weekend. And now we hae a requirement to exclude also the holidays.  I've entered holidays days in the setup but i'm a bit lost on how to integrate it on our trigger.
Could someone help me on it ?

Thanks

Here is the trigger:

trigger TOKD_T13_Case_Before_Insert on Case (before insert) {
    System.debug('[TOKD_T13_Case_Before_Insert] Start Trigger insert Case before : ' + trigger.new );

    List<TOKD_Type_de_demande__c> listTypeDemande = [select Id, SLA_Unite__c, SLA__c, TOKD_Validation_Metier__c from TOKD_Type_de_demande__c];
    Map<Id, TOKD_Type_de_demande__c> mapTypeDemande = new Map<Id, TOKD_Type_de_demande__c>();
    
    for (TOKD_Type_de_demande__c typeDemande : listTypeDemande){
        mapTypeDemande.put(typeDemande.Id, typeDemande);
    }
    
   User currentUser = [SELECT Id, Name, Contact.TOKD_CONT_Valideur_N_1__c, Contact.TOKD_Valideur__c  
                                FROM User WHERE Id = :UserInfo.getUserId() limit 1]; 
    
    User currentValideurN1 = [SELECT Id, Name, Contact.TOKD_CONT_Valideur_N_1__c, Contact.TOKD_Valideur__c  
                                FROM User WHERE contactId = :currentUser.Contact.TOKD_CONT_Valideur_N_1__c limit 1]; 
    
    for (Case caseTraitement : trigger.new)  {
        integer delai = 0;
        double boucle = 1;
        boolean needValidationN1 = false;
        if (caseTraitement != null){
            system.debug('[TOKD_T13_Case_Before_Insert] idTypeDemande : '+caseTraitement.TOKD_Type_de_demande__c);

            TOKD_Type_de_demande__c requestType = mapTypeDemande.get(caseTraitement.TOKD_Type_de_demande__c);
            
            if(requestType.TOKD_Validation_Metier__c && !currentUser.Contact.TOKD_Valideur__c){
                needValidationN1 = true;
            }       

            System.debug('[TOKD_T13_Case_Before_Insert] UniteTypeDemande : ' + requestType.SLA_Unite__c);
            try{
                if (requestType.SLA_Unite__c == 'J'){
                    boucle = requestType.SLA__c+1;
                }
                else if (requestType.SLA_Unite__c == 'H'){
                    delai = Integer.valueOf(requestType.SLA__c);
                    boucle = 1;
                }
            }
            catch(Exception e){
                boucle = 1;
                delai = 0;
            }
        } 
        
                   
        datetime date_realisation_souhaitee = System.now().date();
        for (double i = 0; i < boucle; i++){
            date_realisation_souhaitee = date_realisation_souhaitee.addHours(24);
            if(date_realisation_souhaitee.format('u') == '6' || date_realisation_souhaitee.format('u') == '7'){
                boucle++;
            }  
        }
            
        datetime date_debut_souhaitee = System.now().date();
        for (double i = 0; i < 1; i++){
            date_debut_souhaitee = date_debut_souhaitee.addHours(24);
            if(date_debut_souhaitee.format('u') == '6' || date_debut_souhaitee.format('u') == '7'){
                i--;
            }
        }
        
        date_realisation_souhaitee = date_realisation_souhaitee.addHours(delai);
        if(date_realisation_souhaitee.format('u') == '7'){
            date_realisation_souhaitee = date_realisation_souhaitee.addHours(24);
        }             
        else if (date_realisation_souhaitee.format('u') == '6'){
            date_realisation_souhaitee = date_realisation_souhaitee.addHours(48);
        }
       
        Id validateurN1Id = currentUser.Contact.TOKD_CONT_Valideur_N_1__c;
        
        System.debug('[TOKD_T13_Case_Before_Insert] ValidationMetier : ' + needValidationN1);
        System.debug('[TOKD_T13_Case_Before_Insert] validateur N+1 : ' + validateurN1Id);
        System.debug('[TOKD_T13_Case_Before_Insert] date_debut_souhaitee : ' + date_debut_souhaitee);
        System.debug('[TOKD_T13_Case_Before_Insert] date_realisation_souhaitee : ' + date_realisation_souhaitee);
        
        caseTraitement.TOKD_Date_debut_souhaitee__c = date.valueOf(date_debut_souhaitee);
        caseTraitement.TOKD_Date_realisation_souhaitee__c = date.valueOf(date_realisation_souhaitee);
        
        caseTraitement.TOKD_REQ_Validation_Metier__c = needValidationN1;
        if (needValidationN1){
            caseTraitement.TOKD_REQ_Valideur_N1__c = validateurN1Id;
            caseTraitement.TOKD_Ancien_valideur_N_1__c = validateurN1Id;   
            caseTraitement.TOKD_REQ_Valideur_N_1__c = currentValideurN1.id;
        }

    }
}
Lise-Marie DECLOUXLise-Marie DECLOUX
Nobody for helping ?