You need to sign in to do that
Don't have an account?
Karen Hanson
Error In Apex Trigger to create contacts record only if check box is checked in account
HI ,
I have a custom field called Create_Contact__c that is a data type "checkbox". in Account object and
I need to evaluate it in an apex trigger. and create new contact record when it is checked in account object .
Error: Compile Error: Variable does not exist: Create_Contact__c at line 3 column 6
I have a custom field called Create_Contact__c that is a data type "checkbox". in Account object and
I need to evaluate it in an apex trigger. and create new contact record when it is checked in account object .
Error: Compile Error: Variable does not exist: Create_Contact__c at line 3 column 6
trigger CreateAccountContact on Account (after insert, after update){ if(Create_Contact__c == True) { List<Contact> ct = new List <Contact>(); for(Account acc : trigger.new){ Contact c = new Contact(LastName = acc.name, AccountId=acc.id, Fax=acc.Fax, MailingStreet=acc.BillingStreet, MailingCity=acc.BillingCity, MailingState=acc.BillingState, MailingPostalCode=acc.BillingPostalCode, MailingCountry=acc.BillingCountry, Phone=acc.Phone); ct.add(c); } insert ct; } }
1) Check you was using outside the loop
2) Before DML please check size of list. in case of Create_Contact__c == false it will fail
Please below code. I hope that will help you Let us know if this will help you
Thanks
Amit Chaudhary
If you perform DML on empty list that doesn't count DML operation
Please see this discussion http://salesforce.stackexchange.com/q/19399/18731
When the Checkbox is True and you try to update other fields, a New Contact is created for every Update.(Eg : If you Update the account phone number again a new contact will be created as the checkbox is True).
Please find the Code below that exactly fits your purpose. Let us know if this will help you
Thanks,
Onesh.K