1

I want to get the ID of the record in Trigger.isBefore && Trigger.isInsert. I need to start a batch process based on this ID. It works for Trigger.isUpdate but does not work for Trigger.isInsert.

Below is my code:

if(Trigger.isBefore && Trigger.isInsert)
{
  onbeforeInsert((list<MyCustomObj__c>) trigger.new,(map<id, MyCustomObj__c>) trigger.OldMap);
}

This does not work. Can anyone please help me here?

I am getting the below exception:

System.QueryException: invalid ID field: null

I have tried using AfterInsert Trigger as below:

 if (Trigger.isAfter && Trigger.isInsert)
 {
     onAfterUpdate((list<MyCustomObject__c>) trigger.new,(map<id, MyCustomObject__c>) trigger.OldMap);
 }

private void onAfterUpdate(list<MyCustomObject__c> lsttriggernew,map<id,MyCustomObject__c> triggeroldmap)
{            
    startBatch(lsttriggerNew,triggeroldmap);            
}

I have a method StartBatch where I am updating a field value of the same object.

public static void startBatch(list<MyCustomObject__c> lsttriggernew, map<id,MyCustomObject__c> triggeroldmap)
{
    MyField__c = 'Progress';
    //Then calling the batch job here
}

When I use AfterInsert Trigger, I am getting below error-

Apex trigger caused an unexpected exception, contact your administrator: execution of AfterInsert caused by: System.FinalException: Record is read-only: Class.TriggerHandler.startBatch

4
  • 2
    The ID doesn't exist in before insert because you haven't created the record yet. If you want your batch to process inserted records, you have to call it from the after insert trigger. Sep 17, 2018 at 1:57
  • When I try the same in AfterInsert, I am getting below error: Apex trigger caused an unexpected exception, contact your administrator: execution of AfterInsert caused by: System.FinalException: Record is read-only: Class.TriggerHandler.startBatch: line 58, column 1
    – Ramy
    Sep 17, 2018 at 2:01
  • Please edit your original post to include this error message and the relevant code from your TriggerHandler that is causing the error. Sep 17, 2018 at 2:04
  • Sure. I just updated the post to include all the details.
    – Ramy
    Sep 17, 2018 at 2:15

1 Answer 1

2

As you've found, you don't get an Id until after insert, and you can only make changes to records in Trigger.new in a before trigger. (See documentation on Context Variable Considerations for more details.)

There are several ways around this. One way is to use DML to update the record in the after insert trigger. Another way around this is a bit simpler and less likely to affect limits -- update the field in the before trigger, and run the batch job in the after trigger. Ideally, they should be the same trigger, so it's clear what's happening. E.g.:

trigger RunTheBatch on MyCustomObject__c (before insert, after insert) {
  if (Trigger.isBefore) {
    for (MyCustomObject__c mco : Trigger.new) {
      mco.MyField__c = 'Progress';
    }
  } else {
    startBatch(Trigger.new, Trigger.oldMap);
  }
}

You must log in to answer this question.

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