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
Austin GutzAustin Gutz 

Create Tasks with Apex

Our accounting controller would like to assign tasks to different users for any account that's past due. 

Basically, every Monday I need tasks auto-generated for any account where past_due_amount__c > $0. The task would be auto assigned to the user in the collections_owner__c field found on the account with a due date of the end of the week. 

If Apex, can someone help me with that?
Anyone have a better idea? 

Here is what I have so far.
global class WeeklyAccountingTasks implements schedule{
    global void execute(schedulableContext ctx){
        List<account> Accts = [SELECT Id, Name, Collection_Ownership__c, Past_Due_Amount__c
            FROM Account
            WHERE Past_Due_Amount__c > 0];
        // Create a task for each account in the list
    }

}

 
Raj VakatiRaj Vakati
try this code
 
global class WeeklyAccountingTasks implements schedule{
    global void execute(schedulableContext ctx){
        List<account> Accts = [SELECT Id, Name, Collection_Ownership__c, Past_Due_Amount__c
            FROM Account
            WHERE Past_Due_Amount__c > 0];
			
			List<Task> insertTask = new List<Task>();

for(Case account : Accts)
{
Task newTask = new Task();
newTask.subject = 'New Account Task';
newTask.whatId = account.Id;
newTask.whoId=account.Collection_Ownership__c;
newTask.ownerId = account.Collection_Ownership__c;
newTask.status = 'In progress';
newTask.Priority = 'Normal';
insertTask.add(newTask);
}
if(insertTask.size() > 0)
insert insertTask;
}


    }

}

 
Austin GutzAustin Gutz
Hi Raj,

I'm getting the following error:

Error: Compile Error: Invalid loop variable type expected Account was Case at line 9 column 1