2

I have written the trigger code for excluding Holidays and weekends based on the offset value of the account in salesforce and to populate the deleivery date on the order product object

Trigger code:

trigger populatedeleiverydate on orderitem(before insert) {
    Map<Id, Order> orders = new Map<Id, Order>();
    Date date1;
    for(OrderItem record: Trigger.new) {
        System.debug('Trigger::'+Trigger.new);
        orders.put(record.OrderId, null);
    }
    orders.putAll([SELECT Account.JDE_Date_Delivery_Offset__c, OrderDeliveryDate__c,Subtype__c FROM Order WHERE Id = :orders.keySet()]);
    Set<String> markets = new Set<String>();
    for(Order record: orders.values()) {
        if(record.Subtype__c == 'Offer') {
            markets.add('FR');
        }
        if(record.Subtype__c == 'ASP') {
            markets.add('IT');
        }

    }

    Map<string, Map<Date,Holiday__c>> holidays = new Map<string, Map<Date,Holiday__c>>();
    for(String market: markets) {
        holidays.put(market, new Map<Date, Holiday__c>());
    }
    for(Holiday__c record: [SELECT Date__c, Country__c FROM Holiday__c WHERE Country__c = :markets]) {
        holidays.get(record.Country__c).put(record.Date__c, record);
    }
    Time midnight = Time.newInstance(0, 0, 0, 0);
    for(OrderItem record: Trigger.new) {
        Order orderRecord = orders.get(record.OrderId);
        if(orderRecord.OrderDeliveryDate__c  == record.Delivery_Date__c)
        {
        String market = orderRecord.SubType__c == 'Offer'? 'FR': 'IT';
        Date tempDate = Date.today()+1;
        DateTime tempDateTime = DateTime.newInstance(tempDate, midnight);
        Integer remainingDays = orderRecord.Account.JDE_Date_Delivery_Offset__c.intValue();
        if(Account.JDE_Date_Delivery_Offset__c==''&& orderRecord.SubType__c == 'Offer')
        {
          remainingDays = 5;
        if(tempDateTime.format('EEEE') == 'Sunday') {
            tempDate = tempDate.addDays(-1);
        }
        while(remainingDays > 0) {
            tempDateTime = DateTime.newInstance(tempDate, midnight);
            if(tempDateTime.format('EEEE') == 'Saturday') {
                tempDate = tempDate.addDays(2);
                 continue;
            }
            if(holidays.get(market) == null || !holidays.get(market).containsKey(tempDate)) {
                remainingDays--;
            }
            tempDate = tempDate.addDays(1);

        }
        }
        if(Account.JDE_Date_Delivery_Offset__c==''&& orderRecord.SubType__c == 'ASP')
         remainingDays = 3;
        if(tempDateTime.format('EEEE') == 'Sunday') {
            tempDate = tempDate.addDays(-1);
        }
        while(remainingDays > 0) {
            tempDateTime = DateTime.newInstance(tempDate, midnight);
            if(tempDateTime.format('EEEE') == 'Saturday') {
                tempDate = tempDate.addDays(2);
                 continue;
            }
            if(holidays.get(market) == null || !holidays.get(market).containsKey(tempDate)) {
                remainingDays--;
            }
            tempDate = tempDate.addDays(1);

        }
        else
        {
        if(tempDateTime.format('EEEE') == 'Sunday') {
            tempDate = tempDate.addDays(-1);
        }
        while(remainingDays > 0) {
            tempDateTime = DateTime.newInstance(tempDate, midnight);
            if(tempDateTime.format('EEEE') == 'Saturday') {
                tempDate = tempDate.addDays(2);
                 continue;
            }
            if(holidays.get(market) == null || !holidays.get(market).containsKey(tempDate)) {
                remainingDays--;
            }
            tempDate = tempDate.addDays(1);

        }
        }

    date1 = orderRecord.OrderDeliveryDate__c;
    if(date1<=tempDate)
    record.Delivery_Date__c = tempDate;
    }
}

}

In this code,I have added some repeated conditions of code, for offsetvalue as Null for asp and Offer, remaining days we have to set the integer value for this process i have repeated this code three times

Like this:

if(Account.JDE_Date_Delivery_Offset__c==''&& orderRecord.SubType__c == 'Offer')
        {
          remainingDays = 5;
        if(tempDateTime.format('EEEE') == 'Sunday') {
            tempDate = tempDate.addDays(-1);
        }
        while(remainingDays > 0) {
            tempDateTime = DateTime.newInstance(tempDate, midnight);
            if(tempDateTime.format('EEEE') == 'Saturday') {
                tempDate = tempDate.addDays(2);
                 continue;
            }
            if(holidays.get(market) == null || !holidays.get(market).containsKey(tempDate)) {
                remainingDays--;
            }
            tempDate = tempDate.addDays(1);

        }
        }
        if(Account.JDE_Date_Delivery_Offset__c==''&& orderRecord.SubType__c == 'ASP')
         remainingDays = 3;
        if(tempDateTime.format('EEEE') == 'Sunday') {
            tempDate = tempDate.addDays(-1);
        }
        while(remainingDays > 0) {
            tempDateTime = DateTime.newInstance(tempDate, midnight);
            if(tempDateTime.format('EEEE') == 'Saturday') {
                tempDate = tempDate.addDays(2);
                 continue;
            }
            if(holidays.get(market) == null || !holidays.get(market).containsKey(tempDate)) {
                remainingDays--;
            }
            tempDate = tempDate.addDays(1);

        }
        else
        {
        if(tempDateTime.format('EEEE') == 'Sunday') {
            tempDate = tempDate.addDays(-1);
        }
        while(remainingDays > 0) {
            tempDateTime = DateTime.newInstance(tempDate, midnight);
            if(tempDateTime.format('EEEE') == 'Saturday') {
                tempDate = tempDate.addDays(2);
                 continue;
            }
            if(holidays.get(market) == null || !holidays.get(market).containsKey(tempDate)) {
                remainingDays--;
            }
            tempDate = tempDate.addDays(1);

        }
        }

Is it possible to reduce the lines of same repeated code in trigger,Is there any alternate way is there?

Any help appreciated

2

Where you build the set of market values you have:

if(record.Subtype__c == 'Offer') {
    markets.add('FR');
}
if(record.Subtype__c == 'ASP') {
    markets.add('IT');
}

But where you lookup you have:

String market = orderRecord.SubType__c == 'Offer'? 'FR': 'IT';

So if a record.Subtype__c of a value other than 'Offer' or 'ASP' would cause an NPE.

One solution is to make the above two pieces of code consistent. An uglier solution would be add a guard:

if(holidays.get(market) == null || !holidays.get(market).containsKey(tempDate)) {
0

Yeah I got the answer:

if(Account.JDE_Date_Delivery_Offset__c==''&& orderRecord.SubType__c == 'Offer')
        {
          remainingDays = 5;
        }

        if(Account.JDE_Date_Delivery_Offset__c==''&& orderRecord.SubType__c == 'ASP')
        {
         remainingDays = 3;
        }

Thank you

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.