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
Ajitkumar Pradhan 9Ajitkumar Pradhan 9 

Batch not executing

global class BatchJobStatus implements Database.Batchable<sObject>,Database.AllowsCallouts

{
 public String query;
 global Database.querylocator start(Database.BatchableContext BC){
        query = 'Select id, AccountId, Account_Company_Id__c, JobDiva_ID__c, StageName from Opportunity where JobDiva_ID__c!=null AND StageName ='\Lost\'';
        return Database.getQueryLocator(query);
    }
 global void start(){
        Database.executeBatch(new BatchJobStatus(),100);
    } 

global void execute(Database.BatchableContext bc, List<Opportunity> oppList){
       for (Opportunity opp : oppList) {
        if(opp.StageName=='Lost'){
opp.StageName = 'Closed';
        }try{
update oppList;
}
   catch(Exception e) {
            System.debug(e);
        }
         
    }    
}
global void finish(Database.BatchableContext bc){
        // execute any post-processing operations
    }    
}

Code is not working. The opportunity satge is not getting updated.
RKSalesforceRKSalesforce
Please try this code . Hope this will work:
global class BatchJobStatus implements Database.Batchable<sObject>,Database.AllowsCallouts

{
 public String query;
 global Database.querylocator start(Database.BatchableContext BC){
        query = 'Select id, AccountId, Account_Company_Id__c, JobDiva_ID__c, StageName from Opportunity where JobDiva_ID__c!=null AND StageName ='\Lost\'';
        return Database.getQueryLocator(query);
    }
global void execute(Database.BatchableContext bc, List<Opportunity> oppList){
       for (Opportunity opp : oppList) {
        if(opp.StageName=='Lost'){
			opp.StageName = 'Closed';
        }
		try{
			update oppList;
		}
		catch(Exception e) {
            System.debug(e);
        }         
    }    
}
global void finish(Database.BatchableContext bc){
        // execute any post-processing operations
    }    
}
Mark as best answer if helped.

Regards,
Ramakant
 
Ajitkumar Pradhan 9Ajitkumar Pradhan 9
Hi Ramakant,

Still not working.

 
RKSalesforceRKSalesforce
can you try below code:
global class BatchJobStatus implements Database.Batchable<sObject>,Database.AllowsCallouts

{

	public String query;
	global Database.querylocator start(Database.BatchableContext BC){
		query = 'Select id, AccountId, Account_Company_Id__c, JobDiva_ID__c, StageName from Opportunity where JobDiva_ID__c!=null AND StageName ='\Lost\'';
		return Database.getQueryLocator(query);
	}
	global void execute(Database.BatchableContext bc, List<sObject> scope){
		//Opportunit List to Process
		List<Opportunity> oppList =  New List<Opportunity>();
		oppList = (List<Opportunity>) scope;
		for (Opportunity opp : oppList) {
			if(opp.StageName=='Lost'){
				opp.StageName = 'Closed';
			}
			try{
				update oppList;
			}
			catch(Exception e) {
				System.debug(e);
			}         
		}    
	}
	global void finish(Database.BatchableContext bc){
		// execute any post-processing operations
	}    
}

 
Amit Chaudhary 8Amit Chaudhary 8
Please execute below code and let me know what error you are getting
global class BatchJobStatus  implements Database.Batchable<sObject> 
{
    global Database.QueryLocator start(Database.BatchableContext BC) 
    {
        String query = 'Select id, AccountId, Account_Company_Id__c, JobDiva_ID__c, StageName from Opportunity where (JobDiva_ID__c!=null AND StageName =\'Lost\' )';
        return Database.getQueryLocator(query);

	}
    global void execute(Database.BatchableContext BC, List<Opportunity> oppList) 
    {
		for (Opportunity opp : oppList) {
			if(opp.StageName=='Lost'){
				opp.StageName = 'Closed';
			}
		}	
		update oppList;
    }
    global void finish(Database.BatchableContext BC) {
    }
}

Execute below code.

Database.executeBatch(new BatchJobStatus(),100);

Let us know the result
 
Arun MKArun MK
You should not have the dml operation (update oppList) inside the for loop. Move that dml operation after the for loop.