function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
carlsosgcarlsosg 

Embedding VisualForce Page in Contact

I'm trying to figure out how to make it possible to include several Account fields on a Contact object - and make it possible to edit those fields (so the changes appear at the Account level also).

 

I've thought about creating a VisualForce page containing the Account fields, and embedding this in the Contact page layout - but that's as far as I've gotten.

 

Does anyone else have experience embedding VF page in a Contact - so that the fields can be edited at the Account level also?

rohitsfdcrohitsfdc

Hi,

the best way to achieve that is by using a VF page with a custom controller. in this way, you can create and edit multiple objects in the same page.

in the following example, acount and contact fields are edited on the same page

 

Vf page

 <apex:pageBlockSection title="Account Information">    
        <apex:inputField id="accountName" value="{!account.name}"/>
        <apex:inputField id="accountSite" value="{!account.site}"/>
      </apex:pageBlockSection>
      <apex:pageBlockSection title="Contact Information">
        <apex:inputField id="contactFirstName" value="{!contact.firstName}"/>
        <apex:inputField id="contactLastName" value="{!contact.lastName}"/>
        <apex:inputField id="contactPhone" value="{!contact.phone}"/>
      </apex:pageBlockSection>

<apex:commandButton value="Save" Action="{!save}" />

 Controller:

Account account;
   Contact contact;
 public Account getAccount() {
      if(account == null) account = new Account();
      return account;
   }

   public Contact getContact() {
      if(contact == null) contact = new Contact();
      return contact;
   }

 public PageReference save() {

      // Create the account. Before inserting, copy the contact's  
    
      // phone number into the account phone number field.  
    
      account.phone = contact.phone;
      insert account;

      // Create the contact. Before inserting, use the id field  
    
      // that's created once the account is inserted to create  
    
      // the relationship between the contact and the account.  
    
      contact.accountId = account.id;
      insert contact;
}

 

carlsosgcarlsosg

Thank you for your response. Is there a way to do this from the main Contact page - by including a VF page on the standard layout?

rohitsfdcrohitsfdc

Yes, you can do that. but the problem is that you cannot save account information from contact save button.

But if you want to see the account information, you can do that by creating a vf page on contact and calling its account field by using code like this

 

<Apex:inputField value="{!contact.account.accountnumber}" />

carlsosgcarlsosg

I'm sorry but this still doesn't seem to work. Could you please help me with details for a controller that would work to display and update Account fields from a VF page embedded in a standard Contact page layout?

rohitsfdcrohitsfdc

hi,

Like i said, your requirement is only possible if there are two save buttons in the contact page. one will save account info, and other contact info. if thats fine with you, i can help you wid the code

 

carlsosgcarlsosg

Yes - I understand. That is an acceptable solution. For some reason I can't get a standard controller to work with the embedded VF page.