So here is my requirement :-

I want to populate opportunity Name same as Account Name on my visual force opportunity Page.

what i have done so far :- I have written a class

public class testopportunity {
public Opportunity c;
public Account acc ;
public testopportunity(ApexPages.StandardController ctrl){
    c = (Opportunity) ctrl.getRecord();

public void fetchRelatedAccountData(){

        if(c.AccountID!= null){
  acc = [SELECT Name
        FROM Account WHERE Id = :c.AccountID];

        c.Name =  acc.Name;


Visual force code i have used is

<apex:PageBlockSectionItem >
              <apex:outputLabel value="Product"/>
         <apex:actionRegion >
               <apex:inputField value="{!Opportunity.Product_picklist__c}" >
              <apex:actionSupport event="onchange" reRender="ajaxrequest" />

But strange thing happening is the VF page says first fill all the mandatory field and my bad luck is all the field are mandatory.

Can we achieve this requirement by java script. Or i have to modify my code?

Kindly help!

1 Answer 1


AN easier way to do this would be to:

  1. Put <apex:outPutField value="{!Opportunity.Account.Name}" rendered="false"> on the VF page
  2. Change your constructor to:

    public testopportunity(ApexPages.StandardController ctrl){
    c = (Opportunity) ctrl.getRecord();
    c.Name = c.Account.Name == null ? c.Name : c.Account.Name;

1: Makes the Account Name field available to the getRecord() method without having to use a query (Only works of the Opportunity has an account associated with it)

2: will change the name if the account name is not null and thus the page will display the updated name.

You will need to decide when you actually update the opportunity with the new name.

For you other issue on required fields, research it will allow you to submit data from a particular part of the page without checking the validations on the rest of the page

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.