You need to sign in to do that
Don't have an account?
Oz 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??
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??
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
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..
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
Try Below Trigger Please Mark It As Best Answer If It Helps
Thank You!
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!