You need to sign in to do that
Don't have an account?
Ian Lin 585
Ian Lin 585
Can anyone answer to the following scenario?. I need to create a account after a contact is created with the contact name as account name. If the account name with the same contact name exists assign the accountId to Id of existing account otherwise create a account with the name of the contact name and assign the new account id to contact.
My code is :
It throws the below error. Can anyone help to fix this issue.?
Thanks.
14:59:37:160 EXCEPTION_THROWN [15]|System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CreateDefaultContact: execution of AfterInsert
14:49:50:170 FATAL_ERROR System.FinalException: Record is read-only
Need help on trigger on contact to create Account object with the contact name and associate it with contact
Ian Lin 585
Can anyone answer to the following scenario?. I need to create a account after a contact is created with the contact name as account name. If the account name with the same contact name exists assign the accountId to Id of existing account otherwise create a account with the name of the contact name and assign the new account id to contact.
My code is :
trigger CreateDefaultContact on Contact (after insert) { ContactDefault.createContact(trigger.new); } public class ContactDefault { public static void createContact(List<Contact> contactList) { List<Contact> needAcctsLst = new List<Contact>(); for(Contact c : contactList){ needAcctsLst.add(c); } List<Account> accountList = new List<Account>(); List<Contact> conLst = new List<Contact>(); for(Contact conObj : needAcctsLst) { String cname=conObj.FirstName+' '+conObj.LastName; Account acc = [select Id,name from Account where name=:cname LIMIT 1]; if (acc!=null && acc.Id!=null){ System.debug( 'inside if '); conObj.AccountId = acc.Id; }else{ Account accObj = new Account(); accObj.Name = cname; conObj.AccountId = accObj.Id; accountList.add(accObj); System.debug( 'inside else '); } conLst.add(conObj); } if(accountList.size() > 0) { insert accountList; } if(conLst.size() > 0) { System.debug( 'updating contact '); update conLst; } } } @isTest public class ContactDefaultTest { @testSetup static void testSetUpData() { Account testAccount = new Account(); testAccount.Name='aaa ttt' ; insert testAccount; } @istest public static void testMethod1(){ Contact cont = new Contact(); cont.FirstName='aaa'; cont.LastName='ttt'; insert cont; String cname=cont.FirstName+' '+cont.LastName; Account acc = [select Id,name from Account where name=:cname LIMIT 1]; System.assertEquals(acc.Id,cont.AccountId,'Equals .. passed'); } }
It throws the below error. Can anyone help to fix this issue.?
Thanks.
14:59:37:160 EXCEPTION_THROWN [15]|System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CreateDefaultContact: execution of AfterInsert
14:49:50:170 FATAL_ERROR System.FinalException: Record is read-only
Please find the Working Code.
Please Mark it as Best Answer if the Solution helps you.
Thanks,
Vijay Nagella,
Solution Architect,
Steadfast Consultancy Services
All Answers
Please find the Working Code.
Please Mark it as Best Answer if the Solution helps you.
Thanks,
Vijay Nagella,
Solution Architect,
Steadfast Consultancy Services
Thanks for the answer. Best answer.