[SalesForce] Syncing custom fields between QuoteLineItem and OpportunityLineItem

I'm trying to syncronize between two custom fields on QuoteLineItem and OpportunityLineItem named Field1__c and Field2__c.

So after inserting the QuoteLineItem the two fields will be updated on the OpportunityLineItem.

I created a formula field on QuoteLineItem to get the OpportunityLineItem ID after insert in order to user in my class.

The problem is, I can see clearly the OpportunityLineItem ID on my presentation page. but the code doesnt bring it at all. I'm using a JSON for that matter.

any idea why?

METHOD:

public static void syncronizeQLI(list <QuoteLineItem> lstQLI){

map<string,string> mapQuoteOppty=new map<string,string>();
string JSONContent=Json.Serialize(lstQLI);
System.debug('##### JSONCONTENT      '+JSONContent);
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__c')
    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__c')
    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++;
}
System.debug('************ MAP QUOTE OPP            '+mapQuoteOppty);
Set<Id> SetOppId=new Set<Id>();
Set<Id> SetQuoteId=new Set<Id>();
for(QuoteLineItem QLI : lstQLI)
{
    SetQuoteId.add(QLI.QuoteId);
}
List<Quote> Lstquotes =[SELECT id, OpportunityId, isSyncing FROM Quote WHERE isSyncing = true AND Id in :SetQuoteId];
for(Quote Qt:Lstquotes)
{
    SetOppId.add(Qt.OpportunityId);
}

List<OpportunityLineItem> lstOLI=[select Id, OpportunityId,Field1__c,Field2__c from OpportunityLineItem where OpportunityId in:SetOppId];

Map<Id,OpportunityLineItem> MapOLI=new Map<Id,OpportunityLineItem>([select Id,OpportunityId, Field2__c, Field1__c  from OpportunityLineItem where OpportunityId in:SetOppId]);

Map<Id,QuoteLineItem > MapQLI=new map<Id,QuoteLineItem>([Select Id,QuoteId, Field2__c, Field1__c from QuoteLineItem where QuoteId in:SetQuoteId]);

list<OpportunityLineItem> updateQuoteLineItem =new list<OpportunityLineItem >();


for(QuoteLineItem qli:MapQLI.values())
{
system.debug(' -------- QLI.ID         '+qli.id);
system.debug('mapQuoteOppty.get(qli.id) '+mapQuoteOppty.get(qli.id));
       if(mapQuoteOppty.get(qli.id)!=null)
   {
      String OppID = mapQuoteOppty.get(qli.id);
      System.debug('###### OPPID      -----------  '+OppID);
      OpportunityLineItem OLI = MapOLI.get(OppID);
      qli.Field1__c=OLI.Field2__c;
      qli.Field2__c=OLI.Field1__c;   

      updateQuoteLineItem.add(OLI);
   }

}
system.debug('UPDATES QUOTE LINE ITEM LIST        '+updateQuoteLineItem);
update updateQuoteLineItem;
}

TRIGGER:

trigger QuoteLineItemAfterInsert on QuoteLineItem (before insert, after insert){
list<QuoteLineItem> lstQLI = new list <QuoteLineItem>();
for ( QuoteLineItem qli : trigger.new){
    lstQLI.add(qli);
}
system.debug('LIST QLI TRIGGER           '+ lstQLI);
if(lstQLI.size()>0){
    AP001_SyncronazingQLI_OLI.syncronizeQLI(lstQLI);
}
}

Best Answer

It seems like "OpportunityLineItemId" field on Quote Line Item object is a special field.

SELECT Id, OpportunityLineItemId FROM QuoteLineItem WHERE QuoteId ='0Q01N00000xxxxxxx' AND OpportunityLineItemId != null

If I remove "OpportunityLineItemId" from WHERE clause, it does show me the the id. But adding the condition does not show any records. Tried it in trigger and developer console, same result.

Related Topic