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
Jacob Elliott 1Jacob Elliott 1 

Apex Trigger to create new record throwing this error:execution of AfterInsert caused by: System.SObjectException: DML statement cannot operate on trigger.new or trigger.old ()

I am new to Apex and would really appreciate any help I can get! I have an Object called Interview and when a user creates an Interview Task with a record type of Interview, I want a new Interview Record to be created. When trying to save the new interview record, I get this error: "execution of AfterInsert caused by: System.SObjectException: DML statement cannot operate on trigger.new or trigger.old ():. 

Below is my Code
 
trigger CreateInterview on Task (after insert) {
    for (Task t : Trigger.new) {
        Interview__c i                          = new Interview__c();
        i.CreatedById                          = t.OwnerId;
        i.Candidate__c                        = t.Candidate__c;
        i.Interview_Date__c                = t.ActivityDate;
        i.Job_Position_Text__c           = t.Job_Position__c;
        i.Hiring_Manager__c               = t.Hiring_Manager__c;
        i.Hiring_Location__c                = t.Hiring_Location__c;
        i.Hiring_Unit_Department__c  =  t.Hiring_Unit_Department__c;
        i.Id                                            = t.Interview__c;
        i.Comments__c                        = t.Description;
        i.Name                                     = t.Subject;
        i.Id                                            = t.WhatId;
        insert t;
    }
}

User-added imageUser-added image
Best Answer chosen by Jacob Elliott 1
Raj VakatiRaj Vakati
You need to insert the Interview__c  and Task so change insert t to insert i and more over you cannt insert the same record on after insert ..

All Answers

Raj VakatiRaj Vakati
Change is as below
 
trigger CreateInterview on Task (after insert) {
    for (Task t : Trigger.new) {
        Interview__c i                          = new Interview__c();
        i.CreatedById                          = t.OwnerId;
        i.Candidate__c                        = t.Candidate__c;
        i.Interview_Date__c                = t.ActivityDate;
        i.Job_Position_Text__c           = t.Job_Position__c;
        i.Hiring_Manager__c               = t.Hiring_Manager__c;
        i.Hiring_Location__c                = t.Hiring_Location__c;
        i.Hiring_Unit_Department__c  =  t.Hiring_Unit_Department__c;
        i.Id                                            = t.Interview__c;
        i.Comments__c                        = t.Description;
        i.Name                                     = t.Subject;
        i.Id                                            = t.WhatId;
        insert i;
    }
}

 
Raj VakatiRaj Vakati
You need to insert the Interview__c  and Task so change insert t to insert i and more over you cannt insert the same record on after insert ..
This was selected as the best answer