You need to sign in to do that
Don't have an account?
jishan roy
how to solve the error in opportunity object in trigger
How to display opportunity associated all products in trigger.
Here is my code:
trigger opportunityProductCount on Product2 (after insert, after update, after delete, after undelete) {
set<Id> setIds = new set<Id>();
list<Opportunity> opplist= new list<Opportunity>();
if(trigger.isafter){
if(trigger.isinsert|| trigger.isUpdate|| trigger.isUndelete){
for(Product2 p: trigger.new){
setIds.add(p.OpportunityId);
}
}
if(trigger.isupdate|| trigger.isdelete){
for(Product2 po:trigger.old){
setIds.add(po.OpportunityId);
}
}
}
List<Opportunity> oplist = [Select Id,Name,Number_of_Product__c,(select id,name from Products2)from Opportunity where Id in:setIds];
system.debug('show products'+ oplist);
for (Opportunity opp: oplist){
Integer count = opp.Products2.size();
opp.Number_of_Product__c = count;
opplist.add(opp);
}
update opplist;
}
Here is my code:
trigger opportunityProductCount on Product2 (after insert, after update, after delete, after undelete) {
set<Id> setIds = new set<Id>();
list<Opportunity> opplist= new list<Opportunity>();
if(trigger.isafter){
if(trigger.isinsert|| trigger.isUpdate|| trigger.isUndelete){
for(Product2 p: trigger.new){
setIds.add(p.OpportunityId);
}
}
if(trigger.isupdate|| trigger.isdelete){
for(Product2 po:trigger.old){
setIds.add(po.OpportunityId);
}
}
}
List<Opportunity> oplist = [Select Id,Name,Number_of_Product__c,(select id,name from Products2)from Opportunity where Id in:setIds];
system.debug('show products'+ oplist);
for (Opportunity opp: oplist){
Integer count = opp.Products2.size();
opp.Number_of_Product__c = count;
opplist.add(opp);
}
update opplist;
}
What is the error you are seeing?Thx
error is opportunitis id does not exist this error occured
Product2 object doesn't have opportunity lookup Opportunitylineitem have product and opportunity lookup
Opportunitylineitem object not show how to create record?
i got this error:
Didn't understand relationship 'Oppotunitylineitems' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.
Select id, Total_Oppotunitylineitems__c ,Description ,(Select id from Opportunitylineitems) from Opportunity where Id in : setoppIds
How To Create Opportunitylineitem
opportunity opp = new opportunity();
opp.Name='Syncing Opp';
Opp.CloseDate = system.today();
opp.StageName ='Closed Won';
insert opp;
Product2 p = new Product2();
p.Name ='test pro';
p.Description = 'test pro';
p.isActive = true;
insert p;
system.debug('p> '+ p.Id);
Pricebook2 stdPriceBook = [select id, name,isActive,IsArchived from Pricebook2 where isStandard = true limit 1];
stdPriceBook.isActive = true;
update stdPriceBook;
system.debug(stdPriceBook.isActive);
system.debug(stdPriceBook.IsArchived);
PriceBookEntry customPriceBookEntry = new PriceBookEntry();
customPriceBookEntry.Product2Id = p.Id;
customPriceBookEntry.Pricebook2Id = stdPriceBook.Id;
customPriceBookEntry.UnitPrice = 5000;
customPriceBookEntry.IsActive = true;
insert customPriceBookEntry;
system.debug('customPriceBookEntry> '+ customPriceBookEntry.Id);
OpportunityLineItem oppLineItem = new OpportunityLineItem();
oppLineItem.OpportunityId = '0062w00000DYKacAAH';
oppLineItem.PricebookEntryId = customPriceBookEntry.Id;
oppLineItem.Quantity = 5;
oppLineItem.UnitPrice = 1000;
insert oppLineItem;