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
penchala rajupenchala raju 

can any one solve this batch apex program

global class customerbatch implements Database.Batchable<sobject>
{
    public String myname;
    global customerbatch(String myname)
    {
        this.myname=myname;
    }
    global Database.QueryLocator start(Database.BatchableContext bc)
    {
        return Database.getQueryLocator('select id,AType__c from Customer__C where name='+myname);
    }
    global void execute(Database.BatchableContext bc,list<customer__c> scope)
    {
        list<Customer__c> cust=new List<Customer__c>();
        for(Customer__c c:cust)
        {
            c.AType__c='saving';
            cust.add(c);
        
        }
        update cust;
    }
        global void finish(Database.BatchableContext bc)
        {
            Messaging.singleEmailMessage myemail=new Messaging.singleEmailMessage();
            String[] toadd=new String[]{'penchalaraju7@gmail.com'};
            myemail.setToAddresses(toadd);
            myemail.setSubject('my notification');
            messaging.sendEmail(new messaging.Email[]{myemail});
            
        }
    
}

 
Andrew Wilkinson 2Andrew Wilkinson 2
Can you describe what functionality you are trying to accomplish or what issues you are having with the above?
penchala rajupenchala raju
after execution of the batch apex like

customerbatch cb=new customerbatch(myname);
Database.executeBatch(cb,5);

it showing variable name does not exist
 
Andrew Wilkinson 2Andrew Wilkinson 2
Is that the entire script you are using? If so, myname is not defined. You would need to add before the first line:

String myname = 'someText';

Replace someText with the name you are wanting to use in the batch.
Amit Chaudhary 8Amit Chaudhary 8


customerbatch cb=new customerbatch('TestName');  // Please add Name here
Database.executeBatch(cb,5);
 
penchala rajupenchala raju
after adding name i got a error like this
User-added imageUser-added image
 
Amit Chaudhary 8Amit Chaudhary 8
Please modify your batch job like below :-
global class customerbatch implements Database.Batchable<sobject>
{
    public String myname;
    global customerbatch(String myname)
    {
        this.myname=myname;
    }
    global Database.QueryLocator start(Database.BatchableContext bc)
    {
        return Database.getQueryLocator( 'select id,AType__c from Customer__C where name =\''+myname+'\'' );
    }
    global void execute(Database.BatchableContext bc,list<customer__c> scope)
    {
        list<Customer__c> cust=new List<Customer__c>();
        for(Customer__c c:cust)
        {
            c.AType__c='saving';
            cust.add(c);
        
        }
        update cust;
    }
        global void finish(Database.BatchableContext bc)
        {
            Messaging.singleEmailMessage myemail=new Messaging.singleEmailMessage();
            String[] toadd=new String[]{'penchalaraju7@gmail.com'};
            myemail.setToAddresses(toadd);
            myemail.setSubject('my notification');
            messaging.sendEmail(new messaging.Email[]{myemail});
            
        }
}
Please use below code to call batch job :-

customerbatch cb=new customerbatch('TestName');  // Please add Name here
Database.executeBatch(cb,5);

Please make this as best solution if this will help you.

Thanks
Amit Chaudhary
penchala rajupenchala raju
program is executing but records are not updating with saving account
 
Amit Chaudhary 8Amit Chaudhary 8
Please try below code :-
global class customerbatch implements Database.Batchable<sobject>
{
    public String myname;
    global customerbatch(String myname)
    {
        this.myname=myname;
    }
    global Database.QueryLocator start(Database.BatchableContext bc)
    {
		String Query = 'select id,AType__c from Customer__C where name =\''+myname+'\'' ;
		System.debug('------Query------->'+Query);	
        return Database.getQueryLocator( Query );
    }
    global void execute(Database.BatchableContext bc,list<customer__c> scope)
    {
        list<Customer__c> cust=new List<Customer__c>();
		System.debug('--------size--------->'+cust.size() );
        for(Customer__c c:cust)
        {
            c.AType__c='saving';
            cust.add(c);
        }
        update cust;
    }
        global void finish(Database.BatchableContext bc)
        {
            Messaging.singleEmailMessage myemail=new Messaging.singleEmailMessage();
            String[] toadd=new String[]{'penchalaraju7@gmail.com'};
            myemail.setToAddresses(toadd);
            myemail.setSubject('my notification');
            messaging.sendEmail(new messaging.Email[]{myemail});
            
        }
}

Please check all debug log. Check Query and Check how many record are comming after query.

NOTE:- According to query if you are passing name then only one record will come.
for exp:- if you are passing 'raju' then only one record will come
select id,AType__c from Customer__C where name='raju';

After executing the batch job. Please check record once batch job will completed.