+ Start a Discussion
tridgetridge 

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.

Best Answer chosen by Admin (Salesforce Developers) 
MandyKoolMandyKool

Hi,

 

This is your trigger !!!

trigger ContactPhoneUpdate on Contact (before update) 
{ 
    Map<Id, Contact> mapContactByAccountId = new Map<Id, Contact>();
    for (Contact objContact : Trigger.new)
    {
         if(objContact.Is_Same__c == true)
             mapContactByAccountId.put(objContact.AccountId,objContact );
    }
    
    if(mapContactByAccountId.size() > 0)
    {
        List<Account> lstAccounts = [Select Id,Phone from Account where ID IN :mapContactByAccountId.keySet()];
        if(!lstAccounts.isEmpty())
        {
            for(Account objAccount : lstAccounts)
            {
                Contact objContact = mapContactByAccountId.get(objAccount.Id);
                objContact.Phone = objAccount.Phone;
            }
        }
    }    
}

 

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

Starz26Starz26
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 Id IN (Select AccountID From Contacts where = :cons.id)];
for(Account a: accs) { If(cons.Contact_Account_Phone_the_Same__c ==true)
{Cons.phone__c=accs.phone__c;
}
}
}
}}

 

You will need to modify trigger to bulkify but this should work for one record

tridgetridge

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

 

Select AccountID From Contacts where = :cons.id


MandyKoolMandyKool

Hi,

 

This is your trigger !!!

trigger ContactPhoneUpdate on Contact (before update) 
{ 
    Map<Id, Contact> mapContactByAccountId = new Map<Id, Contact>();
    for (Contact objContact : Trigger.new)
    {
         if(objContact.Is_Same__c == true)
             mapContactByAccountId.put(objContact.AccountId,objContact );
    }
    
    if(mapContactByAccountId.size() > 0)
    {
        List<Account> lstAccounts = [Select Id,Phone from Account where ID IN :mapContactByAccountId.keySet()];
        if(!lstAccounts.isEmpty())
        {
            for(Account objAccount : lstAccounts)
            {
                Contact objContact = mapContactByAccountId.get(objAccount.Id);
                objContact.Phone = objAccount.Phone;
            }
        }
    }    
}

 

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

 

This was selected as the best answer
tridgetridge

Thank you very much - it works!!!!