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
Soubhagya Ranjan 2Soubhagya Ranjan 2 

Error in Batch Class

I want to delete records having status as rejected through batch class . 

please check the below code and let me know the correct code 

global class batchdeleteloan implements Database.batchable<sobject>{
global final String Query;
global batchdeleteloan (String q)
{
Query=q;
}
global Database.QueryLocator start(Database.BatchableContext BC)
{
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC,List<SObject> scope)
{
List <List<Laon_Application__c> > loanList = new list<List<Laon_Application__c> >();
for(List<Laon_Application__c> l : scope)
{
List<Laon_Application__c> la = (List<Laon_Application__c>)l;
If(l.Status__c = 'Rejected'){
LoanList.add(la);
}
}
Delete LoanList;
}

global void finish(Database.BatchableContext BC){

}
}
Naval Sharma4Naval Sharma4
Try the following code.
For more knowledge about batch see the link. (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm)
global class batchdeleteloan implements Database.batchable<sobject>{
	global final String Query;
	global batchdeleteloan (String q){
		Query=q;
	}
	global Database.QueryLocator start(Database.BatchableContext BC){
		return Database.getQueryLocator(query);
	}
	global void execute(Database.BatchableContext BC, List<SObject> scope){
		List<Laon_Application__c> loanList = new List<Laon_Application__c>();
		for(SObject sobj : scope){
			Laon_Application__c l = (Laon_Application__c) sobj;
			If(l.Status__c == 'Rejected'){
				LoanList.add(l);
			}
		}
		Delete LoanList;
	}

	global void finish(Database.BatchableContext BC){

	}
}

 
Amit Chaudhary 8Amit Chaudhary 8
Please check below post to learn about test classes
1) http://amitsalesforce.blogspot.com/search/label/Batch%20Job

Please update your code like below
global class batchdeleteloan implements Database.batchable<sobject>
{
	global final String Query;
	global batchdeleteloan (String q)
	{
		Query=q;
	}
	
	global Database.QueryLocator start(Database.BatchableContext BC)
	{
		return Database.getQueryLocator(query);
	}
	
	global void execute(Database.BatchableContext BC, List<Laon_Application__c>  scope)
	{
		List <List<Laon_Application__c> > loanList = new list<List<Laon_Application__c> >();

		for(List<Laon_Application__c> l : scope)
		{
			If(l.Status__c == 'Rejected')
			{
				LoanList.add(l);
			}
		}
		if(LoanList.size() > 0 )
		{
			Delete LoanList;
		}	
	}

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

Let us know if this will help you
 
Soubhagya Ranjan 2Soubhagya Ranjan 2
Hi Amit ,

still getting error  : Initial term of field expression must be a concrete SObject: List<Laon_Application__c> at line 20 column 16
Soubhagya Ranjan 2Soubhagya Ranjan 2
Hi Naval ,

while running in developer console it is showing constructer not defiend .
Amit Singh 1Amit Singh 1
Hello, 

Use below code for the batch class.
global class batchdeleteloan implements Database.batchable<sobject>
{
	global final String Query;
	global batchdeleteloan (String q)
	{
		Query=q;
	}
	
	global Database.QueryLocator start(Database.BatchableContext BC)
	{
		return Database.getQueryLocator(query);
	}
	
	global void execute(Database.BatchableContext BC, List<Laon_Application__c>  scope)
	{
		List<Laon_Application__c> loanList = new list<Laon_Application__c>();

		for(Laon_Application__c l : scope)
		{
			If(l.Status__c == 'Rejected')
			{
				LoanList.add(l);
			}
		}
		if(LoanList.size()> 0 )
		{
			Delete LoanList;
		}	
	}

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

For executing the batch from Developer console use below code.
 
String query = 'Your Query Here';
Database.executeBatch(new batchdeleteloan(query));
Let me know if this helps :)

Thanks!
Amit Singh
 
Amit Chaudhary 8Amit Chaudhary 8
Hi Soubhagya Ranjan 2,

I did one small system issue in below line
List <List<Laon_Application__c> > loanList = new list<List<Laon_Application__c> >();
for(List<Laon_Application__c> l : scope)
Please update code like below
List <Laon_Application__c> loanList = new list<Laon_Application__c>();
for(Laon_Application__c l : scope)

Please try below code :-
global class batchdeleteloan implements Database.batchable<sobject>
{
	global final String Query;
	global batchdeleteloan (String q)
	{
		Query=q;
	}
	
	global Database.QueryLocator start(Database.BatchableContext BC)
	{
		return Database.getQueryLocator(query);
	}
	
	global void execute(Database.BatchableContext BC, List<Laon_Application__c>  scope)
	{
		List<Laon_Application__c > loanList = new list<Laon_Application__c>();

		for(Laon_Application__c l : scope)
		{
			If(l.Status__c == 'Rejected')
			{
				LoanList.add(l);
			}
		}
		if(LoanList.size() > 0 )
		{
			Delete LoanList;
		}	
	}

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

NOTE :- To execute the above batch job you need to pass query in your constructor like below
String query = 'Select status__c,id from Laon_Application__c';// please update your query 
Database.executeBatch(new batchdeleteloan(query));


Let us know if this will help you