You need to sign in to do that
Don't have an account?

Insert not working, Could I be using it wrong?
I am trying to insert a bunch of tasks programicly. Everytime i try to insert more then one I get an error. Can anyone Explain what the error means and what I am doing wrong? It would really help thanks.
Apex for Inserting:
trigger Account_Task_Trigger on Account (before insert, before update) { List<Account> newAccount = trigger.new; List<Contact> contactList = [SELECT Name, Id, AccountId, Title FROM Contact WHERE Title LIKE '%Director%' OR Title LIKE '%Manager%']; List<Contact> contacts = new List<Contact>(); Task newTask = new Task(); List<Task> taskList = new List<Task>(); for(Account A : newAccount){ if(A.TAP__c == 'Reviewed - TAP'){ for(Contact C : contactList){ if(A.Id == C.AccountId) { contacts.add(C); } } } } for(Contact c : contacts){ newTask.Type = 'TAP'; newTask.Subject = 'Call'; newTask.Priority = 'Normal'; newTask.Status = 'Not Started'; newTask.WhoId = c.Id; newTask.WhatId = c.AccountId; taskList.add(newTask); } for(Task T : taskList){ insert T; } }
Error I get:
Force.com Sandbox: Developer script exception from Solving IT : Account_Task_Trigger : Account_Task_Trigger: execution of BeforeUpdate caused by: System.DmlException: Insert failed. First exception on row 0 with id 00TQ0000008pucSMAQ; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id] Trigger.Account_Task_Trigger: line 30, column 3
In this loop, you only have one newTask instance, you a new one for each contact (add a newTask = new Task() inside the loop.
All Answers
Same issue here. Any help in this regard would be appreciated!
In this loop, you only have one newTask instance, you a new one for each contact (add a newTask = new Task() inside the loop.
If you are creating new task for each contact
Task newTask = new Task(); should be inside the for loop ???
for(Contact c : contacts){
newTask.Type = 'TAP';
newTask.Subject = 'Call';
newTask.Priority = 'Normal';
newTask.Status = 'Not Started';
newTask.WhoId = c.Id;
newTask.WhatId = c.AccountId;
taskList.add(newTask);
}