function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rakesh ERakesh E 

Before insert trigger of parent is getting called when doing insert operation on child object

 Hi,

 

 we have one after insert trigger on Parent object. in this trigger we are creating child records for this parent record and inserting them.

 

but when we do insert operation on child records its calling "Before Update" trigger of Parent object.

this is a Master-Detail relationship

 

could any one please tell what is happening here.

 

thanks in advance

 

Regards

Rakesh

Vinit_KumarVinit_Kumar

Hi ,

 

I think you have a roll up summary field o parent object which is being updated while you are iserting child records.Please check for the roll up sumary field on parent and if that is there ,then that is the root cause.

Rakesh ERakesh E

Thank you for an instant reply.

 

please could you tell me what could be a solution/work around for this problem. it will be a great help.

can we make it not happen that way. beacuse its creating some probelm by calling triggers recursively.

 

Regards

Rakesh

Vinit_KumarVinit_Kumar

Hi,

 

I am not sure about your business need.But,if you want to prevent Triggers being invoked recursively,you should Triggerhelper class.

 

public class TriggerHelperClass {   

    private static boolean flagvalue = false;


    public static boolean hasAlreadyfired() {
        return flagvalue;
    }
    
    public static void setAlreadyfired() {
        flagvalue = true;
    }

}

Then use this class like below in your Apex trigger :-

 

trigger recursivetrigger on Task (before insert) {


if (!TriggerHelperClass.hasAlreadyfired()) {

// do your code here

TriggerHelperClass.setAlreadyfired();

}
}

 



 

In your trigger use like below :-