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
K_devK_dev 

Error: Compile Error: Variable does not exist: o.Product_Id__c at line 26 column 30

cAN ANYONE HELP ME I CREATED THE PRODUCT_Id__C ON OPPORTUNITY STILL IT SAYS SAME ERROR

 

trigger create_oli on Opportunity (after insert)
{
list<OpportunityLineItem> lst_oli=new list<OpportunityLineItem >();
map<string,string> pdcts=new map<string,string>();
for(Opportunity o:Trigger.New)
{
if(o.Product_Id__c!=null)
{
pdcts.put(o.Id,o.Product_Id__c);
}
}
if(!pdcts.IsEmpty())
{
map<String,string> pbe_map=new map<string,string>();
list<pricebookentry> pbe=[select id, Product2Id from PricebookEntry where Product2Id IN:pdcts.values()];
for(pricebookentry p:pbe)
{
pbe_map.put(p.Product2Id,p.Id);
}
for(string s:pdcts.Keyset())
{
if(pbe_map.containsKey(pdcts.get(s)))
{
OpportunityLineItem oli=new OpportunityLineItem();
oli.Opportunityid=s;
//oli.Product2=o.Product_Id__c;
oli.Quantity = 1;
oli.PricebookEntryId=pbe_map.get(pdcts.get(s));
oli.TotalPrice=1000;
lst_oli.add(oli);
}
}
if(!lst_oli.IsEmpty())
{
insert lst_oli;
}
}
}

Best Answer chosen by Admin (Salesforce Developers) 
souvik9086souvik9086

Write like this

 

oli.Product2=pdcts.get(oli.Opportunityid);

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks