+ Start a Discussion
sandeep reddy 37sandeep reddy 37 

i got problem with batch apex inserting records need to get error messege it fails

i need get error if  inserting a records  if dml is fails  shows error 
how to do  please tell me let you know


public class batchapexerror implements database.Batchable<sobject>,database.stateful {
    public string errors[] =new new string[]{'jnkjdnksdj','hfhh'};
    public database.QueryLocator start(database.BatchableContext be){
        return database.getQueryLocator('select id,name,firstname,lastname from account ');
       }
    public void execute(database.BatchableContext bc,list<sobject> acc){
        account a=new account();
        for(account a1:a){
               try{
                   if(a.AnnualRevenue==50000){
                a.name='prakesh';
                a.industry='banking';
                insert a;
                   }
               }catch(exception e){
                       errors.add(e.getmessage());
                   }
                   
            } 
            
        }
    public void finish(database.BatchableContext bc){
        
    }
   }
Kanika DuaKanika Dua
Try Adding System.Debug For Account like
public class batchapexerror implements database.Batchable<sobject>,database.stateful {
    public string errors[] =new new string[]{'jnkjdnksdj','hfhh'};
    public database.QueryLocator start(database.BatchableContext be){
        return database.getQueryLocator('select id,name,firstname,lastname from account ');
       }
    public void execute(database.BatchableContext bc,list<sobject> acc){
        account a=new account();
        for(account a1:a){
               try{
                   if(a.AnnualRevenue==50000){
                a.name='prakesh';
                a.industry='banking';
                insert a;
               System.debug('@@@@'+a);
                   }
               }catch(exception e){
                       errors.add(e.getmessage());
                   }
                   
            } 
            
        }
    public void finish(database.BatchableContext bc){
        
    }
   }
And Then Check in Debug Logs.But First make sure Debug Logs For Your User and particular day are activated
kiran punurukiran punuru
Hi Sandeep,

 You can use the below method to get the reason for failure of records.

insert(recordToInsert, allOrNone)
Adds an sObject, such as an individual account or contact, to your organization's data.
Signature
public static Database.SaveResult insert(sObject recordToInsert, Boolean allOrNone)
Parameters
recordToInsert
Type: sObject
allOrNone
Type: Boolean
The optional allOrNone parameter specifies whether the operation allows partial success. If you specify false for this parameter and a record fails, the remainder of the DML operation can still succeed. This method returns a result object that can be used to verify which records succeeded, which failed, and why.
Return Value
Type: Database.SaveResult
Usage
insert is analogous to the INSERT statement in SQL.
Apex classes and triggers saved (compiled) using API version 15.0 and higher produce a runtime error if you assign a String value that is too long for the field.
Each executed insert method counts against the governor limit for DML statements

Link:https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_database.htm#apex_System_Database_insert
Abu HashimAbu Hashim
As Batch is an Asynchronous process, we can't get real time errors displayed. So ,for capturing the exceptions - if u've any dedicated object for logs, we can insert records into that object with respective record details and error description and examine later on.
Also, we should not use DML operation inside loop - Not good practice for coding - Will hit governor limit . Create a list, add the entries in to the list and finally Use Database.SaveResult[] sr = Database.Insert(ListRec,false); statement to insert. We can iterate through sr variable to get the success and failure sceanrios. As per ur req, we can insert the error records in to LOGS object if the Sr result is a failure.

Let me know if u need more clarification.!!