I have written a triggers on object Job__c as well as Employee__c, so that when an Job record is created an Employee record would be created an vice-verce

createEmp: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, createJob: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0 with id a025i000006jOCEAA2; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

trigger createEmp on Job__c (after insert, after update) {
    List<Employee__c> empList = new List<Employee__c>();
    for(Job__c job: Trigger.new){
        Employee__c emp = new Employee__c();          
        if(job.counter__c == false){
            emp.Job__c = job.id; //lookup
            emp.Name = job.EmployeeName__c;
            emp.Salary__c = job.Salary__c;
    insert empList; 
trigger createJob on Employee__c (after insert, after update) {
    List<Job__c> jobList = new List<Job__c>();
    for(Employee__c emp: Trigger.new){
        Job__c job = new Job__c();
        if(emp.counter__c == false){
            job.id = emp.Job__c;
            job.EmployeeName__c = emp.Name;
            job.Salary__c = emp.Salary__c;
    insert jobList;
  • What does the field counter__c indicate for each object?
    – Moonpie
    Commented Sep 5, 2022 at 18:29

1 Answer 1


Welcome to Salesforce Stack Exchange (SFSE)!

Firstly: You do not seem to be doing any check to see if a related record of the other object already exists before you insert it. Is that a possibility that you need to check for? Or is there a guaranteed 1-to-1 relationship between Job & Employee? (If that 1-to-1 relationship is NOT guaranteed, you might want to consider a different approach entirely.)

Secondly: In your createJob on Employee__c trigger, in the first line of your if statement you are explicitly setting the Id for the object you are about to insert. In Salesforce the Id is created by the system upon insertion - you cannot specify the Id of a record you want to create.

Going by the code from your first trigger, you might want that line to be...

job.Emp__c = emp.Id;

...or you may need to figure out whatever other logice. But explicitly setting the job.Id when creating the Job record is not going to work.

Thirdly: Once you get it working, you should consider moving all of your trigger logic to helper classes. You can find plenty about that all over SFSE, Trailhead and the general internet.

You must log in to answer this question.

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