+ Start a Discussion
SoundarSoundar 

How Can I Compare With a old map Id For Below Code

Dear Friends,

Let me know how can we compare with old map Id For Below Code

*********************************************************************************
 
trigger QuoteLineItemUpdateOnOpp on Quote (after insert,after Update) {
    
    IF(Trigger.isAfter && Trigger.isInsert){
    Set<ID> qtId = New Set<Id>();
    Set<Boolean> syn = New Set<Boolean>();
    Set<Id> OppId =  New Set<Id>();
    List<OpportunityLineItem> lItem = New  List<OpportunityLineItem>();
    for(Quote q : Trigger.new){
        qtId.add(q.Id);
        syn.add(q.Custom_Synchronize__c);
    }
    //Quote qt=trigger.new[0];
    //Quote qt = New Quote();
    IF(qtId.size() > 0){
        System.debug('Quote Size : ' + qtId.size());
       // List<Quote> qtList = [Select id,name from Quote Where ID =:qtId];
        List<Quote_Line_Item__c> qtLItem = [Select Id,name,Quantity__c,Price__c,Quotes__c,Quotes__r.Opportunity.Id, Products__c from Quote_Line_Item__c
                                           Where Quotes__c IN :qtId];
        System.debug('Quote Line Item Size : ' + qtLItem.size());
        System.debug('Checking a Quote LineItem : ' + qtLItem);
         
        //List<Opportunity> oppty = New List<Opportunity>();
        List<Opportunity> oppList = [Select id,name,CloseDate,(Select id,name,ListPrice from OpportunityLineItems) From Opportunity Where ID IN :oppId];
        
        //Update QuoteLineItem To OpportunityLine Item
        
        
        For(Quote_Line_Item__c qteLineItem  : qtLItem){
            OpportunityLineItem OppLItem = New OpportunityLineItem();
            
            OppLItem.Id = qteLineItem.Quotes__r.id;
            OppLItem.UnitPrice = qteLineItem.Price__c;
            OppLItem.Quantity = qteLineItem.Quantity__c;
            OppLItem.PricebookEntryId = qteLineItem.Products__c;
            lItem.add(OppLItem);
        }
        
        if(lItem.size() > 0){
            
            update lItem;
        }
        
    } 
}   
    
}







*******************************************


Thanks IN Advance

Regards,
SOUNDAR RAJAN P
Best Answer chosen by Soundar
SoundarSoundar
Hi Shiva Rajendran,

I had make some changes in below coding now it's working as well.. 
 
trigger QuoteLineItemUpdateOnOpp on Quote (after insert,after Update) {
    IF(Trigger.isUpdate && Trigger.isAfter){
        
        System.debug('After Update : ');
    Set<ID> qtId = New Set<Id>();
    Set<Boolean> syn = New Set<Boolean>();
    Set<Id> OppId =  New Set<Id>();
    List<OpportunityLineItem> lItem = New  List<OpportunityLineItem>();
   Set<Id> setPrId  = New Set<Id>();     
   Map<Id,Id> PrMap  = New Map<Id,Id>(); 
        
    for(Quote qId : Trigger.new){
        if( Trigger.oldMap.get( qId.Id).Custom_Synchronize__c != qId.Custom_Synchronize__c ){        
        qtId.add(qId.Id);
        syn.add(qId.Custom_Synchronize__c);
        System.debug('Old Map Id: ' + qId.Id);
         
    }
        }
       
    //Quote qt=trigger.new[0];
    //Quote qt = New Quote();
    IF(qtId.size() > 0){
        System.debug('Quote Size : ' + qtId.size());
       // List<Quote> qtList = [Select id,name from Quote Where ID =:qtId];
        List<Quote_Line_Item__c> qtLItem = [Select Id,name,Quantity__c,Price__c,Quotes__c,Quotes__r.Opportunity.Id, Products__c from Quote_Line_Item__c
                                           Where Quotes__c IN :qtId];
        System.debug('Quote Line Item Size : ' + qtLItem.size());
        System.debug('Checking a Quote LineItem : ' + qtLItem);
        
        for(Quote_Line_Item__c qLi : qtLItem){
            setPrId.add(qli.Products__c);
        }
        
        for(PriceBookEntry pEntry : [Select id,Product2Id from PriceBookEntry Where Product2Id IN :setPrId AND IsActive = True LIMIT 1]){
            PrMap.put(pEntry.Product2Id, pEntry.Id);
        }
         
        //List<Opportunity> oppty = New List<Opportunity>();
        List<Opportunity> oppList = [Select id,name,CloseDate,(Select id,name,ListPrice from OpportunityLineItems) From Opportunity Where ID IN :oppId];
        
        //Update QuoteLineItem To OpportunityLine Item
              
        
        For(Quote_Line_Item__c qteLineItem  : qtLItem){
            OpportunityLineItem OppLItem = New OpportunityLineItem();
            
            //OppLItem.Id = qteLineItem.Quotes__r.id;
            OppLItem.OpportunityId = qteLineItem.Quotes__r.Opportunity.Id;
            OppLItem.UnitPrice = qteLineItem.Price__c;
            OppLItem.Quantity = qteLineItem.Quantity__c;
            OppLItem.PricebookEntryId = PrMap.get(qteLineItem.Products__c);
            lItem.add(OppLItem);
        }
        
        if(lItem.size() > 0){
            
            insert lItem;
        }
        
    } 
}   
    
}

