Trigger to update count of child records with custom field of parent object

Vijay Kumar
2 min readAug 12, 2020
Trigger to update count of child records with custom field of parent object

Hey guys, today in this post we are going to learn about how to update count of child records with custom field value into parent object using trigger map list 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 employee size, rest records of child object should be automatic removed.

parentObjTrigger:- Custom Fields & Relationships

Create a custom field on parent Object name as Employee__c (Number Type).

childObjTrigger:- Custom Fields & Relationships

Create a lookup relationship field on child Object name as childLookup__c :: Lookup(parentObjTrigger).

Files we used in this post example:-

EmployeeSizeTrigger.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 thecustom 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.

Final Output

Other post that would you like to learn

Step 1:- Apex Class Trigger : EmployeeSizeTrigger.apxt

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

Originally published at https://www.w3web.net on August 12, 2020.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response