+ Start a Discussion
Geetha BGeetha B 

Handling errors in batch apex

Can any one give a simple solution to handle failure records in batches
Best Answer chosen by Geetha B
RishavRishav
Hii Geeta,

  Try to implement the DATABASE namespace and its classes to handle the error in run time . Try to refer the docment "apex Language Reference , go to page number 756  and study you will get idea" By this you can get your all error in debug log like this:
   This is a simple example for handling the error now you cn implement in the same way in batch apex;
Account[]accList = new list<Account>
{new Account(name ='Account1'),
    new Account()};
        // created one empty and one correct Account
        DataBase.SaveResult[] sr = Database.insert(accList,false);
//iterate through each returned list
for(Database.saveResult srl:sr)
{
    if(srl.isSuccess())
    {
        system.debug('successfully inserted Account. Id is :'+srl.getId());
    }
    else
    {
        for(DataBase.Error err:srl.getErrors())
        {
          system.debug('Following are the errors');
            system.debug(err.getStatusCode()+':'+err.getMessage());
            system.debug('following field affected'+err.getFields());
        }
    }
   
  }
  If you got it then mark as a best answer.

Thanks 
Rishav

All Answers

RishavRishav
Hii Geeta,

  Try to implement the DATABASE namespace and its classes to handle the error in run time . Try to refer the docment "apex Language Reference , go to page number 756  and study you will get idea" By this you can get your all error in debug log like this:
   This is a simple example for handling the error now you cn implement in the same way in batch apex;
Account[]accList = new list<Account>
{new Account(name ='Account1'),
    new Account()};
        // created one empty and one correct Account
        DataBase.SaveResult[] sr = Database.insert(accList,false);
//iterate through each returned list
for(Database.saveResult srl:sr)
{
    if(srl.isSuccess())
    {
        system.debug('successfully inserted Account. Id is :'+srl.getId());
    }
    else
    {
        for(DataBase.Error err:srl.getErrors())
        {
          system.debug('Following are the errors');
            system.debug(err.getStatusCode()+':'+err.getMessage());
            system.debug('following field affected'+err.getFields());
        }
    }
   
  }
  If you got it then mark as a best answer.

Thanks 
Rishav

This was selected as the best answer
Geetha BGeetha B
Thank You Rishav,

                        If we do updation than how we can hadle errors.


RishavRishav
HIi Geetha,
                     procedure will be same only method will be changed.First write your  code for update .For update operation it will be like this 

 Database.saveResult [] sr = Database.update(instanceName,TRUE/False);   // here update is a method of database class

// To collect the result  of INSERT and UPDATE operation same one class that is  SaveResult  class is used. for delete there is DeleteResult class is there .

   If you want more detail info about these classes then please refer the above document and  go to page number:114 you will get all the classes that is used in  DML operation by using Database Namespace.

Thanks
Rishav
Geetha BGeetha B
Thank You so much Rishav,

                                      Now it is clear to me.
Geetha BGeetha B
Hi Rishav,

             U asked me to refer in apex reference document , can u please send me the link for that document

Thank You.

RishavRishav
Hii Geetha,
                          This is the link yo can refer and download the document.
http://www.salesforce.com/us/developer/docs/apexcode/salesforce_apex_language_reference.pdf

Thanks 
Rishav