Quote ---> QuoteLineItem (Related List)---> Product will be create in Opportunity --> OppLineItem (Related List) When we click a synchronous button ....  


Thanks for help me ...

Regards,

Soundar Rajan P

All Answers

Shiva RajendranShiva Rajendran
Hi Soundar,
If you are looking for code to only choose quote records for which some specific field has been updated on the trigger.new
Use the below code
 
for( Quote qId : Trigger.newMap.keySet() )
{
  if( Trigger.oldMap.get( qId ).Custom_Synchronize__c != Trigger.newMap.get( qId ).Custom_Synchronize__c )
  {
        //replace  Custom_Synchronize__c by any other field you are looking for
     // do something here because your field has changed
// or add only these ids to some list for further processing
  }
}
Let me know if you need further help.

Thanks and Regards,
Shiva RV
SoundarSoundar

Hi Shiva Rajendran,

Sorry for late reply ..

I tried your logic , it's showing new Error 
 

ERROR :  Incompatible key type Quote for Map<Id,Quote>

trigger QuoteLineItemUpdateOnOpp on Quote (after insert,after Update) {
    
    IF(Trigger.isAfter && Trigger.isInsert){
    Set<ID> qtId = New Set<Id>();
    Set<Boolean> syn = New Set<Boolean>();
    Set<Id> OppId =  New Set<Id>();
    List<OpportunityLineItem> lItem = New  List<OpportunityLineItem>();
    for(Quote qId : Trigger.newMap.keySet()){
        if( Trigger.oldMap.get( qId ).Custom_Synchronize__c != Trigger.newMap.get( qId ).Custom_Synchronize__c ){        //ERROR LINE
        qtId.add(qId.Id);
        syn.add(qId.Custom_Synchronize__c);
    }
        }
       
    //Quote qt=trigger.new[0];
    //Quote qt = New Quote();
    IF(qtId.size() > 0){
        System.debug('Quote Size : ' + qtId.size());
       // List<Quote> qtList = [Select id,name from Quote Where ID =:qtId];
        List<Quote_Line_Item__c> qtLItem = [Select Id,name,Quantity__c,Price__c,Quotes__c,Quotes__r.Opportunity.Id, Products__c from Quote_Line_Item__c
                                           Where Quotes__c IN :qtId];
        System.debug('Quote Line Item Size : ' + qtLItem.size());
        System.debug('Checking a Quote LineItem : ' + qtLItem);
         
        //List<Opportunity> oppty = New List<Opportunity>();
        List<Opportunity> oppList = [Select id,name,CloseDate,(Select id,name,ListPrice from OpportunityLineItems) From Opportunity Where ID IN :oppId];
        
        //Update QuoteLineItem To OpportunityLine Item
              
        
        For(Quote_Line_Item__c qteLineItem  : qtLItem){
            OpportunityLineItem OppLItem = New OpportunityLineItem();
            
            OppLItem.Id = qteLineItem.Quotes__r.id;
            OppLItem.UnitPrice = qteLineItem.Price__c;
            OppLItem.Quantity = qteLineItem.Quantity__c;
            OppLItem.PricebookEntryId = qteLineItem.Products__c;
            lItem.add(OppLItem);
        }
        
        if(lItem.size() > 0){
            
            update lItem;
        }
        
    } 
}   
    
}


