0

I have implemented a delivery delay for some of the opportunity line Item based upon the product family and opportunity close date.

But now the business wants if a user changes the close date from 8-03-2021 to 28 - 3- 2021 then the planned delivery date in Opportunity line items each Items Planned delivery date should automatically update with + 20 days. for ex if Planned delivery is 18-03-2021 it should automatically update to 7-04-2021.

Please find my code below.

public class HLP01_OPPLI {

    public static void updateQuantity(List<OpportunityLineItem> OppLI ){
        system.debug('-- Trigger OppLI aftIns --');
        system.debug(OppLI);
        
        Set<OpportunityLineItem> OpplineItemUPD= new Set<OpportunityLineItem>();        
        List<string> listOppId=new List<string>();   
        List<string> listOppliId=new List<string>(); 
        
        
        // List opp id        
        for(OpportunityLineItem OppLIIter:OppLI){            
            listOppId.add(OppLIIter.OpportunityId);
            listOppliId.add(OppLIIter.id);
        }        
        // eliminer les Oppli du CPQ (family == null)
        List<OpportunityLineItem> oppliFamily = [select id,Product2.Family,opportunityId, quantity from OpportunityLineItem where id in: listOppliId AND Product2.Family != null];
        system.debug('list Opportunity oppliFamily ' +oppliFamily);
        system.debug('lancre2'+listOppId.size());
        List<Opportunity> ListOppContract = [SELECT id, TECH_Parent_Opp_contract__c ,
                                            (SELECT id, quantity, OpportunityID FROM OpportunityLineItems Where Product2.Family='Contracts')
                                            FROM Opportunity where TECH_Parent_Opp_contract__c in:listOppId and TECH_Contract_Opp_Created__c=true];
        
        system.debug('list Opportunity contract '+ListOppContract);
        
        for(OpportunityLineItem oppIter : oppliFamily){
            for(Opportunity oppContract : ListOppContract){
                if(oppIter.opportunityId == oppContract.TECH_Parent_Opp_contract__c){
                    List<OpportunityLineItem> LineItem = oppContract.OpportunityLineItems;
                    if(!LineItem.isempty()){
                        LineItem[0].quantity=oppIter.quantity;
                        OpplineItemUPD.add(LineItem[0]);
                    }
                }
            }            
        }
        system.debug(OpplineItemUPD); 
        update new List<OpportunityLineItem>(OpplineItemUPD);
    }
    
    public static void updatePlannedDate(List<OpportunityLineItem> OppLI){
        
        List<Id> lstPrdId = new List<Id>();
        List<Id> lstOPPId = new List<Id>();        
        For(OpportunityLineItem oneOppli : OppLI){
            lstPrdId.add(oneOppli.Product2Id);
            lstOPPId.add(oneOppli.OpportunityId);
        }
        
        // Map Product
        Map<id,Product2> mapPrd2=new Map<id,Product2>([Select id, Level_1_family__c from Product2 where id in: lstPrdId]);
        // Map OPP
        Map<id,Opportunity> mapOPP=new Map<id,Opportunity>([Select id, CloseDate from Opportunity where id in: lstOPPId]);
        
        For(OpportunityLineItem oneOppliF : OppLI){
            If(oneOppliF.ServiceDate == null){
                // Delay
                oneOppliF.ServiceDate = mapOPP.get(oneOppliF.Opportunityid).closedate + getDeliveryDelay(mapPrd2.get(oneOppliF.Product2Id).Level_1_family__c);
            }
        }        
    }
    
    public static Integer getDeliveryDelay(String productFamily) {
        if      (productFamily == null || productFamily == '')  return 42;
        else if (productFamily == 'Trailer')                    return 28;
        else if (productFamily == 'Diesel Truck')               return 28;
        else if (productFamily == 'Engineless')                 return 42;
        else if (productFamily == 'Direct Drive')               return 28;
        else if (productFamily == 'Contracts')                  return 0;
        else if (productFamily == 'LCV')                        return 28;
        else                                                    return 15;
    }
}

Please advise how I can Implement this.

Thanks

2
  • Does you HLP01_OPPLI static methods get called from a Trigger or another service?
    – TSmith
    Mar 8, 2021 at 11:54
  • @TSmith yes in a trigger Mar 8, 2021 at 11:56

1 Answer 1

0

When called from a Trigger, you can either pass the old and new context directly to your static methods or do the work elsewhere, up to you. In short, you should look to calculate the difference in days against the old and new CloseDate.

Integer difference = Trigger.oldMap.get(opportunityLineItem.Id).CloseDate.daysBetween(Trigger.newMap.get(opportunityLineItem.Id).CloseDate);

You could refactor the signature on updatePlannedDate() to accept a Map or else separate your concerns by keeping the existing method for inserts and a new method for updates (extensions due to Users modifying the CloseDate).

If extension, you can use Date.addDays()

updatedLineItem.ServiceDate = updatedLineItem.ServiceDate.addDays(difference);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .