0

I have written the trigger for ignoring the weekends and holidays from holiday__c custom object based on the offset value of account

Trigger

trigger DeliveryDateUpdation on orderitem(before insert) {
    Map<Id, Order> orders = new Map<Id, Order>();
    for(OrderItem record: Trigger.new) {
        orders.put(record.OrderId, null);
    }
    orders.putAll([SELECT Id,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');
        }
    }
    if(markets.size()<=0) {return;}
    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 && (orderRecord.SubType__c == 'Offer'|| orderRecord.SubType__c == 'ASP')) 
        {
            String market = orderRecord.SubType__c == 'Offer'? 'FR': 'IT';
            Date UpdatedDeliverydate = Date.today();
            integer Offsetvalue;
            if(orderRecord.Account.JDE_Date_Delivery_Offset__c!= null) {
                Offsetvalue = orderRecord.Account.JDE_Date_Delivery_Offset__c.intValue();
            }
            if(orderRecord.Account.JDE_Date_Delivery_Offset__c == null && orderRecord.SubType__c == 'Offer') {
                Offsetvalue = 5;
            }
            if(orderRecord.Account.JDE_Date_Delivery_Offset__c == null  && orderRecord.SubType__c == 'ASP') {
                Offsetvalue = 3;
            } 
            while(Offsetvalue > 0) {
                UpdatedDeliverydate = UpdatedDeliverydate.addDays(1);
                DateTime UpdatedDeliverydatetime = DateTime.newInstance(UpdatedDeliverydate , midnight);
                String dayName = UpdatedDeliverydatetime.format('EEEE');
                if(dayName != 'Saturday' && dayName != 'Sunday' && !holidays.get(market).containsKey(UpdatedDeliverydate)) {
                    Offsetvalue --; 
                }      
            }
            if(orderRecord.OrderDeliveryDate__c < UpdatedDeliverydate)
                record.Delivery_Date__c = UpdatedDeliverydate;
        }
    }
}

Test class

@isTest
private class DeliveryDateUpdationtest{
    static testMethod void DeliveryDateUpdation()
    {
        account[] testaccount1 = new account[0];
        testAccount1.add(new Account(Name='Test',Type='Test',Status__c = 'Draft',Call_Status__c = 'called On',JDE_Date_Delivery_Offset__c = ''));
        testAccount1.add(new Account(Name='Test',Type='Test',Status__c = 'Draft',Call_Status__c = 'called On',JDE_Date_Delivery_Offset__c = '2'));
        insert testAccount1;
        Product2 testProduct = new Product2(Name='Test', IsActive=true);
        insert testProduct;
        Pricebook2 testPriceBook = new Pricebook2(Name='Test', IsActive=true);
        insert testPriceBook;
        PricebookEntry[] testPricebookEntries = new PricebookEntry[0];
        testPricebookEntries.add(new PricebookEntry(Pricebook2Id=Test.getStandardPricebookId(), UnitPrice=4.99, Product2Id=testProduct.Id));
        testPricebookEntries.add(new PricebookEntry(Pricebook2Id=testPriceBook.Id, UnitPrice=4.99, UseStandardPrice=false, Product2Id=testProduct.Id));
        insert testPricebookEntries;
        order[] order1 = new order[0];
        order1.add(new Order(AccountId = testAccount.id, EffectiveDate = '29/3/2017',Status = 'Draft',Shipping_Urgency__c = 'high',Subtype__c = 'offer'));
        order1.add(new order(AccountId = testAccount.id, EffectiveDate = '29/3/2017',Status = 'Draft',Shipping_Urgency__c = 'high',Subtype__c = 'ASP'));
        order1.add(new order(AccountId = testAccount.id, EffectiveDate = '29/3/2017',Status = 'Draft',Shipping_Urgency__c = 'high',Subtype__c = 'Normal'));
        insert order1;
        Holiday__c holiday1 = new Holiday__c[0];
        holiday1.add(new Holiday__c(Date__c = '29/3/2017',Country__c = 'FR'));
        holiday1.add(new Holiday__c(Date__c = '29/3/2017',Country__c = 'IT'));
        insert holiday1;

How to insert the orderitem for checking three scenarios for order subtype as offer,Asp and Normal and then how to proceed with the test class.whether i have to insert user object for this test class?please anyone guide me for the logic

2
  • 1
    Indent your code please ! – benahm Mar 29 '17 at 8:50
  • You should use if...else if ... construct for these if statements if(record.Subtype__c == 'Offer') {markets.add('FR');} if(record.Subtype__c == 'ASP') {markets.add('IT');} – itzmukeshy7 Mar 29 '17 at 8:52
2

First, you have to link a pricebook to the orders you're inserting:

Date dateDelivery = Date.today();
order1.add(new Order(AccountId = testAccount.id, EffectiveDate = '29/3/2017',Status = 'Draft',Shipping_Urgency__c = 'high',Subtype__c = 'offer', Pricebook2Id=testPriceBook.Id,OrderDeliveryDate__c=dateDelivery));
order1.add(new order(AccountId = testAccount.id, EffectiveDate = '29/3/2017',Status = 'Draft',Shipping_Urgency__c = 'high',Subtype__c = 'ASP', Pricebook2Id=testPriceBook.Id,OrderDeliveryDate__c=dateDelivery));
order1.add(new order(AccountId = testAccount.id, EffectiveDate = '29/3/2017',Status = 'Draft',Shipping_Urgency__c = 'high',Subtype__c = 'Normal', Pricebook2Id=testPriceBook.Id,OrderDeliveryDate__c=dateDelivery));

Then, just insert some order items with the different sub types and referencing a pricebook entry linked to the same pricebook as the order:

OrderItem orderItemOffer = new OrderItem();
orderItemOffer.PricebookEntryId = testPricebookEntries[1].Id;
orderItemOffer.OrderId = order1[0].Id;
orderItemOffer.Quantity = 2; //set your desired quantity
orderItemOffer.UnitPrice = 50; //set your desired unit price
orderItemOffer.Delivery_Date__c = dateDelivery;

insert orderItemOffer;

OrderItem orderItemASP = new OrderItem();
orderItemASP.PricebookEntryId = testPricebookEntries[1].Id;
orderItemASP.OrderId = order1[1].Id;
orderItemASP.Quantity = 2; //set your desired quantity
orderItemASP.UnitPrice = 50; //set your desired unit price
orderItemASP.Delivery_Date__c = dateDelivery;

insert orderItemASP;

Note

Don't forget to add some assertions at the end of your test to check that your trigger works as expected.

1

Your Answer

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