Trigger to update parent object value -- w3web.net

Trigger to update count of child records with custom field of parent object | count of child records on parent for lookup relationship uses of apex trigger in Salesforce

1,030 views

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

Final Output

Trigger to update parent object value -- w3web.net

Other related post that would you like to learn in Salesforce

Step 1:- Apex Class Trigger : EmployeeSizeTrigger.apxt

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

EmployeeSizeTrigger.apxt [Apex Class Trigger]

 

Further post that would you like to learn in Salesforce

 

 

FAQ (Frequently Asked Questions)

Can we update parent to child in process builder?

You can use process builder to update parent record from child. Create a process builder on child object and update parent object, here I am using account and contact. Created process builder on contact and updating account status field when contact status is rejected.

Can we update child record using workflow?

Salesforce allows updating of the parent fields through a workflow on child object.

Can we update parent record using workflow?

For updating related records, Process Builder can update any field on any related record, where Workflow can only update some fields on a parent record of a Master-Detail relationship. Process Builder can also update multiple related records in a situation when all of a record's child records need the same update.

Related Topics | You May Also Like

  • Your reaction of the article ▾
 

Hi, I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net

13 thoughts on “Trigger to update count of child records with custom field of parent object | count of child records on parent for lookup relationship uses of apex trigger in Salesforce”

  1. You’re a beautiful inspiration. It really helps me in any situation. Where I stuck. Many of my friends told me to comment there post but I stuck what I should comment. Finally I got your post it always help me. Thanks for the lovely post.

    Reply

Leave a Comment