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
Nandhini s 11Nandhini s 11 

Where am i going wrong

I have written the below trigger to update Number of child opportunity in parent opportunity record. But it's not working, 

trigger NoOfChildOppty on Opportunity (after insert,after update,after delete,after undelete) {
set<id> oId = new set<id>();
    List<opportunity> oppty = new List<opportunity>();
    if(!trigger.isDelete)
{  
    for(opportunity o: trigger.new)
    {
         oId.add(o.Parent_Opportunity__r.id );
        
    }}
    else
    {
         for(opportunity o: trigger.old)
    {
        oId.add(o.Parent_Opportunity__r.id );
        
    }    }
    for(opportunity op: [select id,No_Of_child_Oppty__c from opportunity where id =: oId])
    {
        
        List<opportunity> opp = [select id from opportunity where Parent_Opportunity__r.id=: op.id];
       op.No_Of_child_Oppty__c = opp.size();
       oppty.add(op);                
    }
    update oppty;
}
 
Ajay K DubediAjay K Dubedi
Hi Nandhini,
Here if you insert Opportunity related to parent Opportunity, Firstly we count total opportunity related to that Parent Opportunity and then update it's field No_Of_child_Oppty__c  to that count.
Try this code:
Trigger:
trigger NoOfChildOppty on Opportunity (after insert) {
   
    if(trigger.IsInsert && trigger.Isafter) {
        system.debug('------Inside trigger------');
        NoOfChildOppty_handler.incrementNoOfChildOppty(trigger.new);
    }
}
Trigger Handler:
public class NoOfChildOppty_handler {
    public static void incrementNoOfChildOppty(List<Opportunity> oppList) {
        try
        {
            system.debug('oppList.Parent_Opportunity__c------' + oppList[0].Parent_Opportunity__c);
            Set<Id> parentOppIdSet = new Set<Id>();
            List<Opportunity> AllOppList = new List<Opportunity>();
            Map<Id, Integer> oppIdVsCount = new Map<Id, Integer>();
            List<Opportunity> UpdateOppList = new List<Opportunity>();
            if(oppList.size() > 0) {
                for(Opportunity op : oppList) {
                    if(op.Parent_Opportunity__c != null) {
                        parentOppIdSet.add(op.Parent_Opportunity__c);
                    }
                }
                AllOppList = [SELECT Id, Parent_Opportunity__r.Id FROM Opportunity WHERE Parent_Opportunity__r.Id In : parentOppIdSet LIMIT 10000];
                system.debug('AllOppList.size()------' + AllOppList.size());
                if(AllOppList.size() > 0) {
                    for(Id oppId : parentOppIdSet) {
                        for(Opportunity op : AllOppList) {
                           
                            system.debug('----0--------' + op.Parent_Opportunity__r.Id + '---' + oppId);
                            if(oppId == op.Parent_Opportunity__r.Id) {
                                //system.debug('----1-');
                                if(!oppIdVsCount.containsKey(oppId)) {
                                    oppIdVsCount.put(oppId, 1);
                                    //system.debug('----2-');
                                } else {
                                    //system.debug('----3-');
                                    Integer temp = oppIdVsCount.get(oppId);
                                    temp++;
                                    oppIdVsCount.put(oppId, temp);
                                }
                            }
                        }
                    }
                    system.debug('oppIdVsCount------' + oppIdVsCount);
                    UpdateOppList = [SELECT Id, No_Of_child_Oppty__c FROM Opportunity WHERE Id In : parentOppIdSet LIMIT 10000];
                    for(Opportunity op : UpdateOppList) {
                        system.debug('op.Id------' + op.Id);
                        if(oppIdVsCount.containsKey(op.Id)) {
                            system.debug('Value Of Count------' + oppIdVsCount.get(op.Id));
                            op.No_Of_child_Oppty__c = oppIdVsCount.get(op.Id);
                        }
                    }
                    update UpdateOppList;                  
                }
            }
        }
        catch(Exception e)
        {
            system.debug('->>'+e.getCause() + '->>' + e.getLineNumber() + '->>' + e.getMessage());
        }       
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi