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 Code not eexcuting

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){
     
    }    
}
Best Answer chosen by Ajitkumar Pradhan 9
mohdAnasmohdAnas
query = 'Select id, AccountId, Account_Company_Id__c, JobDiva_ID__c, StageName from Opportunity where JobDiva_ID__c!=null AND StageName =\'Lost\'';


Hello Ajit,

Try this I hope it will work

All Answers

mohdAnasmohdAnas
query = 'Select id, AccountId, Account_Company_Id__c, JobDiva_ID__c, StageName from Opportunity where JobDiva_ID__c!=null AND StageName =\'Lost\'';


Hello Ajit,

Try this I hope it will work

This was selected as the best answer
Ajitkumar Pradhan 9Ajitkumar Pradhan 9
Hi,
Thanks for correction.

My opportunity stage should aslo get update right? according to this code. But its not getting update. Am i missing something could you please help.
Amit Chaudhary 8Amit Chaudhary 8
Try to update your code like below
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){
    }    
}

Let us know if this will help you
 
Ajitkumar Pradhan 9Ajitkumar Pradhan 9
I can't find the difference between your code and my code.

Thanks
Amit
Amit Chaudhary 8Amit Chaudhary 8
I changed the query and even i removed the DML from loop.
Ajitkumar Pradhan 9Ajitkumar Pradhan 9
oh yeah. 

But still my opportunity stage is not getting updated. Do i need to do anything extra?
I have one opportuity which is Lost and it should get updated to Won but not happening.

Thanks
Amit
Amit Chaudhary 8Amit Chaudhary 8
Make sure for that Opportunity JobDiva_ID__c != null ?
Ajitkumar Pradhan 9Ajitkumar Pradhan 9
It has a Id. Its not null when i fire SOQL query the list is coming.
Ajitkumar Pradhan 9Ajitkumar Pradhan 9
Hi,

Database.executeBatch(new BatchJobStatus(),100);
 i executed this bydoing Ctrl+E and some Log appeared. 
Amit Chaudhary 8Amit Chaudhary 8
Update your execute method like below
global void execute(Database.BatchableContext BC, List<Opportunity> oppList) 
    {
		for (Opportunity opp : oppList) {
			if(opp.StageName=='Lost'){
				opp.StageName = 'Closed';
			}
		}	
		update oppList;
    }

And check any exeption is coming or not
 
mohdAnasmohdAnas
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<sObject> scope){
                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){
    }    
}

try this