+ Start a Discussion
Prince VenkatPrince Venkat 

MasterDetail Rollup

Hi
They are two objects Activity Master
Student Activities
Through programming want to create rollupsummary ( not through configuration)
Activity Master has a field No of Students Enrolled (count of records need to dispalyed here)


trigger Rollupmaster on Student_Activities__c (after insert,after update,after delete)
{
set<id> lstSet = new set<id>();
    list<Activity_Master__c> lstAm = new list<Activity_Master__c>();
    
if((trigger.isinsert || trigger.isupdate)&& trigger.isafter)    
{
    for(Student_Activities__c c : trigger.new)
    {
        if(c.id!=null)
        {
          lstSet.add(c.id);  
        }
    }
if(trigger.isdelete)
{
  for(Student_Activities__c c : trigger.old)  
  {
      lstSet.add(c.id);
  }
    
}
for(Activity_Master__c a :[select id,No_of_Students_Enrolled__c,(select id from Student_Activities__r) from
                             Activity_Master__c where id =:lstSet])
{
  a.No_of_Students_Enrolled__c = a.Student_Activities__r.size();
    lstAm.add(a);
}


update lstAm;

}
}

Getting error in soql query Any suggestions.

 
Best Answer chosen by Prince Venkat
CharuDuttCharuDutt
Hii Prince Venkat
Try Below Code
Activity_Master__c Is Master Detail  Field In Student_Activities__c Object  You Can Change According To the Field Name in Your Org 
trigger NumberOfChild on Student_Activities__c(After Insert,After Update,After Delete) {
List<Activity_Master__c> accList=new List<Activity_Master__c>();
    Set<Id> setAccIds = new Set<Id>();
    if(Trigger.isInsert){
         if(trigger.isAfter){
        for(Student_Activities__c con : Trigger.new){
            if(con.Activity_Master__c!= null){
            setAccIds.add(con.Activity_Master__c);
            	}
			}
		}
    } 
    system.debug('setAccIds ==> '+setAccIds);
    if(Trigger.isUpdate){
         if(trigger.isAfter){
        for(Student_Activities__c con : Trigger.new){ 
            if(con.Activity_Master__c!=Trigger.oldMap.get(con.Id).Activity_Master__c){
               	setAccIds.add(con.Activity_Master__c);
                setAccIds.add(Trigger.oldMap.get(con.Id).Activity_Master__c);
            	}
			}        
        }
    }
    if(Trigger.isDelete){
        if(trigger.isAfter){
        for(Student_Activities__c con : Trigger.old) { 
            if(con.Activity_Master__c!= null){
            setAccIds.add(con.Activity_Master__c);
            	}
        	}
        }
    }    
    for(Activity_Master__c acc :[Select id,Total_Contacts__c ,(Select id,name from Student_Activities__r) from Activity_Master__c where Id in : setAccIds]){
        acc.No_of_Students_Enrolled__c = acc.Student_Activities__r.size();
        acclist.add(acc);
    }
    if(acclist.size()>0){
        update accList;     
    }
}
Please Mark It As Best Answer If It Helps
Thank You!

 

All Answers

AbhinavAbhinav (Salesforce Developers) 
Hi Prince ,

Could you please tell the error?

Thanks!
Prince VenkatPrince Venkat
Hi Abhinav
ERROR at Row:1:Column:54
Didn't understand relationship 'Student_Activities__r' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.
CharuDuttCharuDutt
Hii Prince Venkat
Try Below Code
Activity_Master__c Is Master Detail  Field In Student_Activities__c Object  You Can Change According To the Field Name in Your Org 
trigger NumberOfChild on Student_Activities__c(After Insert,After Update,After Delete) {
List<Activity_Master__c> accList=new List<Activity_Master__c>();
    Set<Id> setAccIds = new Set<Id>();
    if(Trigger.isInsert){
         if(trigger.isAfter){
        for(Student_Activities__c con : Trigger.new){
            if(con.Activity_Master__c!= null){
            setAccIds.add(con.Activity_Master__c);
            	}
			}
		}
    } 
    system.debug('setAccIds ==> '+setAccIds);
    if(Trigger.isUpdate){
         if(trigger.isAfter){
        for(Student_Activities__c con : Trigger.new){ 
            if(con.Activity_Master__c!=Trigger.oldMap.get(con.Id).Activity_Master__c){
               	setAccIds.add(con.Activity_Master__c);
                setAccIds.add(Trigger.oldMap.get(con.Id).Activity_Master__c);
            	}
			}        
        }
    }
    if(Trigger.isDelete){
        if(trigger.isAfter){
        for(Student_Activities__c con : Trigger.old) { 
            if(con.Activity_Master__c!= null){
            setAccIds.add(con.Activity_Master__c);
            	}
        	}
        }
    }    
    for(Activity_Master__c acc :[Select id,Total_Contacts__c ,(Select id,name from Student_Activities__r) from Activity_Master__c where Id in : setAccIds]){
        acc.No_of_Students_Enrolled__c = acc.Student_Activities__r.size();
        acclist.add(acc);
    }
    if(acclist.size()>0){
        update accList;     
    }
}
Please Mark It As Best Answer If It Helps
Thank You!

 
This was selected as the best answer
Prince VenkatPrince Venkat
Hi

Still i am getting the error

                                                 ^
ERROR at Row:1:Column:60
Didn't understand relationship 'Student_Activities__r' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.
AbhinavAbhinav (Salesforce Developers) 
Hi Prince ,

Could you please cross check your child relationship name once again by following this link (https://www.forcetalks.com/salesforce-topic/how-to-find-child-relationship-name-in-salesforce/#:~:text=Adarsh,-Member&text=Hello%20Ankit%2C,between%20parent%20and%20child%20objects%5D.)   just navigate to  Relationship Field on child Object  and than appending __r  with that in query.

Thanks!