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
Waqar Hussain SFWaqar Hussain SF 

Trigger to create child records, I am getting error while I create a record. Here is my trigger

trigger createSal on Employee__c (after insert, after update) {

       List<Salary__c> sal = new List<Salary__c>();

    for (Employee__c newEmp: Trigger.New) {
        if (newEmp.Name != null) {
            sal.add(new Salary__c(Id = newEmp.Id, Bonus__c=100,Monthly_Salary__c = 12000));
        }
    }
    insert sal;

}
Best Answer chosen by Waqar Hussain SF
NekosanNekosan
You cannot add 'id' in insert. You need to replace it with correct field api name for employee. Try removing it.

All Answers

NekosanNekosan
Id = newEmp.Id is wrong. You should give newempy.id to employee id field. 
sal.add(new Salary__c(Id = newEmp.Id, Bonus__c=100,Monthly_Salary__c = 12000));

 
Cloud_forceCloud_force
sal.add(new Salary__c(Id = newEmp.Id, Bonus__c=100,Monthly_Salary__c = 12000));
you cannot set id of a record.. i think you should replace id with your employee look up field api name.

thanks,
http://www.forcexplore.com/2014/07/salesforce-adm-201-dumps-part-14.html
Waqar Hussain SFWaqar Hussain SF
It gives me this error..
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger createSal caused an unexpected exception, contact your administrator: createSal: execution of AfterInsert caused by: System.TypeException: Invalid id value for this SObject type: a0V9000000JYW2iEAH: Trigger.createSal: line 7, column 1
NekosanNekosan
You cannot add 'id' in insert. You need to replace it with correct field api name for employee. Try removing it.
This was selected as the best answer
Waqar Hussain SFWaqar Hussain SF
Is this correct?
sal.add(new Salary__c(Employee__c = newEmp.Id, Bonus__c=100, Monthly_Salary__c = 12000));
NekosanNekosan
Yup.
I found this link - http://developer.force.com/cookbook/recipe/creating-a-child-record-when-a-parent-record-is-created. 
Hope this helps.