+ Start a Discussion
ch ranjithch ranjith 

Not updating field in student with BAtch apex Please help me

global class batchstudent implements Database.Batchable<sobject>
{
global final String Query;
global batchstudent(String q)
{
Query=q;
}

global Database.QueryLocator start(Database.BatchableContext BC)
{
return Database.getQueryLocator(query);
}

global void execute(Database.BatchableContext BC,List<account> scope)
{
List <student__C> lstAccount = new list<student__C>();
for(Sobject s : scope)
{
student__C a = (student__C)s;
a.status__c='updated from batch apex';
system.debug(a);
lstAccount.add(a);
}
update lstAccount;
}

global void finish(Database.BatchableContext BC)
{
asyncapexjob a=[select id,status,numberoferrors,jobitemsprocessed,totaljobitems
                 from asyncapexjob where id=:bc.getjobid()];
              
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'ranjith.imp@gmail.com'};
mail.setToAddresses(toAddresses);
mail.setSubject('Apex Batch Job is done');
mail.setPlainTextBody('The batch Apex job processed '+a.totaljobitems+'batches with'+a.numberoferrors+'failures');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
Developer console :  id batchinstanceid = database.executeBatch(new deleteAccounts('select Id from student__c'));
sunny522sunny522
Hi ranjith...
    Go through the example in the link given below

http://salesforceglobe4u.blogspot.in/2013/12/batch-processing.html

if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.



pbattissonpbattisson
I think you have the wrong object being used within your loop the amended code is below:

global class batchstudent implements Database.Batchable<sobject>
{
    global final String Query;
    
    //You should ensure that the query is on the Student__c object
    global batchstudent(String q)
    {
        Query=q;
    }

    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        return Database.getQueryLocator(query);
    }

    //This method should take in a list of Students as the scope and update that list
    global void execute(Database.BatchableContext BC,List<student__c> scope)
    {
        for(Sobject s : scope)
        {
            s.status__c = 'updated from batch apex';
        }
        update scope;
    }

    global void finish(Database.BatchableContext BC)
    {
        asyncapexjob a=[select id,status,numberoferrors,jobitemsprocessed,totaljobitems
                 from asyncapexjob where id=:bc.getjobid()];
              
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {'ranjith.imp@gmail.com'};
        mail.setToAddresses(toAddresses);
        mail.setSubject('Apex Batch Job is done');
        mail.setPlainTextBody('The batch Apex job processed '+a.totaljobitems+'batches with'+a.numberoferrors+'failures');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}
This should work as you seemed to be retrieveing accounts and then trying to update and save them as student__c records.

ch ranjithch ranjith
Not updating field with below code

global class batchstudent implements Database.Batchable<sobject>
{
global final String Query;
global batchstudent(String q)
{
Query=q;
}

global Database.QueryLocator start(Database.BatchableContext BC)
{
return Database.getQueryLocator(query);
}

global void execute(Database.BatchableContext BC,List<student__C> scope)
{
List <student__C> lstAccount = new list<student__C>();
for(Sobject s : scope)
{
student__C a = (student__C)s;
a.status__c='updated from batch apex';
system.debug(a);
}
update scope;
}

global void finish(Database.BatchableContext BC)
{
asyncapexjob a=[select id,status,numberoferrors,jobitemsprocessed,totaljobitems
                 from asyncapexjob where id=:bc.getjobid()];
             
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'ranjith.imp@gmail.com'};
mail.setToAddresses(toAddresses);
mail.setSubject('Apex Batch Job is done');
mail.setPlainTextBody('The batch Apex job processed '+a.totaljobitems+'batches with'+a.numberoferrors+'failures');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}

developer console:
id batchinstanceid = database.executeBatch(new deleteAccounts('select Id from student__c'));
pbattissonpbattisson
Could you post the query you are using?
ch ranjithch ranjith
id batchinstanceid = database.executeBatch(new batchstudent('select Id from student__c'));
pbattissonpbattisson
Try updating the query to 'SELECT Id, Status__c FROM student__c' and using the code I wrote for you. You need to specify the field to be able to use it.
ch ranjithch ranjith
id batchinstanceid = database.executeBatch(new batchstudent('select Id,status__c from student__c'));
I tried this also but field is not updating
ch ranjithch ranjith
When i was tring to save above code its giving the follwing error:

Error: Compile Error: Field expression not allowed for generic SObject at line 22 column 13
ch ranjithch ranjith
When i was tring to save above code its giving the follwing error:

Error: Compile Error: expecting an equals sign, found ':' at line 20 column 23
pbattissonpbattisson
OKay I have removed some of the previous answers to help clarify the code for anybody looking at this in the future.

global class batchstudent implements Database.Batchable<sobject>
{
    global final String Query;
    
    //You should ensure that the query is on the Student__c object
    global batchstudent(String q)
    {
        Query=q;
    }

    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        return Database.getQueryLocator(query);
    }

    //This method should take in a list of Students as the scope and update that list
    global void execute(Database.BatchableContext BC,List<student__c> scope)
    {
        System.debug('\n\nSCOPE is:\n\t' + scope + '\n\n');
        for(student__c s : scope)
        {
            s.status__c = 'updated from batch apex';
        }
        update scope;
    }

    global void finish(Database.BatchableContext BC)
    {
        asyncapexjob a=[select id,status,numberoferrors,jobitemsprocessed,totaljobitems
                 from asyncapexjob where id=:bc.getjobid()];
              
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {'ranjith.imp@gmail.com'};
        mail.setToAddresses(toAddresses);
        mail.setSubject('Apex Batch Job is done');
        mail.setPlainTextBody('The batch Apex job processed '+a.totaljobitems+'batches with'+a.numberoferrors+'failures');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}

These past few issues should all have been easily resolvable yourself however. PLease paste up the debug.
ch ranjithch ranjith
Executing but not updating the field

30.0 APEX_CODE,FINEST;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
05:24:03.033 (33776860)|EXECUTION_STARTED
05:24:03.033 (33814867)|CODE_UNIT_STARTED|[EXTERNAL]|01p9000000575Ln|batchstudent
05:24:03.036 (36921797)|HEAP_ALLOCATE|[71]|Bytes:3
05:24:03.036 (36952755)|HEAP_ALLOCATE|[76]|Bytes:152
05:24:03.036 (36976935)|HEAP_ALLOCATE|[272]|Bytes:408
05:24:03.037 (37011203)|HEAP_ALLOCATE|[285]|Bytes:408
05:24:03.037 (37045004)|HEAP_ALLOCATE|[379]|Bytes:48
05:24:03.037 (37091457)|HEAP_ALLOCATE|[131]|Bytes:6
05:24:03.038 (38367592)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:43
05:24:03.048 (48523047)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:10
05:24:03.048 (48540586)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:368
05:24:03.048 (48546426)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:2
05:24:03.048 (48568262)|METHOD_ENTRY|[1]|01p9000000575Ln|batchstudent.batchstudent()
05:24:03.048 (48575581)|STATEMENT_EXECUTE|[1]
05:24:03.048 (48581987)|STATEMENT_EXECUTE|[1]
05:24:03.048 (48589317)|METHOD_EXIT|[1]|batchstudent
05:24:03.048 (48597766)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
05:24:03.048 (48602325)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:12
05:24:03.048 (48615141)|VARIABLE_SCOPE_BEGIN|[11]|this|batchstudent|true|false
05:24:03.048 (48674269)|VARIABLE_ASSIGNMENT|[11]|this|{"Query":"select Id,status__c  (15 more) ..."}|0x461c4149
05:24:03.048 (48684550)|VARIABLE_SCOPE_BEGIN|[11]|BC|Database.BatchableContext|true|false
05:24:03.048 (48794151)|VARIABLE_ASSIGNMENT|[11]|BC|{"jobId":"7079000001RJIR6AAP"}|0x69024394
05:24:03.048 (48808789)|STATEMENT_EXECUTE|[12]
05:24:03.048 (48813053)|STATEMENT_EXECUTE|[13]
05:24:03.048 (48859204)|HEAP_ALLOCATE|[50]|Bytes:5
05:24:03.048 (48884366)|HEAP_ALLOCATE|[56]|Bytes:5
05:24:03.048 (48901936)|HEAP_ALLOCATE|[63]|Bytes:7
05:24:03.048 (48949074)|SYSTEM_METHOD_ENTRY|[13]|Database.getQueryLocator(String)
05:24:03.050 (50569005)|SOQL_EXECUTE_BEGIN|[13]|Aggregations:0|select Id,status__c from student__c
05:24:03.084 (84102596)|SOQL_EXECUTE_END|[13]|Rows:424
05:24:03.084 (84159130)|HEAP_ALLOCATE|[13]|Bytes:20
05:24:03.084 (84181369)|SYSTEM_METHOD_EXIT|[13]|Database.getQueryLocator(String)
05:24:03.270 (122674173)|CUMULATIVE_LIMIT_USAGE
05:24:03.270|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 200
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 60000
  Maximum heap size: 0 out of 12000000
  Number of callouts: 0 out of 0
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 0

05:24:03.270|CUMULATIVE_LIMIT_USAGE_END

05:24:03.122 (122717906)|CODE_UNIT_FINISHED|batchstudent
05:24:03.122 (122736993)|EXECUTION_FINISHED

pbattissonpbattisson
Hey

That is the wrong debug log - I am looking for the log where my debug statement will be printed - one of the batch runs.

Thanks