You need to sign in to do that
Don't have an account?
Nar_7
Calculate no. of child records
Hi Folks, I am new to salesforce and wolud like to know, "How to calculate no of child records to a parent object without using trigger".
Any help will be appreciated, Thank you very much beforehand.
Any help will be appreciated, Thank you very much beforehand.
If it is a lookup relationship between the parent and child object, then you need to create a trigger on child object that will update the count field (you need to create a custom field to store the count of child records) on parent object. You need to handle the insert, delete and undelete events in this trigger. You may also have to handle the update event in case your lookup relationship allows reparenting of records.
If there is a master-detail relationship between the parent and child objects, then you can use rollup summary field.
While your formula fields calculate values using fields within a single record, roll-up summary fields calculate values from a set of related records, such as those in a related list. You can create roll-up summary fields that automatically display a value on a master record based on the values of records in a detailed record. These detail records must be directly related to the master through a master-detail relationship.
If it is a lookup relationship, then you can create a Trigger on the child object.
Hope this helps you!
Best regards,
Jyothsna
You can do this by below two option :-
Option 1:-
If you have mater detail relation ship then you can try the rollup-summery field.
Option 2:-
You can try the same with trigger. Please check below post for code.
1) http://www.infallibletechie.com/2014/11/how-to-count-number-of-child-records-in.html
2) https://github.com/abhinavguptas/Salesforce-Lookup-Rollup-Summaries
3) http://automationchampion.com/2013/02/13/count-records-in-a-related-list/
4) https://www.sundoginteractive.com/blog/rollup-summary-with-a-lookup-field-salesforce
Sample code. Let us know if this will help you
Thanks
Amit Chaudhary
I would request you to inform us about the relationship between child and parent objects.
Scenario 1: If the Relationship is Master-Detail:
Then you can go to Parent object and Create a custom field with Roll-Up Summary and select the roll-up type as Count as shown below:
Scenario 2: If the Relationship is Lookup:
Then we have to implement it by writing the Trigger:
To calculate Rollup Summary using the Trigger, you need to follow below:
Need to consider all DML operations like insert, update, delete and undelete of child records.
Insert of Child record:
--> Need to write an after insert trigger to update the Parent record with calculated count value.
Update of Child record:
--> Make sure that the Parent information got changed.
--> Need to write an after update trigger and get both old and new Parent information and perform the re-calculation of count.
Delete of Child record:
--> Need to write an after delete trigger to update the Parent record with re-calculated count value.
Undelete of Child record:
--> Need to write an after undelete trigger to update the Parent record with re-calculated count value.
Also check the below code:
Also look into below posts:
1) http://blog.jeffdouglas.com/2011/08/23/salesforce-trigger-when-rollups-summaries-not-possible/
2) https://success.salesforce.com/answers?id=90630000000h3mNAAQ
3) https://developer.salesforce.com/forums/?id=906F00000008yWuIAI
4) https://github.com/abhinavguptas/Salesforce-Lookup-Rollup-Summaries
5) http://www.iterativelogic.com/developing-custom-apex-rollup-summary-field-logic/ (http://www.iterativelogic.com/developing-custom-apex-rollup-summary-field-logic/)
Please do let me know if it helps you.
Regards,
Mahesh
Please go through below link you will find all the scenarios like delete, update and insert count.
https://sfdceinstien.wordpress.com/2019/01/19/custom-lookup-maintain-number-of-child-count-on-parent-object-in-case-of-lookup-relationship-in-saleforce/
Regards,
Keerti Yadav
Case 1: If the Relationship is Master-Detail:
Then you can create a custom field with Roll-Up Summary on the Parent object and select the roll-up type as Count.
Case 2: If the Relationship is Lookup:
Then we have to write the Trigger.
Case 3: You can use the subquery or Relationship Query as below:
SOQL for two standard objects. Let’s say Account as a parent object and Contact as a child object.
Query to fetch the all Contact which is associated with Account ‘Account1’.
// When a record is fetched from Contact
SELECT Name, Account.Name FROM Contact where Account.Name = 'Account1'
// When a record is fetched from Account.
// Contacts is a child relationship name of Contact object.
SELECT Name, (SELECT Name FROM Contacts) FROM Account where Name = 'Account1'
trigger counttrigger on Contact (after insert, after update,after delete, after undelete) {
list<Account> aclist = new list<Account>();
set<id> setid = new set<id>();
if(trigger.isAfter){
if(trigger.isinsert || trigger.isupdate||trigger.isundelete){
for(contact con : trigger.new){
setid.add(con.AccountId);
}
}
if(trigger.isdelete || trigger.isupdate){
for(contact con : trigger.old){
setid.add(con.AccountId);
}
list<Account> acclist = [select id,No_of_contats__c,(select id from contacts) from Account where id=: setid];
for(Account ac: acclist){
Integer count = ac.contacts.size();
ac.no_of_contats__c=count;
aclist.add(ac);
}
}
update aclist;
}
}