• Gonapati Bhavitha
  • NEWBIE
  • 0 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 3
    Replies
Hi,
I'm new in apex and need some help,


I write a trigger that creates a new account every time that I created an account:
trigger AccountParent on Account (after insert) {
    
    List<Account> acc = new List<Account>();
    
    for (Account a : Trigger.new) {
        Account ac = new Account();
        ac.Name = a.Description;
        ac.Parent__c = a.Name;
        acc.add(ac);
    }
    
    if (acc.size()>0) {
        insert acc;
    }
}

but when i create account i get an error:
AccountParent: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AccountParent: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name]: [Name] Trigger.AccountParent: line 13, column 1: [] Trigger.AccountParent: line 13, column 1

why?? 
 
Hello as we all know that customer validation excute first and then before trigger . 
so I write a trigger to show error message if description is null
trigger updatecontact on contact( before trigger)
for(contact con: trigger new)
If(con.description == null)
Con.adderror(' message from trigger i.e contact is null')
 
And wrote a custom validation for the same thing
When I try to save the code its showing message from before trigger not from custom validation .

Why so . Please help I'm confused