I need to write a process to handle the following:
- Retrieve all price book entries for the standard price book
- Retrieve all price book entries for non-standard price books that have a matching product2Id in the above list (1).
- For each non-standard entry in (2), update values on the object to match those with its corresponding match in list (1). Then set a custom Is_Synchornsied__c boolean value on the associated non-standard price book to true.
Ive had a few attempts at this and my most recent is getting an APEX CPU time limit exception. Could anyone assist me me with the most efficient way to achieve the above please.
What i have so far:
//get standard price book
pricebook2 SPB = [SELECT Id FROM Pricebook2 WHERE IsActive = true AND
IsStandard = true];
String SPBId = SPB.Id;
//get book entries for the standard price book
List<pricebookEntry> StandardPBEntries = [SELECT Id, Name, Pricebook2Id,
Product2Id, UnitPrice, UseStandardPrice,
ProductCode,Is_Negotiated__c
FROM PricebookEntry WHERE priceBook2Id = :SPBId];
//prep list of entries for final update at end outside of any loop;
List<pricebookEntry> listForEntryUpdate = new List<pricebookEntry>();
List<pricebook2> listForBookUpdate = new List<pricebook2>();
//create list of standard entry Product2Id's
Set<Id> product2IdsSet = new Set<Id>();
for(pricebookEntry StandardEntry : StandardPBEntries){
product2IdsSet.add(StandardEntry.Product2Id);
}
//look for matches
List<pricebookEntry> Matches = [SELECT Id, Name, Pricebook2Id, Product2Id, IsActive, Is_Negotiated__c FROM PricebookEntry
where product2id IN: product2IdsSet and isactive = true and Is_Negotiated__c = false
AND priceBook2Id IN (SELECT Id FROM Pricebook2 WHERE IsActive = true AND IsStandard = false
and Is_Syncronised__c = false)];
//for each match update values based on the standard entry with the same product2Id
for(pricebookEntry aMatch : Matches){
for(pricebookEntry standard : StandardPBEntries){
If(standard.product2Id == aMatch.product2Id){
aMatch.UnitPrice = standard.Unitprice;
aMatch.UseStandardPrice = standard.UseStandardPrice;
}
// aMatch.pricebook2.Is_Syncronised__c = true;
// listForEntryUpdate.add(aMatch);
// listForBookUpdate.add(aMatch.pricebook2);
}
}