Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

When should I use a database.stateful?

Asked by David Piper Feb 8, 2023 1.4K views 1 answer
Share

About this question

I am writing a batch class which will get the records whose status picklist value is 'no response' and whose created date is before greater than or equal to 48 hrs and update the status to 'declined'.

Do I need to use Database.stateful here ?

global class SolicitaBatch implements Database.Batchable{
String query;
String email;
Id toUserId;
Id fromUserId;
global Database.QueryLocator start(Database.BatchableContext BC)
    {
        Datetime dtime = datetime.now().addhours(-48);
        String query = 'select id,createdDate, Status__c from Solicitation__C  where createdDate <= :dtime and Status__c = 'No Response' ';
        return Database.getQueryLocator(query);
    }
global void execute(Database.BatchableContext BC, List scope){
    List Sols = new List();
    for(Solicitation__C s : scope)
    {
        s.Status__c = 'ABCD';
    }
    update scope;
} 
global void finish(Database.BatchableContext BC)
    {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(new String[] {email});
        mail.setReplyTo('batch@acme.com');
        mail.setSenderDisplayName('Batch Processing');
        mail.setSubject('Batch Process Completed');
        mail.setPlainTextBody('Batch Process has completed');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}

Your answer

1 Answer

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.