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
hkp716hkp716 

Cant assign task using Apex Class

Hi everyone,

 

I cant get my scheduled aplex class to assign a task to the lead.  How do i check if my list, setfollow, is populating with any lead records?  And am I using the proper syntax to insert a task and assign to the lead owner?

 

The scheduler works fine and i see the job in monitoring.

 

-hkp716

 

Apex Class:

 

global class leadfollowup30daystest2 implements Schedulable{

global void execute(SchedulableContext sc)
{
leadfollowup30daystest2 q = new leadfollowup30daystest2();
        String sch = '0 25 16 * * ?';
        system.schedule('30 day lead followup test2', sch, q);
}
public static Date todayMinusThirty = system.Today().addDays(-2);


public static void updateLeads ()
{


//collect a list of records

List<Lead> setfollow = [SELECT Id FROM Lead WHERE (LastActivityDate = :todayMinusThirty OR LastActivityDate = NULL) AND LastModifiedDate = :todayMinusThirty AND ISconverted = FALSE AND Map_Record_Type__c = 'Contract_DirectCustomer' AND (status = 'Working ASM' OR status = 'Contacted' OR status = 'Prospecting' OR status = 'Qualified' OR status = 'Proposal' OR status = 'Negotiation' OR status = 'Sent to ABD' OR status = 'Working Lead Specialist')];

for (Lead ld :setfollow)
{
    Task t = new Task();
    t.Subject = 'Needs Follow up - No Activity in 30 dayss';
    t.Status = 'Not Started';
    t.Priority = 'Normal';
    t.ActivityDate = Date.Today()+7;
    t.OwnerID = ld.OwnerID;
    
   insert t;
}


}
}

Coco_SdyneyCoco_Sdyney

change this part :

for (Lead ld :setfollow)
{
    Task t = new Task();
    t.Subject = 'Needs Follow up - No Activity in 30 dayss';
    t.Status = 'Not Started';
    t.Priority = 'Normal';
    t.ActivityDate = Date.Today()+7;
    t.OwnerID = ld.OwnerID;
    
   insert t;
}

 

to

List<Task> list_newTasks = new List<Task>();

for (Lead ld :setfollow)
{
    Task t = new Task();
    t.Subject = 'Needs Follow up - No Activity in 30 dayss';
    t.Status = 'Not Started';
    t.Priority = 'Normal';
    t.ActivityDate = Date.Today()+7;
    t.OwnerID = ld.OwnerID;
    t.WhoId = ld.Id;
   list_newTasks.add(t);
}

insert list_newTasks;

hkp716hkp716

Thanks for the reply coco_sydney

 

Unfortunately no luck....

 

I tweaked my code so i can single out a lead...

 

Not sure if my query is incorrect or the task assignment.

 

-hkp716

 

Updated apex:

 

global class followupnodate implements Schedulable{

global void execute(SchedulableContext sc)
{
followupnodate q = new followupnodate();
        String sch = '0 15 17 * * ?';
        system.schedule('30 day lead followup test2', sch, q);
}


public static void updateLeads ()
{


//collect a list of records

List<Lead> assignt = [SELECT Id, Lead_Nbr__c FROM Lead WHERE Lead_Nbr__c = 'L-0000005177'];

List<Task> list_newTasks = new List<Task>();
for (Lead ld :assignt)
{
    Task t = new Task();
    t.Subject = 'Needs Follow up - No Activity in 30 dayss';
    t.Status = 'Not Started';
    t.Priority = 'Normal';
    t.ActivityDate = Date.Today()+7;
    t.OwnerID = ld.OwnerID;
    t.WhoId = ld.Id;
   list_newTasks.add(t);
}
insert list_newTasks;


}
}

Coco_SdyneyCoco_Sdyney

:) 

 

didn't see updateLeads() method being called actually, you need to call this method in execute(SchedulableContext sc)

hkp716hkp716

Stil no luck....not sure if i did this correctly....

