• Mani Kandan01
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 2
    Replies
Hai all

i use this code to get the input value 
Date startDate1 = Trigger.newMap.get(product.OpportunityId).startDate__c;

it display the below error
Method does not exist or incorrect signature: void get(Date) from the type Map<Id,SObject>

but when using this it returns corresponding id.
String startDate1 = Trigger.newMap.get(product.OpportunityId).Id;

thanks in advance.
 
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 

Hai all hope everyone fine.

I need to get the input value of Date field from opportunity object using apex trigger before insert. (No need to insert record).

hope someone will help me. 

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

Hai all hope everyone fine.

I need to get the input value of Date field from opportunity object using apex trigger before insert. (No need to insert record).

hope someone will help me. 

Thank You