Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if(checkRecursive.checkOneTimeForupdate()) {
- for(PriceBookEntry pbe :[Select id ,name,PriceBook2Id,PriceBook2.Name,Product2.name
- from PriceBookEntry where PriceBook2.Name = 'Sales price in GBP'
- and Product2.Family ='Delivery' and CurrencyIsoCode = 'GBP' limit 999]){
- productMap.put(pbe.Product2.name,pbe.Id);
- }
- List<OpportunityLineItem> opLines = new List<OpportunityLineItem>();
- List<OpportunityLineItem> oppLtmToDelete = new List<OpportunityLineItem>();
- List<OpportunityLineItem> oppLtmTocheck = new List<OpportunityLineItem>();
- List<Opportunity> OppsWithOplis = [select id, name, Delivery__c, Account.Distance_Between__c, Account.billingaddress,
- (select id, name, NotRemove__c, quantity, product2.Family
- from OpportunityLineItems
- where OpportunityId IN :Trigger.newMap.keySet() AND
- product2.Family ='Delivery' AND
- (NotRemove__c = True or NotRemove__c = False)
- )
- from Opportunity where
- Id IN :Trigger.newMap.keySet() AND
- Delivery__c != null AND
- Account.Distance_Between__c != null];
- //Loop through Opps only once
- for(Opportunity opp : OppsWithOplis){
- system.debug('***** Inside existing code.');
- if(Account.billingaddress != null && opp.Delivery__c != '-none-') {
- Product_Distance_Amount__c pdistanceStg = Product_Distance_Amount__c.getValues(opp.Delivery__c);
- if(productMap.containskey(opp.Delivery__c)) {
- //Loop through related Oplis only once
- for(OpportunityLineItem opli : opp.OpportunityLineItems){
- if(opli.NotRemove__c == True){
- oppLtmTocheck.add(opli);
- }else if(opli.NotRemove__c == False){
- oppLtmToDelete.add(opli);
- }
- }
- OpportunityLineItem newItem = new OpportunityLineItem();
- newItem.OpportunityId = Opp.id;
- newItem.PriceBookEntryid = productMap.get(opp.Delivery__c);
- newItem.quantity = 1;
- if(opp.Account.Distance_Between__c == '0-25') newItem.UnitPrice = pdistanceStg.X0_25_miles__c;
- else if(opp.Account.Distance_Between__c == '26-75') newItem.UnitPrice = pdistanceStg.X26_75_miles__c;
- else if(opp.Account.Distance_Between__c == '76-150') newItem.UnitPrice = pdistanceStg.X76_150_miles__c;
- else if(opp.Account.Distance_Between__c == '151-250') newItem.UnitPrice = pdistanceStg.X151_250_miles__c;
- else if(opp.Account.Distance_Between__c == '250-350') newItem.UnitPrice = pdistanceStg.X250_miles__c;
- else newItem.UnitPrice = pdistanceStg.X350_miles__c;
- if(oppLtmTocheck.isEmpty()) opLines.add(newItem);
- }
- }
- }
- if(!oppLtmToDelete.isEmpty()) delete oppLtmToDelete;
- if(!opLines.isEmpty()) insert opLines;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement