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
charan admincharan admin 

Trigger Secenario

Hi Everybody

I have account stage name fields ( name is new, open, losedwon,
losedlost) in opportunity object have stage name (new, open, losedwon, losedlost) i have account have abc , but opportunity have records have 10 records (2 is new, 2 is open, losedwon is 3, losedlost is 4) you display total as well in account object and individual records also display using trigger how

Thanks in Advance
Charan
ANUTEJANUTEJ (Salesforce Developers) 
Hi Charan,

Below is an implementation that has a trigger that could count the number of child records and enter into the parent record can you try checking the below link once:

Trigger: https://developer.salesforce.com/forums/?id=906F0000000BYzFIAW

This value you can update on the child record by referring from the child records.

Also I would suggest you try checking to see the viability of implementing this scenario using flow as this is something that can be easily achieved using a flow, below is the link that you can use for reference to implement your use case:

Link: https://trailblazers.salesforce.com/answers?id=9063A000000sxarQAA

I hope this helps and in case if this comes in handy can you please choose this as the best answer so that it can be useful for others in the future.

Regards,
Anutej
charu tekadecharu tekade
trigger UpdateOrder on Child__c (after insert, after update, after delete, after undelete) {

   List<Parent__c> ct = new List<Parent__c>();
   
   Set<Id> custord = new Set<Id>();
   
   if(Trigger.isDelete) {
     for(Child__c test:Trigger.Old) {
      
        custord.add(test.Parent__c);   
    
     }   
   
   }
   else
   if(Trigger.isUpdate) {

     for(Child__c test:Trigger.New) {
      
        custord.add(test.Parent__c);   
    
     }

     for(Child__c test:Trigger.Old) {
      
        custord.add(test.Parent__c);   
    
     }   
   
   }
   else
   {
     for(Child__c test:Trigger.New) {
      
        custord.add(test.Parent__c);   
    
     }
   }
   
   AggregateResult[] groupedResults = [SELECT COUNT(Id), Parent__c FROM Child__c where Parent__c IN :custord GROUP      BY Parent__c ];
   
   for(AggregateResult ar:groupedResults) {
     
     Id custid = (ID)ar.get('Parent__c');
     
     Integer count = (INTEGER)ar.get('expr0');
     
     Parent__c cust1 = new Parent__c(Id=custid);
     
     cust1.child_count__c = count;
     
     ct.add(cust1);
      
   }
   
   
   update ct;

}