0

I am trying to apply first run into my trigger class as below:

public class testclass{
    public static boolean firstrun = true;

    if (firstrun) {
        if (Trigger.isInsert || Trigger.isUpdate) {
           <<Execute Code here>>
           fisrtrun = false;
        }
   }
}

While running upsert operation, above class code does not run for update as the value for firstrun become false after running insert.

Is there any other practice we can follow to apply so that apex class does not run more than once.

3
  • Try using 2 variable one for update one for insert and move the code to a method and call them with 2 if block of Trigger.isInsert ,Trigger.isUpdate Commented Jun 20, 2017 at 15:12
  • 1
    It sounds like it is doing what you want. Blocking after it ran during insert
    – Eric
    Commented Jun 20, 2017 at 15:55
  • I am with @Eric on this one, if you only want it to run once it appears to be doing what you want. Actually, you could set the firstrun variable to false to ensure the execution method is only triggered one time.
    – zgc7009
    Commented Jun 20, 2017 at 19:07

1 Answer 1

0

I am trying to say below, this it's not a new style or alternative but may solve your issue.

public class testclass{
    public static boolean firstrunInsert = true;
    public static boolean firstrunUpdate = true;
    if (firstrunInsert && Trigger.isInsert) {
        fun();
        firstrunInsert = false;
   }
   if (firstrunUpdate && Trigger.isUpdate) {
           fun();
           firstrunUpdate = false;
   }
   public void fun(){
    <<Execute Code here>>
   }
}
3
  • Unfortunately, it did not works now. While upserting, there are two records in csv file (one for insertion and another for update). The trigger.isInsert is called for records to be inserted. Subsequently, trigger.isUpdate is called for the insert records (after workflows). So, after first record is inserted properly then both trigger.isInsert and trigger.isUpdate are called and both variables (firstrunInsert & firstrunUpdate) become false. As a result, second record is not processed at all. Is there any other way to proceed in this scenario?
    – Vijay Garg
    Commented Jun 27, 2017 at 11:49
  • I think you need to modify the logic and need to skip the workflow field update on new and implement all logic inside the insert trigger. Commented Jun 27, 2017 at 14:52
  • Yes, it seems a feasible option
    – Vijay Garg
    Commented Jun 29, 2017 at 6:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .