Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. if(checkRecursive.checkOneTimeForupdate()) {
  2. for(PriceBookEntry pbe :[Select id ,name,PriceBook2Id,PriceBook2.Name,Product2.name
  3. from PriceBookEntry where PriceBook2.Name = 'Sales price in GBP'
  4. and Product2.Family ='Delivery' and CurrencyIsoCode = 'GBP' limit 999]){
  5. productMap.put(pbe.Product2.name,pbe.Id);
  6.  
  7. }
  8.  
  9. List<OpportunityLineItem> opLines = new List<OpportunityLineItem>();
  10. List<OpportunityLineItem> oppLtmToDelete = new List<OpportunityLineItem>();
  11. List<OpportunityLineItem> oppLtmTocheck = new List<OpportunityLineItem>();
  12.  
  13. List<Opportunity> OppsWithOplis = [select id, name, Delivery__c, Account.Distance_Between__c, Account.billingaddress,
  14. (select id, name, NotRemove__c, quantity, product2.Family
  15. from OpportunityLineItems
  16. where OpportunityId IN :Trigger.newMap.keySet() AND
  17. product2.Family ='Delivery' AND
  18. (NotRemove__c = True or NotRemove__c = False)
  19. )
  20. from Opportunity where
  21. Id IN :Trigger.newMap.keySet() AND
  22. Delivery__c != null AND
  23. Account.Distance_Between__c != null];
  24.  
  25. //Loop through Opps only once
  26. for(Opportunity opp : OppsWithOplis){
  27. system.debug('***** Inside existing code.');
  28. if(Account.billingaddress != null && opp.Delivery__c != '-none-') {
  29. Product_Distance_Amount__c pdistanceStg = Product_Distance_Amount__c.getValues(opp.Delivery__c);
  30.  
  31. if(productMap.containskey(opp.Delivery__c)) {
  32. //Loop through related Oplis only once
  33. for(OpportunityLineItem opli : opp.OpportunityLineItems){
  34. if(opli.NotRemove__c == True){
  35. oppLtmTocheck.add(opli);
  36. }else if(opli.NotRemove__c == False){
  37. oppLtmToDelete.add(opli);
  38. }
  39. }
  40.  
  41. OpportunityLineItem newItem = new OpportunityLineItem();
  42. newItem.OpportunityId = Opp.id;
  43. newItem.PriceBookEntryid = productMap.get(opp.Delivery__c);
  44. newItem.quantity = 1;
  45. if(opp.Account.Distance_Between__c == '0-25') newItem.UnitPrice = pdistanceStg.X0_25_miles__c;
  46. else if(opp.Account.Distance_Between__c == '26-75') newItem.UnitPrice = pdistanceStg.X26_75_miles__c;
  47. else if(opp.Account.Distance_Between__c == '76-150') newItem.UnitPrice = pdistanceStg.X76_150_miles__c;
  48. else if(opp.Account.Distance_Between__c == '151-250') newItem.UnitPrice = pdistanceStg.X151_250_miles__c;
  49. else if(opp.Account.Distance_Between__c == '250-350') newItem.UnitPrice = pdistanceStg.X250_miles__c;
  50. else newItem.UnitPrice = pdistanceStg.X350_miles__c;
  51. if(oppLtmTocheck.isEmpty()) opLines.add(newItem);
  52. }
  53. }
  54. }
  55.  
  56. if(!oppLtmToDelete.isEmpty()) delete oppLtmToDelete;
  57.  
  58. if(!opLines.isEmpty()) insert opLines;
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement