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
ankaiah bandi 6ankaiah bandi 6 

need code for below requirement(count of child records for self lookup relationship)

Hi,

i need to display the count of child records for custom object called "Network__c", which have the  selflookup lookup realtionship.the Network__c can have 3 recordtypes called A ,B &C... here A is parent of B recordtype and B is parent of C record type..Now my expected scenario is, i need to displsy
1. the count of B records and C records at A recordtype level.
2. the count of C records at B recordtype level.
Note: here at B recordtype level and C record type level have the same lookup relationship field name called parent__c. at B recordtype level we need to select only A record type and at C record type level we need to select only B record type.
problem : i am not able display the count of C records at A record type level.

Now i can have error to display count of C records,please see the below code  and can any one help me how can i proceed? 
error message: "initial term of field expression must be a concrete SObject: Map<Id,Network__c>"

trigger Countofchilds on Network__c (before insert, before update, before delete) {
    
    Map<Id,Network__c> Amap_Ids = new Map<Id,Network__c>();
    Map<Id,Network__c> Bmap_Ids = new Map<Id,Network__c>();
    Map<Id,Network__c> Cmap_Ids = new Map<Id,Network__c>();
    if(Trigger.isUpdate || Trigger.isInsert){
        Amap_Ids=getTypeofNtw.groupNtw(Trigger.New);
        Bmap_Ids=getTypeofNtw.MNtw(Trigger.New);
        Cmap_Ids=getTypeofNtw.ONtw(Trigger.New);
    }
    if(!Amap_Ids.isEmpty() && (Trigger.isUpdate || Trigger.isInsert)){
        Map<Id,Network__c> lnpp =new Map<Id,Network__c>( [SELECT Id,Number of C__c,Number of B__C,(Select id from Parent__r) FROM Network__c WHERE Id IN: Amap_Ids.keySet()] );

        Set<Id> MNppset = new Set<Id>();
        for(Id i : lnpp.keyset()){
            Network__c np = lnpp.get(i);
            for(Network__c n : np.Parent__r){
                MNppset.add(n.Id);
            }
        }
        Map<Id,Network__c> mnpp =new Map<Id,Network__c>( [SELECT Id,(Select id from Parent__r) FROM Network__c WHERE Id IN: MNppset] );
        for(Id i : lnpp.keyset()){
            Network__c np = lnpp.get(i);            
            try{
                system.debug('------>'+np.Parent__r.size());
                if(!np.Parent__r.isEmpty())   
                    Amap_Ids.get(i).Number of B__C=np.Parent__r.size();
               for(Network__c n : np.Parent__r){
                    if(!mnpp.Parent__r.isEmpty()) 
                        Amap_Ids.get(i).Number of C__c  =Amap_Ids.get(i).Number of C__c+1; 
                }
            }catch(Exception e){
                Amap_Ids.get(i).Number of B__C=0;
            }    
        }        
    }                   
 }//End
 
can any one provide the code for the above requirement, it would be greatfull......Thanks.....!!!!