+ Start a Discussion
ankaiah bandi 6ankaiah bandi 6 

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


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){
    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){
        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);            
                    Amap_Ids.get(i).Number of B__C=np.Parent__r.size();
               for(Network__c n : np.Parent__r){
                        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;
can any one provide the code for the above requirement, it would be greatfull......Thanks.....!!!!