Could you please explain why this error was showing and how can i solve this error...
 
SoundarSoundar
Hi Shiva Rajendran,

I had make some changes in below coding now it's working as well.. 
 
trigger QuoteLineItemUpdateOnOpp on Quote (after insert,after Update) {
    IF(Trigger.isUpdate && Trigger.isAfter){
        
        System.debug('After Update : ');
    Set<ID> qtId = New Set<Id>();
    Set<Boolean> syn = New Set<Boolean>();
    Set<Id> OppId =  New Set<Id>();
    List<OpportunityLineItem> lItem = New  List<OpportunityLineItem>();
   Set<Id> setPrId  = New Set<Id>();     
   Map<Id,Id> PrMap  = New Map<Id,Id>(); 
        
    for(Quote qId : Trigger.new){
        if( Trigger.oldMap.get( qId.Id).Custom_Synchronize__c != qId.Custom_Synchronize__c ){        
        qtId.add(qId.Id);
        syn.add(qId.Custom_Synchronize__c);
        System.debug('Old Map Id: ' + qId.Id);
         
    }
        }
       
    //Quote qt=trigger.new[0];
    //Quote qt = New Quote();
    IF(qtId.size() > 0){
        System.debug('Quote Size : ' + qtId.size());
       // List<Quote> qtList = [Select id,name from Quote Where ID =:qtId];
        List<Quote_Line_Item__c> qtLItem = [Select Id,name,Quantity__c,Price__c,Quotes__c,Quotes__r.Opportunity.Id, Products__c from Quote_Line_Item__c
                                           Where Quotes__c IN :qtId];
        System.debug('Quote Line Item Size : ' + qtLItem.size());
        System.debug('Checking a Quote LineItem : ' + qtLItem);
        
        for(Quote_Line_Item__c qLi : qtLItem){
            setPrId.add(qli.Products__c);
        }
        
        for(PriceBookEntry pEntry : [Select id,Product2Id from PriceBookEntry Where Product2Id IN :setPrId AND IsActive = True LIMIT 1]){
            PrMap.put(pEntry.Product2Id, pEntry.Id);
        }
         
        //List<Opportunity> oppty = New List<Opportunity>();
        List<Opportunity> oppList = [Select id,name,CloseDate,(Select id,name,ListPrice from OpportunityLineItems) From Opportunity Where ID IN :oppId];
        
        //Update QuoteLineItem To OpportunityLine Item
              
        
        For(Quote_Line_Item__c qteLineItem  : qtLItem){
            OpportunityLineItem OppLItem = New OpportunityLineItem();
            
            //OppLItem.Id = qteLineItem.Quotes__r.id;
            OppLItem.OpportunityId = qteLineItem.Quotes__r.Opportunity.Id;
            OppLItem.UnitPrice = qteLineItem.Price__c;
            OppLItem.Quantity = qteLineItem.Quantity__c;
            OppLItem.PricebookEntryId = PrMap.get(qteLineItem.Products__c);
            lItem.add(OppLItem);
        }
        
        if(lItem.size() > 0){
            
            insert lItem;
        }
        
    } 
}   
    
}

Quote ---> QuoteLineItem (Related List)---> Product will be create in Opportunity --> OppLineItem (Related List) When we click a synchronous button ....  


Thanks for help me ...

Regards,

Soundar Rajan P
This was selected as the best answer
Shiva RajendranShiva Rajendran
Hi Sounder,
Sorry for the code issue. It was Id not Quote type in for loop
 
for( Id qId : Trigger.newMap.keySet() )
{
  if( Trigger.oldMap.get( qId ).Custom_Synchronize__c != Trigger.newMap.get( qId ).Custom_Synchronize__c )
  {
        //replace  Custom_Synchronize__c by any other field you are looking for
     // do something here because your field has changed
// or add only these ids to some list for further processing
  }
}
Thanks and Regards,
Shiva RV
 
SoundarSoundar
HI Shiva Rajendran,

Really Thanks for your help , finally that code has been changed and it's now working as per the requirement (You also given part of your work).

And i have an another one requirement also, i need your help again please. Kindly follow this link .

https://developer.salesforce.com/forums/?id=906F000000094GWIAY#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Developer_Forums&criteria=OPENQUESTIONS&id=9060G000000UZrQQAW