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
DannyK89DannyK89 

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

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell

In this loop, you only have one newTask instance, you a new one for each contact (add a newTask = new Task() inside the 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);
	}
And when you get to inserting them, just do
insert taskList
no need to insert them one at a time.

All Answers

kittydaltkittydalt

Same issue here. Any help in this regard would be appreciated!

 

SuperfellSuperfell

In this loop, you only have one newTask instance, you a new one for each contact (add a newTask = new Task() inside the 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);
	}
And when you get to inserting them, just do
insert taskList
no need to insert them one at a time.
This was selected as the best answer
jyovasjyovas

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);
}