• Kaarthik
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
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.
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.