You need to sign in to do that
Don't have an account?
srinivas 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);
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);
You can try the below code,
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);
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);
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);
trigger AccountTrigger on Account(before insert){
for(Account acc : trigger.new){
acc.Name = 'Mr.' + ' ' + acc.Name;
}
}