0

嗨,大家帮我获得输出。

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

在上面的代码中, startDate 和 endDate 从 db 中检索数据(已经插入的记录)。例如:startDate = 20-03-2021 endDate = 16-04-2021

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

但我需要的是,在通过更改 startDate 编辑记录时,它会在保存到数据库之前检索输入的数据。例如:开始日期 = 26-03-2021

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

希望有人会帮助

谢谢

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;   
}
4

0 回答 0