• tridge
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies

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.

  • March 30, 2012
  • Like
  • 0

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.

  • March 30, 2012
  • Like
  • 0