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
ch ranjithch ranjith 

Not updating field in student with Batch apex Please help me

global class batchstudent implements Database.Batchable<sobject>
{
global final String Query;
global batchstudent(String q)
{
Query=q;
}

global Database.QueryLocator start(Database.BatchableContext BC)
{
return Database.getQueryLocator(query);
}

global void execute(Database.BatchableContext BC,List<student__C> scope)
{
List <student__C> lstAccount = new list<student__C>();
for(Sobject s : scope)
{
student__C a = (student__C)s;
a.status__c='updated from batch apex';
system.debug(a);
}
update scope;
}

global void finish(Database.BatchableContext BC)
{
asyncapexjob a=[select id,status,numberoferrors,jobitemsprocessed,totaljobitems
                 from asyncapexjob where id=:bc.getjobid()];
            
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'ranjith.imp@gmail.com'};
mail.setToAddresses(toAddresses);
mail.setSubject('Apex Batch Job is done');
mail.setPlainTextBody('The batch Apex job processed '+a.totaljobitems+'batches with'+a.numberoferrors+'failures');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}

developer console:
id batchinstanceid = database.executeBatch(new deleteAccounts('select Id from student__c'));
pbattissonpbattisson
Please do not duplicate issues as it makes it harder for people to respond to them for you and help you out.

Duplicated from https://developer.salesforce.com/forums/ForumsMain?id=906F0000000AcGMIA0 please use original post.
Deepak Kumar ShyoranDeepak Kumar Shyoran
Please modify your execute method as you are updating the "Scope" with any modification in it use below code and try it again.

global void execute(Database.BatchableContext BC,List<student__C> scope)
{
List <student__C> lstAccount = new list<student__C>();
for(Sobject s : scope)
{
student__C a = (student__C)s;
a.status__c='updated from batch apex';
lstAccount.add(a) ;
system.debug(a);
}
update lstAccount;
}

Please mark my answer as a best solution to your question to help others if it solves your problem.