• kittydalt
  • NEWBIE
  • 0 Points
  • Member since 2011

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

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