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

Trigger to create task on a new lead - don't have lead id to pass to task whois

I need to create a new TASK and attach it to the LEAD when a NEW LEAD is created.  I have a trigger on the lead that uses BEFORE INSERT event.  It has to be before insert becuase I must update a field on the Lead during the process (if I use after insert i receive a read-only error).


Since the trigger is before insert, the lead hasn't been created and there for does not have an Id.  I need that Lead.Id to put into Task.Whois field.


Question: How can I update the new task with the new lead id after the trigger has created the lead???


THANKS is advance!!!!


Here is my code on the Lead trigger....


trigger trigger_create_task_callback_lead on Lead (before insert, before update) {

List<Task> insertTask = new List<Task>();
Map<ID, User> mapUser = new Map<ID, User>([SELECT Id, Name, Email FROM User]);


If (thisLead.Create_Task__c == true){

Task newTask = new Task();
//newTask.ActivityDate = "2013-08-16";
newTask.CallDisposition = 'Future Callback';
newTask.Description = 'This is a test';
newTask.Priority = 'Normal';
newTask.Status = 'Not Started';
newTask.Subject = 'Callback';
//newTask.WhatId =
newTask.WhoId = thisLead.Id;


System.debug('>>>>>>>>>>>>>>>>>>>>> ' + thisLead.Create_Task__c);
System.debug('~~~~~~~~~~~~~~~~~~~~~ ' + newTask);
System.debug('+++++++++++++++++++++ ' + thisLead.Id);

// Reset Create Flag
thisLead.Create_Task__c = false;

} // end if

If (thisLead.Create_Five9_Callback__c == true){

User Agent = mapUser.get(thisLead.OwnerID);

//Initialize Email Address
String agentEmail = '';

if(Agent!= null) {
agentEmail = Agent.Email;

// Reset Create Flag
thisLead.Create_Five9_Callback__c = false;

Five9_W2C_HTTP_POST.getHTTPPOST(String.valueOf(thisLead.Id), thisLead.FirstName, thisLead.LastName, agentEmail, 'taskId', thisLead.Company, thisLead.Phone);

} // end if

} // end for

list<Task> updateTaskList = new list<Task>();

//Bulk insert
if (insertTask.size()>0) {
List<Database.SaveResult> lsr = Database.insert(insertTask,false);
for(Database.SaveResult sr:lsr) {

System.Debug(' -------TASK INSERTED ------' + lsr);

Database.Error err = sr.getErrors()[0];
} // end if

} // end for

} // end if

} // end trigger


Unless I'm missing something, what you're looking to do can be achieved with standard Salesforce functionality with Workflow Rules + Task creation


First, thank you for responding.  I apprecaite it!!


You are 100000% right - I can do this with a workflow rule...create task, however I need to send the newly created taskid to a class that makes an HTTP post.  I also would like to dynamically assign the OwnerID.  Any thoughts on how I can accomplish that?



The HTTP posting will need to be done with a trigger which should be fairly straight-forward. You'll want it to fire on "after insert" for object "Task" and if it meets certain criteria, get the parent Lead via WhatId and then you'll have the information you'll need for your post.


As far as dynamic owner assignment, do you simply mean assign the Task to the owner that created the Lead? Again, that can be done with standard SF configuration.

Team WorksTeam Works


You can use after trigger on Lead. To update the field on Lead, you can create a workflow on Task..I hope this helps..