+ Start a Discussion
sakthidharan Ambayiramsakthidharan Ambayiram 

//Facing the below error when i try to create a new Account with triggers

//Requirement : we need to create a Contact record every time when you create a new Account
*************
trigger AccountTrigger on Account (after insert) {
List<Contact> ContactList=new List<Contact>();
for(Account a: Trigger.new)
    {
    Contact C=new Contact();
    C.LastName=a.Name;
    ContactList.Add(C);
    }
    insert ContactList;
}
***************************
//Facing the below error when i try to create a new Account
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger AccountTrigger caused an unexpected exception, contact your administrator: 
AccountTrigger: execution of AfterInsert caused by: System.DmlException: Insert failed. 
First exception on row 0; first error: 
FIELD_CUSTOM_VALIDATION_EXCEPTION, u cant insert anew record without email: []: Trigger.AccountTrigger: line 9, column 1
Best Answer chosen by sakthidharan Ambayiram
Khan AnasKhan Anas (Salesforce Developers) 
Yes, there is no field on Account. But, there is an Email field on Contact object. Check the validation or other triggers/workflow on Contact object.

Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Sakthidharan,

Greetings to you!

Seems like there is some validation on Email or it is a required field. Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
trigger CreateConOppWithAcc on Account (after insert) {
    
    List<Contact> conList = new List<Contact>();
    
    for(Account a:Trigger.new) {
        
        Contact con = new Contact();
        con.AccountId=a.id;
        con.LastName=a.Name;
        con.Email='khan@test.com';
        conList.add(con);
    }

    if(conList.size()>0){
        INSERT conlist;
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
sakthidharan Ambayiramsakthidharan Ambayiram
Hi Khan Anas,
Thanks for ur response.
there is no field like Email in Account Object.
Still facing the same issue.
Khan AnasKhan Anas (Salesforce Developers) 
Yes, there is no field on Account. But, there is an Email field on Contact object. Check the validation or other triggers/workflow on Contact object.

Regards,
Khan Anas
This was selected as the best answer
sakthidharan Ambayiramsakthidharan Ambayiram
Thanks Khan Anas
There is a trigger in Contact object for email, beacuse of that it is throwing error and it is working fine now