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
Dev Rana 16Dev Rana 16 

Batch apex not updateing filed

Hi every one. i m using batch apex to update value. when i run the code it gives me a success msg but cant update filed. plz tell me what is the problem..

My Code
global class b1 implements Database.Batchable<Sobject>{

  global final string query;
  global final string s_object;
  global final string Field;
  global final string field_value;
  
  global b1(string q, string s,string f, string v){
           Query = q;
           s_object = s;
           Field = f;
           field_value = v;
  }
    //start method has a return type of database query locater it will takes a query and return data
     global Database.QueryLocator start(Database.BatchableContext BC){
       return Database.getQueryLocator(query);
     }
     
     //this method processing the data
     global void execute(Database.BatchableContext BC, List<sObject> batch ){
        for(sObject o : batch){
         o.put(Field, field_value); 
        }   
        update batch;
     }
     
     global void finish(Database.BatchableContext BC){
     }  

}
And in anonymous window i m pasing this prameter
String q = 'SELECT Description FROM Account LIMIT 10';
String s = 'Account';
String f = 'Description';
String v = 'updated';
Id batchInstanceId = Database.executeBatch(new b1(q,s,f,v), 5);


 
Nayana KNayana K
Check with debug logs
karthikeyan perumalkarthikeyan perumal
Hello Dev Rana, 

your Code work fine nothing issue in your code. 
in anonymous window 
String q = 'SELECT Description FROM Account LIMIT 10';
String s = 'Account';
String f = 'Description';
String v = 'updated';
Id batchInstanceId = Database.executeBatch(new b1(q,s,f,v), 5);
also makr checkbox (tick) for Open Log like below. 
User-added image
in your Developer Console you able to see the log like belwo check the conform update with this string 
User-added image

after conform these thing go to Account and  and select Recently modified from right side Dropdown see below, 
User-added image

click the first record and check the Desciption Field value. so you came to know  your code works..  

User-added image


hope i helped you Mark Best ANSWER if its work for you. 

Thanks
karthik
 
Harish RamachandruniHarish Ramachandruni
Hi ,


Check how many Account records are in org And if it is more then 10 don't give any limt .



Regards ,
Harish.R
Dev Rana 16Dev Rana 16
Thanks everyone i solve my problem.