global class followupnodate implements Schedulable{

global void execute (SchedulableContext sc)
{
followupnodate q = new followupnodate();
        String sch = '0 39 18 * * ?';
        system.schedule('30 day lead followup test2', sch, q);
        updateLeads();
}


public static void updateLeads()
{


//collect a list of records

List<Lead> assignt = [SELECT Id, Lead_Nbr__c FROM Lead WHERE Lead_Nbr__c = 'L-0000005177'];

List<Task> list_newTasks = new List<Task>();
for (Lead ld :assignt)
{
    Task t = new Task();
    t.Subject = 'Needs Follow up - No Activity in 30 dayss';
    t.Status = 'Not Started';
    t.Priority = 'Normal';
    t.ActivityDate = Date.Today()+7;
    t.OwnerID = ld.OwnerID;
    t.WhoId = ld.Id;
   list_newTasks.add(t);
}
insert list_newTasks;


}
}

 

sivaextsivaext

Hi

 

you need to write whole code inside global void execute(ScheduableContext sc).

 

global void execute (SchedulableContext SC ) {

 

/collect a list of records

List<Lead> assignt = [SELECT Id, Lead_Nbr__c FROM Lead WHERE Lead_Nbr__c = 'L-0000005177'];

List<Task> list_newTasks = new List<Task>();
for (Lead ld :assignt)
{
    Task t = new Task();
    t.Subject = 'Needs Follow up - No Activity in 30 dayss';
    t.Status = 'Not Started';
    t.Priority = 'Normal';
    t.ActivityDate = Date.Today()+7;
    t.OwnerID = ld.OwnerID;
    t.WhoId = ld.Id;
   list_newTasks.add(t);
}
insert list_newTasks;

}

 

Scheduling of this have two ways

 

1. you can write below three lines in developer console and excute

   

followupnodate q = new followupnodate();
        String sch = '0 39 18 * * ?';
        system.schedule('30 day lead followup test2', sch, q);

only onetime execution enough. it keep on running according to you

 

2. you can write into construtor of the class

      

    public followupnodate() {

 

    

followupnodate q = new followupnodate();
        String sch = '0 39 18 * * ?';
        system.schedule('30 day lead followup test2', sch, q);

}

    

 

hkp716hkp716

This didnt work either...i tired it both ways

 

 

 

global class followupnodate implements Schedulable{

global void execute (SchedulableContext sc)
{
followupnodate q = new followupnodate();
        String sch = '0 55 8 * * ?';
        system.schedule('30 day lead followup test2', sch, q);



//public void updateLeads()
//{


//collect a list of records

List<Lead> assignt = [SELECT Id FROM Lead WHERE Name = 'testfollowup'];

List<Task> list_newTasks = new List<Task>();
for (Lead ld :assignt)
{
    Task t = new Task();
    t.Subject = 'Needs Follow up - No Activity in 30 dayss';
    t.Status = 'Not Started';
    t.Priority = 'Normal';
    t.ActivityDate = Date.Today()+7;
    t.OwnerID = ld.OwnerID;
    t.WhoId = ld.Id;
   list_newTasks.add(t);

}
insert list_newTasks;

//}
}
}

 

is also tried

 

global class followupnodate implements Schedulable{

global void execute (SchedulableContext sc)
{
//followupnodate q = new followupnodate();
//        String sch = '0 0 9 * * ?';
//        system.schedule('30 day lead followup test2', sch, q);



//public void updateLeads()
//{


//collect a list of records

List<Lead> assignt = [SELECT Id FROM Lead WHERE Name = 'testfollowup'];

List<Task> list_newTasks = new List<Task>();
for (Lead ld :assignt)
{
    Task t = new Task();
    t.Subject = 'Needs Follow up - No Activity in 30 dayss';
    t.Status = 'Not Started';
    t.Priority = 'Normal';
    t.ActivityDate = Date.Today()+7;
    t.OwnerID = ld.OwnerID;
    t.WhoId = ld.Id;
   list_newTasks.add(t);

}
insert list_newTasks;

//}
}
}

 

 

sivaextsivaext

Hi

 

1. what error you getting?

2. are you using class for any special requirement ? you can use trigger to create task.

hkp716hkp716

i forgot to add OwnerID in the query....silly mistake

 

Works now.....thanks for the help

 

-hkp716