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
Nick D'AddarioNick D'Addario 

Visualforce Page on Opportunity to Update Account Fields

Hi Guys,

I'm trying to build a Visualforce page on the opportunity record that allows users to update a few fields on the parent account. I have built the visualforce page, but can't get the newly input values to save back to the account. I'm fairly sure that Opportunity should be the standard controller and I'll need an extension. That's the part I'm having some trouble on.

Can someone point me in the right direction on how I'd accomplish this?

Thanks,
Nick
Best Answer chosen by Nick D'Addario
VamsiVamsi
Updated VF page 
<apex:page standardController="Opportunity" sidebar="false" showHeader="false" extensions="OppAccupdateController">
 <apex:form >
 <apex:pageBlock >
<apex:pageMessage detail="{!errormessage}" severity="error"/>
 <apex:pageblockButtons >
 <apex:commandButton value="Account Save" action="{!Accsave}" />
 </apex:pageblockButtons>
 <apex:pageBlockSection >
 
 <apex:outputText style="font-style:bold" value="Note: Updating these fields will update the associated account.">
 </apex:outputText>
 
 <apex:inputField value="{!acc.Account_Director__c}"/>
 <apex:inputField value="{!acc.Account_Manager__c}"/>
 <apex:inputField value="{!acc.Project_Manager__c}"/>
 <apex:inputField value="{!acc.Website}"/>
 </apex:pageBlockSection>
 
  </apex:pageBlock>
 </apex:form>
</apex:page>
Update Apex Class 
 
public class OppAccupdateController 
{
    public Account Acc {get;set;}
    public Opportunity Opp;
    public string errormessage {get;set;}
    public OppAccupdateController(Apexpages.StandardController controller)
    {
        opp = (Opportunity)controller.getRecord();
        ID accid = [select AccountID from Opportunity where ID=:opp.Id].AccountID;
        if(accid!=null)
        {
        acc = [select id,Website, Project_Manager__c, Account_Director__c, Account_Manager__c from account where ID=:accid]; // specify the list of fields after type seperated by comma from account that you have used in VF page 
        }
    }
    
    public void Accsave()
    {
     try{
        update acc;
        }catch(Exception e)
        {
        system.debug('exception'+' '+ e.getMessage());
        errormessage = e.getMessage();
        }
    }
    
}


 

All Answers

VamsiVamsi
Hi,

Please make use of the below code...

VF Page ..
<apex:page standardController="Opportunity" sidebar="false" showHeader="false" extensions="OppAccupdateController">
<apex:form>
<apex:pageBlock>
    <apex:pageMessages/>
    <apex:pageBlockButtons>
    <apex:commandButton value="Account Save" action="{!Accsave}"/>
    </apex:pageBlockButtons>
    <apex:pageBlockSection>
    <apex:inputField value="{!acc.Type}"/> <!--add fields below this field, that you would like to display from account-->
    </apex:pageBlockSection>
    </apex:pageBlock>    
</apex:form>
</apex:page>

Controller
public class OppAccupdateController 
{
    public Account Acc {get;set;}
    public Opportunity Opp;
	public OppAccupdateController(Apexpages.StandardController controller)
    {
        opp = (Opportunity)controller.getRecord();
        ID accid = [select AccountID from Opportunity where ID=:opp.Id].AccountID;
        if(accid!=null)
        {
        acc = [select id,type from account where ID=:accid]; // specify the list of fields after type seperated by comma from account that you have used in VF page 
        }
    }
    
    public void Accsave()
    {
        try{
        update Acc;
        }catch(Exception e)
        {
        system.debug('exception'+' '+ e.getMessage());
        }
    }
    
}

Hope this helps ...!!
Please mark as best answer if the above helps...!!!
Nick D'AddarioNick D'Addario
Hi Vamsi, 

Thanks so much for your help. I gave your code a try and it displays the fields exactly as it should (I did have to change the way the fields were referenced in the Visualforce Page). However, when I click "Account Save", it doesn't save back to the account. The visualforce page refreshes and has the new inputs that I just entered, but it doesn't appear to be saving back to the account. 

Thanks,
Nick
VamsiVamsi
Ok in VF page replace 
    <apex:pageMessages/> with  <apex:pageMessage detail="{!errormessage}" severity="error"/>

find the updated controller. So from this we can come to know the actual error message from VF page..
 
