I have written a trigger to create/update a contact when account is created/updated. Every account will have one contact associated with it. When i try to load the data from data loader in to account object. I have got the following error
triggerContactLoad: execution of AfterInsert
caused by: System.DmlException: Insert failed. First exception on row 137; first error: INVALID_EMAIL_ADDRESS, Email: invalid email address: BoBoLaTi: [Email]
Trigger.triggerContactLoad: line 14, column
Error message indicates that Email address id BoBoLaTi but our data does not contain any of these email addresses.
Following is my trigger code
trigger triggerContactLoad on Account (after insert,after update) {
list<contact> InsertContact = new list<contact>();
list<contact> UpdateContact = new list<contact>();
contact objContact;
if(trigger.isInsert){
for(account objAccount:trigger.new){
objContact = new contact();
objContact.lastname = objAccount.name;
objContact.AccountId = objAccount.id;
objContact.Email = objAccount.Email__c;
objContact.Description = objAccount.Description;
objContact.Fax = objAccount.Fax;
objContact.MobilePhone = objAccount.Phone;
objContact.User_Id__c = objAccount.User_Id__c;
InsertContact.add(objContact);
}
insert InsertContact;
}
if(trigger.isUpdate){
for(account objAccount:trigger.new){
objContact = [Select Id from Contact where User_Id__c =: objAccount.User_Id__c];
objContact.lastname = objAccount.name;
objContact.AccountId = objAccount.id;
objContact.Email = objAccount.Email__c;
objContact.Description = objAccount.Description;
objContact.Fax = objAccount.Fax;
objContact.MobilePhone = objAccount.Phone;
objContact.User_Id__c = objAccount.User_Id__c;
UpdateContact.add(objContact);
}
update UpdateContact;
}
}