function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Oz AvrahamiOz Avrahami 

create account trigger error

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?? 
 
@anilbathula@@anilbathula@
Hi Avrahami,

Looks like the description filed is blank for the record which you are creating ,make sure you fill in description.
And description is a long area text where as Name is text field which allows only 40 characters .

Thanks
Anil.B
 
Suraj Tripathi 47Suraj Tripathi 47

Hii  Oz Avrahami , 
when you create a new account in your UI , Pls also write description there Because you are giving description as a name to the account which you are creating in trigger..

Please mark as best answer if it helps you.. 

Oz AvrahamiOz Avrahami
That's what's weird, it's not blank.
Oz AvrahamiOz Avrahami
if I change to:
trigger AccountParent on Account (after insert) {
    
    List<Account> acc = new List<Account>();
    
    for (Account a : Trigger.new) {
        Account ac = new Account();
        ac.Name = 'abc';
        ac.Parent__c = a.Name;
        acc.add(ac);
    }
    
    if (acc.size()>0) {
        insert acc;
    }
}

i get thist error:
AccountParent: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AccountParent: maximum trigger depth exceeded Account trigger event AfterInsert Account trigger event AfterInsert Account trigger event AfterInsert Account trigger event AfterInsert Account trigger event AfterInsert Account trigger event AfterInsert Account trigger event AfterInsert Account trigger event AfterInsert Account trigger event AfterInsert Account trigger event AfterInsert Account trigger event AfterInsert Account trigger event AfterInsert Account trigger event AfterInsert Account trigger event AfterInsert Account trigger event AfterInsert Account trigger event AfterInsert: [] Trigger.AccountParent: line 13, column 1
 
CharuDuttCharuDutt
Hii Oz
Try Below Trigger
trigger AccountParent on Account (after insert) {
    
    List<Account> acc = new List<Account>();
    
    for (Account a : Trigger.new) {
        Account ac = new Account();
       if(a.Description != Null){
        ac.Name = a.Description;
       }else{
        ac.Name = a.Name +''+'Test';
       }
        ac.Parent__c = a.Name;
        acc.add(ac);
    }
    
    if (acc.size()>0) {
        insert acc;
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
Gonapati BhavithaGonapati Bhavitha
Hi,
You Can Write Trigger  in logicless
trigger RecursiveTrigger on Account (After insert) {
    list<account> lstaccs = new  list<account>();
    for(account acc : trigger.new)
    {
        account ac = new account();
        ac.ParentId = acc.id;
        ac.Name = acc.name+'_Child';
        ac.phone = acc.phone;
        lstaccs.add(ac);
       
    }
    if(RecursiveCheck.check == False){
         RecursiveCheck.check = True;
         insert lstaccs; 
        
    }
    
}

We can Create one more class for this Recursive Check in Apex class

public class RecursiveCheck {
    public static boolean check = false;

}
Please Mark It As Best Answer If It Helps
Thank You!