I am trying to assign tasks to a user based on their profile once the recorded is inserted into Salesforce.
The trigger is hitting an exception even though it's compiling correctly. The code is below:
trigger insertTasks on User (after insert) {
// get list of users needed to assign
List<User> userList = [SELECT Id FROM User where ProfileId = '00e4x000000m2tPAAQ' LIMIT 10];
List<Objectives__c> objList = [SELECT Id FROM Objectives__c];
//loop thru users in the users list created above.
for(User newUser : Trigger.new){
//create new objectives
Objectives__c obj = new Objectives__c();
obj.OwnerId = userList.get(0).Id;
obj.Status__c = 'Not Started';
}
if(userList.size() > 0)
{
// insert objectives into a list for the new users with the specified profile
insert objList;
}
The error is saying that INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
The goal here is to assign a list of objectives to the new user that gets assigned the correct profile but only if they do not already have any objectives.