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
jishan royjishan 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;

}
SwethaSwetha (Salesforce Developers) 
HI Jishan,
What is the error you are seeing?Thx
jishan royjishan roy
hello swetha;
error is opportunitis id does not exist this error occured
CharuDuttCharuDutt
Hii Jishan Roy
Product2 object doesn't have opportunity lookup Opportunitylineitem have product and opportunity lookup
 
trigger NumberOfChild on Opportunitylineitem(After Insert,After Update,After Delete) {

   List<Opportunity> oppList=new List<Opportunity>();

    Set<Id> setoppIds = new Set<Id>();
    if(Trigger.isInsert){
         if(trigger.isAfter){
        for(Opportunitylineitem oli: Trigger.new){
            if(oli.OpportunityId!= null){
            setoppIds.add(oli.OpportunityId);
            	}
			}
		}
    } 
    system.debug('setoppIds ==> '+setoppIds );
    if(Trigger.isUpdate){
         if(trigger.isAfter){
        for(Opportunitylineitem oli: Trigger.new){
            if(oli.OpportunityId!=Trigger.oldMap.get(oli.Id).OpportunityId){
               	setoppIds .add(oli.OpportunityId);
                setoppIds .add(Trigger.oldMap.get(oli.Id).OpportunityId);
            	}
          
			}        
        }
    }
    if(Trigger.isDelete){
        if(trigger.isAfter){
        for(Opportunitylineitem oli: Trigger.old) { 
            if(oli.OpportunityId!= null){
            setoppIds .add(oli.OpportunityId);
            	}
        	}
        }
    }    
    for(Opportunity opp:[Select id,Total_Oppotunitylineitems__c ,Description ,(Select id from Oppotunitylineitems) from Opportunity where Id in : setoppIds ]){
        opp.Total_Oppotunitylineitems__c = opp.Oppotunitylineitems.size();
        oppList.add(acc);
    }
    if(oppList.size()>0){
        update oppList;     
    }
}

 
jishan royjishan roy
hello CharuDutt,
Opportunitylineitem object not show how to create record?
 
jishan royjishan roy
hello.
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.
CharuDuttCharuDutt
Replace Qurey With This
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;