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
nilesh walkenilesh walke 

Populate the Billing State as California for all the Accounts with Country as USA if Billing State information is blank

please if you  are able to write it explain it littel bit in few words we need to write batch apex.
and the secound condition is 
Every morning at 7 AM for account we have to create Task record by using Batch class

 
Best Answer chosen by nilesh walke
Suraj Tripathi 47Suraj Tripathi 47
Hi Nilesh
Greetings!

This is Batch class. you have to schedule this class.
public class CreateTaskAndPopulateState implements Database.Batchable<sObject>,Schedulable {
    public Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator([SELECT Id, BillingState FROM Account WHERE BillingCountry = 'USA' AND BillingState = NULL]);
    }
    public void execute(Database.BatchableContext BC, List<Account> accList){
        List<Task> taskList = new List<Task>();
        for(Account acc : accList){
            acc.BillingState = 'California';
            Task tsk = new Task();
            tsk.WhatId = acc.Id;
            tsk.Subject = 'Email';
            tsk.Status = 'Not Started';
            tsk.Priority = 'Normal';
            taskList.add(tsk);
        }
        UPDATE accList;
        INSERT taskList;
    }
    public void finish(Database.BatchableContext BC){
    }
    
    public void execute(SchedulableContext sc) {
        CreateTaskAndPopulateState obj = new CreateTaskAndPopulateState();
        Database.executeBatch(obj, 200);
    }
}
If you want to schedule this batch to run daily at 7 AM, please run this code in the anonymous window
System.schedule('Job Name', '0 0 7 1/1 * ? *', new CreateTaskAndPopulateState());
If you find this useful please mark it as the best answer.
Thank you!

Regards,
Suraj Tripathi

All Answers

Suraj Tripathi 47Suraj Tripathi 47
Hi Nilesh
Greetings!

This is Batch class. you have to schedule this class.
public class CreateTaskAndPopulateState implements Database.Batchable<sObject>,Schedulable {
    public Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator([SELECT Id, BillingState FROM Account WHERE BillingCountry = 'USA' AND BillingState = NULL]);
    }
    public void execute(Database.BatchableContext BC, List<Account> accList){
        List<Task> taskList = new List<Task>();
        for(Account acc : accList){
            acc.BillingState = 'California';
            Task tsk = new Task();
            tsk.WhatId = acc.Id;
            tsk.Subject = 'Email';
            tsk.Status = 'Not Started';
            tsk.Priority = 'Normal';
            taskList.add(tsk);
        }
        UPDATE accList;
        INSERT taskList;
    }
    public void finish(Database.BatchableContext BC){
    }
    
    public void execute(SchedulableContext sc) {
        CreateTaskAndPopulateState obj = new CreateTaskAndPopulateState();
        Database.executeBatch(obj, 200);
    }
}
If you want to schedule this batch to run daily at 7 AM, please run this code in the anonymous window
System.schedule('Job Name', '0 0 7 1/1 * ? *', new CreateTaskAndPopulateState());
If you find this useful please mark it as the best answer.
Thank you!

Regards,
Suraj Tripathi
This was selected as the best answer
nilesh walkenilesh walke
Class WhenCountryChanges must implement the method: void System.Schedulable.execute(System.SchedulableContext)

 Hi suraj why this error is comming!?
Suraj Tripathi 47Suraj Tripathi 47
In the last method of the class.

You are using-
public void execute(SchedulableContext sc) {
    CreateTaskAndPopulateState obj = new CreateTaskAndPopulateState();
    Database.executeBatch(obj, 200);
}
Now, Use this-
public void System.Schedulable.execute(System.SchedulableContext sc) {
    CreateTaskAndPopulateState obj = new CreateTaskAndPopulateState();
    Database.executeBatch(obj, 200);
}
Thank you!
Regards,
Suraj Tripathi