Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. public static void syncronizeQLI(list <QuoteLineItem> lstQLI){
  2.  
  3. map<string,string> mapQuoteOppty=new map<string,string>();
  4. string JSONContent=Json.Serialize(lstQLI);
  5. System.debug('##### JSONCONTENT '+JSONContent);
  6. JSONParser parser =JSON.createParser(JSONContent);
  7. list<string> OpptyLineId=new list<string>();
  8. list<string> QuoteLineId=new list<string>();
  9.  
  10. System.debug('parser-------->'+parser );
  11.  
  12. while (parser.nextToken() != null)
  13. {
  14. if(parser.getCurrentToken()==JSONToken.VALUE_STRING && parser.getCurrentName()=='OpportunityLineItemId__c')
  15. OpptyLineId.add(parser.getText());
  16.  
  17. if(parser.getCurrentToken()==JSONToken.VALUE_STRING && parser.getCurrentName()=='Id')
  18. QuoteLineId.add(parser.getText());
  19.  
  20. parser.nextToken();
  21.  
  22. if(parser.getCurrentToken()==JSONToken.VALUE_STRING && parser.getCurrentName()=='OpportunityLineItemId__c')
  23. OpptyLineId.add(parser.getText());
  24.  
  25. if(parser.getCurrentToken()==JSONToken.VALUE_STRING && parser.getCurrentName()=='Id')
  26. QuoteLineId.add(parser.getText());
  27. }
  28.  
  29. System.debug('OpptyLineId-------->'+OpptyLineId);
  30. System.debug('QuoteLineId-------->'+QuoteLineId);
  31.  
  32. integer iCount=0;
  33. for(string strOppLineId : OpptyLineId)
  34. {
  35. string iQuoteLineId=QuoteLineId[iCount];
  36. mapQuoteOppty.put(iQuoteLineId,strOppLineId);
  37. iCount++;
  38. }
  39. System.debug('************ MAP QUOTE OPP '+mapQuoteOppty);
  40. Set<Id> SetOppId=new Set<Id>();
  41. Set<Id> SetQuoteId=new Set<Id>();
  42. for(QuoteLineItem QLI : lstQLI)
  43. {
  44. SetQuoteId.add(QLI.QuoteId);
  45. }
  46. List<Quote> Lstquotes =[SELECT id, OpportunityId, isSyncing FROM Quote WHERE isSyncing = true AND Id in :SetQuoteId];
  47. for(Quote Qt:Lstquotes)
  48. {
  49. SetOppId.add(Qt.OpportunityId);
  50. }
  51.  
  52. List<OpportunityLineItem> lstOLI=[select Id, OpportunityId,Field1__c,Field2__c from OpportunityLineItem where OpportunityId in:SetOppId];
  53.  
  54. Map<Id,OpportunityLineItem> MapOLI=new Map<Id,OpportunityLineItem>([select Id,OpportunityId, Field2__c, Field1__c from OpportunityLineItem where OpportunityId in:SetOppId]);
  55.  
  56. Map<Id,QuoteLineItem > MapQLI=new map<Id,QuoteLineItem>([Select Id,QuoteId, Field2__c, Field1__c from QuoteLineItem where QuoteId in:SetQuoteId]);
  57.  
  58. list<OpportunityLineItem> updateQuoteLineItem =new list<OpportunityLineItem >();
  59.  
  60.  
  61. for(QuoteLineItem qli:MapQLI.values())
  62. {
  63. system.debug(' -------- QLI.ID '+qli.id);
  64. system.debug('mapQuoteOppty.get(qli.id) '+mapQuoteOppty.get(qli.id));
  65. if(mapQuoteOppty.get(qli.id)!=null)
  66. {
  67. String OppID = mapQuoteOppty.get(qli.id);
  68. System.debug('###### OPPID ----------- '+OppID);
  69. OpportunityLineItem OLI = MapOLI.get(OppID);
  70. qli.Field1__c=OLI.Field2__c;
  71. qli.Field2__c=OLI.Field1__c;
  72.  
  73. updateQuoteLineItem.add(OLI);
  74. }
  75.  
  76. }
  77. system.debug('UPDATES QUOTE LINE ITEM LIST '+updateQuoteLineItem);
  78. update updateQuoteLineItem;
  79. }
  80.  
  81. trigger QuoteLineItemAfterInsert on QuoteLineItem (before insert, after insert){
  82. list<QuoteLineItem> lstQLI = new list <QuoteLineItem>();
  83. for ( QuoteLineItem qli : trigger.new){
  84. lstQLI.add(qli);
  85. }
  86. system.debug('LIST QLI TRIGGER '+ lstQLI);
  87. if(lstQLI.size()>0){
  88. AP001_SyncronazingQLI_OLI.syncronizeQLI(lstQLI);
  89. }
  90. }
  91.  
  92. while (parser.nextToken() != null)
  93. {
  94. if(parser.getCurrentToken()==JSONToken.VALUE_STRING && parser.getCurrentName()=='OpportunityLineItemId__c')
  95. OpptyLineId.add(parser.getText());
  96.  
  97. if(parser.getCurrentToken()==JSONToken.VALUE_STRING && parser.getCurrentName()=='Id')
  98. QuoteLineId.add(parser.getText());
  99.  
  100. parser.nextToken();
  101.  
  102. if(parser.getCurrentToken()==JSONToken.VALUE_STRING && parser.getCurrentName()=='OpportunityLineItemId__c')
  103. OpptyLineId.add(parser.getText());
  104.  
  105. if(parser.getCurrentToken()==JSONToken.VALUE_STRING && parser.getCurrentName()=='Id')
  106. QuoteLineId.add(parser.getText());
  107. }
  108.  
  109. for (QuoteLineItem qli : lstQLI) {
  110.  
  111. OpptyLineId.add(qli.OpportunityLineItemId__c);
  112. QuoteLineId.add(qli.Id);
  113. }
  114.  
  115. SELECT Id, OpportunityLineItemId FROM QuoteLineItem WHERE QuoteId ='0Q01N00000xxxxxxx' AND OpportunityLineItemId != null
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement