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
srinivas pulipatisrinivas pulipati 

Hi i am writing batch apex code my requirement is every account name before insert 'Mr.' but it's not update could u please tell me wre is the mistake?

Batch Apex Code:
global class BatchApex implements Database.Batchable<sobject> {
    global DataBase.QueryLocator start(DataBase.BatchableContext bc){
        String query ='select id,name from account';
        return DataBase.getQuerylocator(query);
    }
    global void execute(DataBase.BatchableContext bc,List<Account> scope){
        List<Account> acc=new list<Account>();
        for(Account a:scope){
            a.name ='mr.'+a.name;
            scope.add(a);
        }
        update scope;
    }
    global void finish(DataBase.BatchableContext bc){
        
    }
        }

BatchApex b =new BatchApex();
Database.executeQuery(b,5);
Arunkumar RArunkumar R
Hi Srinivas,

    You can try the below code,
global class BatchApex implements Database.Batchable<sobject> {
    global DataBase.QueryLocator start(DataBase.BatchableContext bc){
        String query ='select id,name from account';
        return DataBase.getQuerylocator(query);
    }
    global void execute(DataBase.BatchableContext bc,List<Account> scope){
        List<Account> acc=new list<Account>();
        for(Account a:scope){
            a.name ='mr.'+a.name;
            acc.add(a);
        }
        update acc;
    }
    global void finish(DataBase.BatchableContext bc){
        
    }
 }

1. Update a newly created list instead of scope list.
2. Execute batch using the below snippet,

BatchApex b =new BatchApex();
Database.executeBatch(b,5);
srinivas pulipatisrinivas pulipati
Tq Arunkumar
Jagadish LutimathJagadish Lutimath
global class BatchApex implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext BC) {
String query = 'SELECT Id,Name FROM Account';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Account> scope) {
for(Account a : scope)
{
a.Name = a.Name + '******';
}
update scope;
}
global void finish(Database.BatchableContext BC) {
}
}




Execute batch using the below snippet,

BatchApex b =new BatchApex();
Database.executeBatch(b,5);
 
Jagadish LutimathJagadish Lutimath
Use a.name = 'Mr.' + a.name
souvik9086souvik9086
global class BatchApex implements Database.Batchable<sobject> {
    global DataBase.QueryLocator start(DataBase.BatchableContext bc){
        String query ='select id,name from account where isdeleted = false';
        return DataBase.getQuerylocator(query);
    }
    global void execute(DataBase.BatchableContext bc,List<sObject> scope){
        List<Account> accListToBeUpdated =new list<Account>();
        for(sobject s : scope){
            Account accObj = (Account)s;           
            accObj.name ='mr.'+accObj.name;
            accListToBeUpdated.add(accObj);
        }
        if(accListToBeUpdated.size() > 0){
            update accListToBeUpdated;
        }
    }
    global void finish(DataBase.BatchableContext bc){
        
    }
 }


Execute batch using the below snippet,

BatchApex b =new BatchApex();
Database.executeBatch(b,5);
 
Vishal Negandhi 16Vishal Negandhi 16
Once you have the existing data updated, I would suggest you write a trigger for this. As it will let you update the record while it gets created. A simple before insert trigger should help. 

trigger AccountTrigger on Account(before insert){
      for(Account acc : trigger.new){
           acc.Name = 'Mr.' + ' ' + acc.Name;
      }
}