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 

Write trigger on opportunity object opportunities associated all product count and display in opportunity.

Can anybody help to out of this que how to associated record and count display in oppoprtunity in trigger.I just started to learn development.
PriyaPriya (Salesforce Developers) 

Hey Jishan,

You don't have to write the trigger to get the count of Opportunity product record. 

You can create a roll up summary field on the Opportunity object which will count the number of associated product and thus the count value will be displayed on the opportunity record.

Hope this information.

Kindly mark it as the best answer so that it can help others.

Regards,

Priya Ranjan

jishan royjishan roy
Hello priya 
thanks for replay,
but the requirement is to write the trigger for that thats why.
CharuDuttCharuDutt
Hii Jishan Roy
Try Below Code
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();
        acclist.add(acc);
    }
    if(acclist.size()>0){
        update accList;     
    }
}
Please Mark It As Best Answer If it Helps
Thank You!
Casey ChristensenCasey Christensen
Thank you for sharing information. Game and music app ApkCima (http://apkcima.com/) is completely free