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