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
rgibbonsrgibbons 

Help with Custom Trigger on Lead Object to Create Task

Hi all,

We're running into a little bit of trouble with a custom APEX trigger on Lead Update or Creation.  We're trying to pull 3 values from custom fields on the Lead Object and create a new task and then NULL out the custom fields on the Lead record.

So far, we're able to generate the new task, but we are running into referencing issues with the custom Lead fields and we receive a "Comparison arguments must be compatible types" on the if statement.

Also when we try to simply insert the values using the Lead.variable_name references into the Subject of the task as a test it inserts as a string with a value of "variable_name."

We're unable to do this using a Workflow Rule, because you can't insert values into custom fields on Tasks using WFRs.

Any suggestions on what we're doing wrong?

Thanks for your help.

Ryan

trigger AfterInsertUpdateLead on Lead (after insert, after update)
{
 list<Task> NewLitTask = new list<Task>();
 for(integer i=0; i<trigger.new.size(); i++)
 {
   Lead lead = new Lead();
  if (lead.Number_of_Gap_Catalogs__c > 0 || lead.Number_of_Group_Catalogs__c > 0 || lead.Number_of_HS_Catalogs__c > 0)
  {
   NewLitTask.add(new Task(
   Subject = 'Lit Request',
   WhoID = trigger.new[i].id
   ));
  }
 }
 insert NewLitTask;
}

 

 

davidjgriffdavidjgriff
First problem I see is that the lead you are checking in your if statement is just an empty object. You'll never have any tasks inserted because none of those statements will ever be true.

Also, what field type are those that are used in the if statement?
sfdcfoxsfdcfox

You're trying to compare a field definition to a numeric value (lead.number_of_gap_catalogs__c expands to schema.lead.number_of_gap_catalogs__c, which is a static variable of type sobjectfield).

 

The code you actually need is far simpler than what you've written:

 

trigger afterinsertupdatelead on lead (after insert, after update) {
  task[] tasks = new task[0];
  for(lead record:trigger.new) {
    if( record.number_of_gap_catalogs__c>0 || lead.number_of_group_catalogs__c>0 ||
         record.number_of_hs_catalogs__c>0) {
      tasks.add(new task(subject='Lit Request',status='Not Started',whoid=record.id));
    }
}
insert tasks; }