You need to sign in to do that
Don't have an account?
APEX CODING - CONTACT OBJECT
I am very new to Apex Coding. I want to write an Apex trigger on the Contact record to automatically populate the Contact Phone field with the related Account field phone number IF the "Contact & Account Phone the Same?" checkbox is checked. Following is the scenario:
1. If the Contact field "Contact & Account Phone the Same?" is TRUE (check box is checked), THEN
2. Automatically update the Contact "Phone" field with the same value in the Account "Phone" field.
Trigger = Before Update
I have written the following code that does not work (error message when trigger is saved) is listed below the code. Any help would be greatly appreciated.
trigger ContactPhoneUpdate on Contact (before update) { for (Contact cons: Trigger.new)
{ if (cons.Phone__C !=null ) { List<Account> accs = new List<Account>();
accs = [SELECT Id FROM Account WHERE AccountId = :cons.ID];
for(Account a: accs) { If(cons.Contact_Account_Phone_the_Same__c ==true)
{Cons.phone__c=accs.phone__c;
}
}
}
}}
Error message is as follows:
Error: Compile Error: No such column 'AccountId' on entity 'Account'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. at line 3 column 8
Line 3 column 8 is this part of the code
[SELECT Id FROM Account WHERE AccountId = :cons.ID];
Thanks.
Hi,
This is your trigger !!!
Just replace the "Is_Same__c" field with your field name( whatever you have used for checkbox) and this will work even for bulk records. Let me know if you face any issues.
Email - mandy.kool@yahoo.com
All Answers
You will need to modify trigger to bulkify but this should work for one record
Thanks for the correction to the code. Copied and pasted to the Contact Trigger page and received the following error message:
Error: Compile Error: unexpected token: '=' at line 3 column 81
Which related to the "=" in this part of the code
Hi,
This is your trigger !!!
Just replace the "Is_Same__c" field with your field name( whatever you have used for checkbox) and this will work even for bulk records. Let me know if you face any issues.
Email - mandy.kool@yahoo.com
Thank you very much - it works!!!!