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
CEO SahooCEO Sahoo 

create a contact field checkbox name Isactive , create batch apex every contact of the contact fields are checked give the batch apex code

i wrote this code
global class UpdateContactFieldsBatch implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext context) {
        // Query all Contacts where IsActive is true
        return Database.getQueryLocator([SELECT Id, IsActive__c FROM Contact WHERE IsActive__c = true]);
    }

    global void execute(Database.BatchableContext context, List<Contact> contacts) {
        // Loop through the selected Contacts and update the desired field(s)
        for (Contact contact : contacts) {
            contact.IsActive__c = true; // Update Email Opt Out field
        }

        // Update the Contacts
        update contacts;
    }

    global void finish(Database.BatchableContext context) {
        // Any post-processing steps can go here
    }
}


and wrote this code for debugging
UpdateContactFieldsBatch batch = new UpdateContactFieldsBatch();
Database.executeBatch(batch);


in apex job show staus completed but in out put in contact field every field in contact are not checked please help mee please modify the code
HarshHarsh (Salesforce Developers) 
Hi Sahoo, 
Try the below code  
global class UpdateContactFieldsBatch implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext context) {
       
        return Database.getQueryLocator([SELECT Id, IsActive__c FROM Contact WHERE IsActive__c = false]);
    }

    global void execute(Database.BatchableContext context, List<Contact> contacts) {
         List<Contact> contactList = new List<Contact>();
        for (Contact con : contacts) {
            con.IsActive__c = true; 
 contactList.add(con);
       }
        update contactList;
    }

    global void finish(Database.BatchableContext context) {
       
    }
}

Here the code will check the following things
  1. it will fetch the record having  IsActive__c  is Flase i.e Unchecked.
  2. In the void method, It will update IsActive__c to True i.e Checked
I hope this will solve your query.

Please mark it as Best Answer if the above information was helpful.

Thanks.
 
Bhanu Prakash SBhanu Prakash S
CEO Sahoo,

You are trying to update the value in temp object in For loop, as Harsh mentioned, you need to add that temp contact object to new Contacts list and update that new contact list object.