+ Start a Discussion
KaarthikKaarthik 

adderror in before insert trigger

Hi,
I have a custom object Manager__c with two fields
Name (Text) - Standard Field
Designation__c (Text(2)) // Just 2 characters.

I wrote the following class to insert two records and using Database.Insert to execute all records even if one fails.
Public class SampleCode
{
    public static void method1()
    {
        List<Manager__C> lstman = new LIst<Manager__c>();
        for(integer i=1;i<3;i++)
        {
           //Manager name as 1,2
           Manager__c man = new manager__C(name=string.valueof(i)); 
           lstman.add(man);
        }
        Database.saveResult[] insres = database.insert(lstman,false);
        for(Database.saveresult res: insres)
        {
            for(Database.Error er : res.getErrors())
            {
                system.debug(er.getmessage());
            }
        }
         
    }
}

I wrote the following trigger (before insert) to set the designation with more than allowed characters.
trigger Manager on Manager__c (before insert) {
    for(Manager__c man : Trigger.New)
    {
       //Following should make all the records error.
        man.Designation__c = 'Executive'; 
    }
}

When I execute the above apex class method, the records are not inserted (as expected).But i am not getting the system validation error messages (some thing like TOO long text...) in the debug statement.
I need a solution for this, like where to write the addError() method in the trigger or any other way. 
Note: All the records inserted in the list should fail and i need errors for all the records to get displayed in system.debug.
Lokesh KumarLokesh Kumar
Try to add man.adderror("Too long Text"); in the trigger or you can put a check before adding the error.

Thanks
Lokesh
Harshit Garg 6Harshit Garg 6
Hi A.D,
 Try this code 
you was forgetting to define the Designation__c field in class.because of that the error was not coming into debug log.  
Public class SampleCode
{
    public SampleCode()
    {
        List<Manager__C> lstman = new LIst<Manager__c>();
        for(integer i=1;i<3;i++)
        {
           //Manager name as 1,2
           Manager__c man = new manager__C(name=string.valueof(i),Designation__c ='Tester'); 
           lstman.add(man);
        }
        Database.saveResult[] insres = database.insert(lstman,false);
        for(Database.saveresult res: insres)
        {
            for(Database.Error er : res.getErrors())
            {
                system.debug(er.getmessage());
            }
        }
Please mark my answer as a solution if it was helpful so it is available to others as a proper solution.
If you felt I went above and beyond, please give me Kudos by clicking on the star icon.

Thanks,
Harshit garg
 
KaarthikKaarthik
Hi Lokesh,
Thanks for your reply.
After the before insert trigger execution (where i am setting the Designation__c), the system validation executes and will throw the error. I need to identify where to catch those errors and add to man.addError() method or any other way other than adderror() method. In this scenario we cannot use man.adderror() in the before insert trigger because within trigger we will not know exception had occured.

Kaarthik.
 
KaarthikKaarthik
Hi Harshit,
Thanks for your reply.
If we add Designation__c in the class while creating the records, save result will get the error message. Unfortunately, in my actual scenario, I need to set that field in the trigger only and not in the class . (The code I mentioned in the question is a sample and simplified version of my actual scenario).

Kaarthik.
Alex EzhilarasanAlex Ezhilarasan

Hi Kaarthik,

I hate to suggest this, But as of now you need to use Batch class to find the failed records information.(Hope you know how to do, I came to this conclusion because of your explanation for 2 people repesonse).

What is happening here is: Trigger takes the list as a batch and inserting the records based on all or none concept. The problem is it is not returning the results to SaveResult.

So my suggestion is write a batch class to process one records at a time and send an email or create an attachment under any appropriate record on finish method. So that you can have a log.

Hope this helps! 

Thanks,
Alex