function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
BerettaJonBerettaJon 

Whats wrong with this

private void updateOpportunity(List<OpportunityLineItem> newRecords) {

// create a new map to hold the
Map<ID,String> productNames = new Map<ID,String>();
List<Opportunity> opps;
List<Product2> prods;
Map<ID,Opportunity> opportunities = new Map<ID,Opportunity>();
Map<ID,PriceBookEntry> pbIDs = new Map<ID,PriceBookEntry>();
Map<ID,Product2> products = new Map<ID,Product2>();


for (OpportunityLineItem oli : newRecords) {
opportunities.put(oli.id,oli.Opportunity);
pbIDs.put(oli.id,oli.pricebookentry);

}
prods = [select name from Product2 where id IN :pbIDs.keyset()];
opps = [select id from Opportunity where id IN :opportunities.keyset()];

for(Product2 prod : prods)
{
// it seems like I never enter this loop
for (Opportunity opp : opps)
{
if(prod.Option_Category__c == 'Model')
{
opp.model__c = prod.name;
}
}
}

// commit the records
update opps;

}

}

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

One thing that jumps out is that you are keying the pbIDs map with OpportunityLineItem ids, but then using it to retrieve Product2 records - I'd imagine your query to populate the prods list is always returning no elements.

All Answers

bob_buzzardbob_buzzard

One thing that jumps out is that you are keying the pbIDs map with OpportunityLineItem ids, but then using it to retrieve Product2 records - I'd imagine your query to populate the prods list is always returning no elements.

This was selected as the best answer
BerettaJonBerettaJon

I think you are right.  What I am trying to accomplish is to update a field on opportunity with the product name from the Opportunity Product Line Item.  How do I get the product name if all I start with is a collection of OpportunitiyLineItems?

bob_buzzardbob_buzzard

You have to jump through a couple more hoops to get at the products.  

 

An opportunitylineitem has a lookup to a pricebookentry sobject (pricebookentryid). You'll then need to retrieve the pricebookentry, which has a relationship to the Product2 record - you can follow that for the product name.

 

You can probably do some of this via formula fields to save yourself some soql queries.

BerettaJonBerettaJon

Here is what ended up working for me if anyone is looking for a solution

 

Set<Id> CurrentOppId = new Set<Id>();
for (OpportunityLineItem OppLnItem : newRecords){
CurrentOppId.add(OppLnItem.OpportunityId);
}

// Create List of One Opportunity Id to Update
List<Opportunity> OppId =
[Select o.Id from Opportunity o
where o.Id in: CurrentOppId];

 

List<OpportunityLineItem> relatedOLIs =
[Select oli.Id, PricebookEntry.Product2.name, PricebookEntry.Product2.Option_Category__c from OpportunityLineItem oli
where oli.OpportunityId in: CurrentOppId];


for (Opportunity opp2 : OppId){

for (OpportunityLineItem oli : relatedOLIs){
if(oli.PricebookEntry.Product2.Option_Category__c == 'Model')
{
opp2.model__c = oli.PricebookEntry.Product2.name;
}
}
}
update OppID;

}