public class OppAccupdateController 
{
    public Account Acc {get;set;}
    public Opportunity Opp;
    public string errormessage {get;set;}
	public OppAccupdateController(Apexpages.StandardController controller)
    {
        opp = (Opportunity)controller.getRecord();
        ID accid = [select AccountID from Opportunity where ID=:opp.Id].AccountID;
        if(accid!=null)
        {
        acc = [select id,type from account where ID=:accid]; // specify the list of fields after type seperated by comma from account that you have used in VF page 
        }
    }
    
    public void Accsave()
    {
        try{
        update Acc;
        }catch(Exception e)
        {
        system.debug('exception'+' '+ e.getMessage());
        errormessage = e.getMessage();
        }
    }
    
}

Please let me know how it goes on ...may be some validations on Account blocking it.


 
VamsiVamsi
Also please make sure that Opportunity has Account Name filled in ...
Nick D'AddarioNick D'Addario
Hi Vamsi,

The opportunity I'm testing with does have an account associated with it already.
VamsiVamsi
Is it displaying any error message when you click on Account save ? Can you post your updated code ?
Nick D'AddarioNick D'Addario
public class OppAccupdateController 
{
    public Account Acc {get;set;}
    public Opportunity Opp;
    public OppAccupdateController(Apexpages.StandardController controller)
    {
        opp = (Opportunity)controller.getRecord();
        ID accid = [select AccountID from Opportunity where ID=:opp.Id].AccountID;
        if(accid!=null)
        {
        acc = [select id, type, Website, Project_Manager__c, Account_Director__c, Account_Manager__c from account where ID=:accid]; // specify the list of fields after type seperated by comma from account that you have used in VF page 
        }
    }
    
    public void Accsave()
    {
     try{
        update acc;
        }catch(Exception e)
        {
        system.debug('exception'+' '+ e.getMessage());
        }
    }
    
}
 
<apex:page standardController="Opportunity" sidebar="false" showHeader="false" extensions="OppAccupdateController">
 <apex:form >
 <apex:pageBlock >
<apex:pageMessages />
 <apex:pageblockButtons >
 <apex:commandButton value="Account Save" action="{!Accsave}" />
 </apex:pageblockButtons>
 <apex:pageBlockSection >
 
 <apex:outputText style="font-style:bold" value="Note: Updating these fields will update the associated account.">
 </apex:outputText>
 
 <apex:inputField value="{!Opportunity.Account.Account_Director__c}"/>
 <apex:inputField value="{!Opportunity.Account.Account_Manager__c}"/>
 <apex:inputField value="{!Opportunity.Account.Project_Manager__c}"/>
 <apex:inputField value="{!Opportunity.Account.Website}"/>
 </apex:pageBlockSection>
 
  </apex:pageBlock>
 </apex:form>
</apex:page>

Upon saving, the visualforce page looks like it refreshes and that's it,
VamsiVamsi
Updated VF page 
<apex:page standardController="Opportunity" sidebar="false" showHeader="false" extensions="OppAccupdateController">
 <apex:form >
 <apex:pageBlock >
<apex:pageMessage detail="{!errormessage}" severity="error"/>
 <apex:pageblockButtons >
 <apex:commandButton value="Account Save" action="{!Accsave}" />
 </apex:pageblockButtons>
 <apex:pageBlockSection >
 
 <apex:outputText style="font-style:bold" value="Note: Updating these fields will update the associated account.">
 </apex:outputText>
 
 <apex:inputField value="{!acc.Account_Director__c}"/>
 <apex:inputField value="{!acc.Account_Manager__c}"/>
 <apex:inputField value="{!acc.Project_Manager__c}"/>
 <apex:inputField value="{!acc.Website}"/>
 </apex:pageBlockSection>
 
  </apex:pageBlock>
 </apex:form>
</apex:page>
Update Apex Class 
 
public class OppAccupdateController 
{
    public Account Acc {get;set;}
    public Opportunity Opp;
    public string errormessage {get;set;}
    public OppAccupdateController(Apexpages.StandardController controller)
    {
        opp = (Opportunity)controller.getRecord();
        ID accid = [select AccountID from Opportunity where ID=:opp.Id].AccountID;
        if(accid!=null)
        {
        acc = [select id,Website, Project_Manager__c, Account_Director__c, Account_Manager__c from account where ID=:accid]; // specify the list of fields after type seperated by comma from account that you have used in VF page 
        }
    }
    
    public void Accsave()
    {
     try{
        update acc;
        }catch(Exception e)
        {
        system.debug('exception'+' '+ e.getMessage());
        errormessage = e.getMessage();
        }
    }
    
}


 
This was selected as the best answer
VamsiVamsi
Hi,

Hope the above answers your question.Please let me know,If you have any issues.

Please mark as best answer if the above helps ..!!!