function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mani Kandan01Mani Kandan01 

how to get input value using apex trigger

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; 
                        }
                    }                 
                }
            }   
        }
    }
} //end BeforeUpdate
    
    
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;   
}
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 
Daniel GrabowskiDaniel Grabowski
Hi Mani,

It sounds like you are changing the value in shipDate on the record, and want to get the new value in a before trigger?

Ex: Date startDate = product.Opportunity.shipDate__c;

You are getting the old value because you are querying existing records in productList.  You can retrieve the new value from Trigger.newMap.

Date startDate =Trigger.newMap.get(product.OpportinityId).shipDate__c;

Good luck, 
Daniel
AbhishekAbhishek (Salesforce Developers) 
Look in the documentation for Trigger.New.  That is where you have access to the data in the inserted records.
There are some great samples in the doc
Mani Kandan01Mani Kandan01
Hai daniel thanks for the reply. 

when i use
Date startDate =Trigger.newMap.get(product.OpportinityId).shipDate__c;

it throws me an error called Variable does not exist: shipDate__c