anusha(salesforce developer)

Friday, 17 June 2016

Syncing custom fields between quotes and opportunities (Custom Quote Sync)

Some time we need to sync Opportunity Line Item and Quote Line Item.When we create Quote automatically  Opportunity line item created as Quote Line Item. But Some time we also need to sync custom field.

Requirement :-

I would like to sync between opportunity line item and quote line item. I have a few custom fields defined in addition to standard fields in both quote line item and opportunity line item objects. Custom fields in both objects are exact replica. 

Solution :- 
For above requirement I have created the below trigger :- 

/*
***********************************************************************
Created By    :- Amit Chaudhary
Created Date  :- 06 NOV 2014
Desc          :- Syncing custom fields between quotes and opportunities 
***********************************************************************
*/

trigger QuoteLineItemTrigger on QuoteLineItem (after insert){


    map<string,string> mapQuoteOppty=new map<string,string>();
    string JSONContent=Json.Serialize(Trigger.New);
    JSONParser parser =JSON.createParser(JSONContent);
    list<string> OpptyLineId=new list<string>();
    list<string> QuoteLineId=new list<string>();
    System.debug('parser-------->'+parser );
    
    while (parser.nextToken() != null) 
    {
        if(parser.getCurrentToken()==JSONToken.VALUE_STRING && parser.getCurrentName()=='OpportunityLineItemId')
        OpptyLineId.add(parser.getText());
            if(parser.getCurrentToken()==JSONToken.VALUE_STRING && parser.getCurrentName()=='Id')
                QuoteLineId.add(parser.getText());
                parser.nextToken();
                    if(parser.getCurrentToken()==JSONToken.VALUE_STRING && parser.getCurrentName()=='OpportunityLineItemId')
                        OpptyLineId.add(parser.getText());
                        if(parser.getCurrentToken()==JSONToken.VALUE_STRING && parser.getCurrentName()=='Id')
                            QuoteLineId.add(parser.getText());
    }

    System.debug('OpptyLineId-------->'+OpptyLineId);
    System.debug('QuoteLineId-------->'+QuoteLineId);
    
    integer iCount=0;
    for(string strOppLineId : OpptyLineId)
    {
        string iQuoteLineId=QuoteLineId[iCount];
        mapQuoteOppty.put(iQuoteLineId,strOppLineId);
        iCount++;
    }
    Set<Id> SetOppId=new Set<Id>();
    Set<Id> SetQuoteId=new Set<Id>();
    for(QuoteLineItem QLI:trigger.new)
    {
        SetQuoteId.add(QLI.QuoteId);
    }
    List<Quote> Lstquotes =[select id, OpportunityId, isSyncing from Quote where Id in :SetQuoteId];
    for(Quote Qt:Lstquotes)
    {
        SetOppId.add(Qt.OpportunityId);
    }
    
    List<OpportunityLineItem> lstOLI=[select Id, OpportunityId,Negotiated__c,End_Date__c,Start_Date__c from OpportunityLineItem where OpportunityId in:SetOppId];
    
    Map<Id,OpportunityLineItem> MapOLI=new Map<Id,OpportunityLineItem>([select Id,Negotiated__c, OpportunityId, End_Date__c,Start_Date__c  from OpportunityLineItem where OpportunityId in:SetOppId]);
    Map<Id,QuoteLineItem > MapQLI=new map<Id,QuoteLineItem>([Select Id, Negotiated__c, End_Date__c,Start_Date__c from QuoteLineItem where QuoteId in:SetQuoteId]);
    
    list<QuoteLineItem> updateQuoteLineItem =new list<QuoteLineItem >();
    for(QuoteLineItem qli:MapQLI.values())
    {
       System.debug('&&&&'+mapQuoteOppty);
       if(mapQuoteOppty.get(qli.id)!=null)
       {
          String OppID = mapQuoteOppty.get(qli.id);
          OpportunityLineItem OLI = MapOLI.get(OppID);
          
          qli.End_Date__c=OLI.End_Date__c;
          qli.Start_Date__c=OLI.Start_Date__c;   
          qli.Negotiated__c= OLI.Negotiated__c;

          updateQuoteLineItem.add(qli);
            
       }
    }
    update updateQuoteLineItem;
    
}

No comments:

Post a Comment