Skip to main content
sandeep reddy preguntó en #Apex
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){

        

    }

   }
3 respuestas
  1. 14 abr 2016, 10:35 a.m.
    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