Apex trigger to count child records -- w3web.net

Apex trigger to create and count the number of child records associated with parent object based on custom field when parent record is created/updated in Salesforce

Hey guys, today in this post we are going to learn about Apex trigger to create and count the number of child records associated with parent object based on custom field when parent record is created/updated in Salesforce.

Real time scenarios:- Write a trigger on parent object where create a custom field as Employee (Number type).

If user insert the parent record, the child record should be automatic inserted equal to the same value of employee.

Or if user update the value of employee less then the total number of child records, in this case the child records should be exist only equal to value of employee size, rest records of child object should be automatic removed.

Files we used in this post example

EmployeeSizeTrigger2.apxt Apex Class Trigger It will be fired whenever insert or update a record on parent object

Parent Cusotm Object:- parentObjTrigger__c

Custom Field:- EmployeeName__c

Parent Object with Custom Field Trigger on parent object and updae the count of related child records with the custom field value of parent object.

Child Custom Object:- childObjTrigger__c

Custom Field:- childLookup__c : Lookup(parentObjTrigger)

Child Object with Custom Field childLookup__c : Lookup(parentObjTrigger) field it is help to communicate on parent object.

Live Example

Trigger to update parent object value -- w3web.net

Other related post that would you like to learn.

Step 1:- Apex Class Trigger : EmployeeSizeTrigger2.apxt

From Developer Console >> File >> New >> Apex Trigger

EmployeeSizeTrigger2.apxt [Apex Class Trigger]

Apex trigger to count child records -- w3web.net

TechW3web:- Apex Trigger to Update Count of Child Records into Parent Object Custom Field in Salesforce

Further post that would you like to learn in LWC

2 thoughts on “Apex trigger to create and count the number of child records associated with parent object based on custom field when parent record is created/updated in Salesforce”

Leave a Comment

Please Contribute to Support If Some Value Added in Your Project or Website