0

Hi Everyone help me to get output.

WHERE shipDate__c >= :startDate AND return_date__c <= :endDate

In the above code startDate & endDate retrives the data (already Inserted Record) from db. for eg: startDate = 20-03-2021 endDate = 16-04-2021

WHERE shipDate__c >= :20-03-2021 AND return_date__c <= :16-04-2021

but what i need is, While editing the record by changing startDate, it retrives the entered data before saving in db. for eg: startDate = 26-03-2021

WHERE shipDate__c >= :26-03-2021 AND return_date__c <= :16-04-2021

Hope someone will help

Thank you

public void   onBeforeUpdate(List<Opportunity> newMap){
    Set<String> oppIds = new Set<String>();
    for(Opportunity opp: newMap){
        oppIds.add(opp.id);
    }
    
    if(oppIds.size() > 0 && oppIds != null){
       //get all record of product with  Opp Id
        List<OpportunityLineItem> productList =  [SELECT Product2.m_ProductGroup__r.Name, OpportunityId, Opportunity.shipDate__c, 
                                                  Opportunity.return_date__c FROM OpportunityLineItem
                                                  WHERE OpportunityId IN :oppIds
                                                  AND IsDeleted = false              
                                                 ];  
        if(productList.size() > 0 && productList !=null){ 
            for(OpportunityLineItem product: productList){
                totalUsed = 0;
                String name =  product.Product2.m_ProductGroup__r.Name;
                Date startDate = product.Opportunity.shipDate__c;
                Date endDate = product.Opportunity.return_date__c;
                
                if(name != ''){
                    totalUsed  = getSum(name, startDate, endDate);
                    if( totalUsed <= 30 ){ 
                        for(Opportunity opp: newMap) { 
                            opp.m_enableproduct__c = true; 
                        }
                    }                 
                }
            }   
        }
    }
} 
   
private Decimal getSum(String productName, Date startDate, Date endDate){
    Decimal sum = 0;        
    List<Opportunity> dataList = new List<Opportunity>();
    List<OpportunityLineItem> productList = new List<OpportunityLineItem>();
    dataList = [SELECT id, shipDate__c, return_date__c FROM Opportunity WHERE shipDate__c >= :startDate AND return_date__c <= :endDate];
    if(dataList != null  && dataList.size() > 0){
        for(Opportunity opp :dataList){
            productList = [SELECT Quantity FROM OpportunityLineItem
                           WHERE OpportunityId =:opp.id  
                           AND Product2.m_ProductGroup__r.Name =: productName
                           AND IsDeleted = false 
                          ];                
            if(productList != null && productList.size() > 0 ){
                for(OpportunityLineItem addTemp :productList){
                    sum += addTemp.Quantity;         
                }  
            }
        }           
        system.debug('sum' + sum);
    }
    return sum;   
}
  • You cannot query the records being inserted in a `before insert` trigger. They are not yet committed to the database. – David Reed Mar 07 '21 at 19:06
  • If you are trying to compare at the data before and after the update you likely will be able to find what you want in new and old context variables. Try describing what you want to do with the data once you have and it might be easier to help. – acrosman Mar 18 '21 at 02:41

0 Answers0