3

I need to create a VF page that I can insert on my Opportunity Detail page layout that will contain custom fields for the Account object. This will allow our sales reps to edit/update these fields when they're working on an opportunity and also allow me to build some validation logic on the opportunity page to require that these Account fields are updated before they can move the opportunity to the next stage in our sales process.

I'm extending the Opportunity controller so that I can edit fields for the Account object associated with the currently selected Opportunity. I'll be creating a VF page that I can place on my Opportunity layout to allow our sales reps to edit these fields while they're working on the opportunity (versus having to switch to the Account detail page).

The first field that I'm attempting to get this working for is a custom field that is a multiselect picklist called "SFA__c" and is a list of SFA tools.

This is what I've put together for my extension class (but it's giving me an error):

public class opportunityExtension{

private final Opportunity o;

public opportunityExtension(ApexPages.StandardController stdController)
    {
        this.o = (Opportunity)stdController.getRecord();
    }

public List<SelectOption> getSFA()
 {
      List<SelectOption> options = new List<SelectOption>();
      Schema.DescribeFieldResult fieldResult = Opportunity.Account.SFA__c.getDescribe();

      List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();

      for( Schema.PicklistEntry f : ple)
      {
        options.add(new SelectOption(f.getLabel(), f.getValue()));
      }
      return options;
  }
}

This doesn't seem to like the way I'm referrencing my SFA__c field? i get the following error:

"Method does not exist or incorrect signature: Opportunity.Account.SFA__c.getDescribe()"

I then have a VF page with the following code that I'm using to test this:

<apex:form >
<apex:pageBlock title="Edit Account Fields" mode="edit">
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}"/>
<apex:commandButton value="Cancel" action="{!cancel}"/>
</apex:pageBlockButtons>

<apex:selectList multiselect="true" size="9" value="{!SFA}" title="SFA Tool">
</apex:selectList>
</apex:pageBlock>
</apex:form>
</apex:page>

Can anyone help me with what I'm missing? Thank you!

3

I would try to use apex:inputField for this approach. The apex:inputField will generate all necessary html code automatically depending on the field type (in your case it will generate a multiselect picklist). Just create a dummy Account object and read the data from the database depending on the Account-ID from the Opportunity:

private final Opportunity o;
public Account acc { get; set; }

public opportunityExtension(ApexPages.StandardController stdController){
    this.o = (Opportunity)stdController.getRecord();

    if(this.o.Account != null){
        acc = [Select Id, SFA__c From Account Where id = : this.o.Account];
    }
    else{
        acc = new Account();
    }
}

Then you can reference accounts fields on the page using apex:inputField tag:

<apex:form >
    <apex:pageBlock title="Edit Account Fields" mode="edit">
        <apex:pageBlockButtons >
        <apex:commandButton value="Save" action="{!save}"/>
        <apex:commandButton value="Cancel" action="{!cancel}"/>
        </apex:pageBlockButtons>

        <apex:pageBlockSection>
            <apex:inputField value="{!acc.SFA__c}" />
        </apex:pageBlockSection>

    </apex:pageBlock>
</apex:form>
</apex:page>
2
  • Thank you for the help here. This was exactly what I needed and I now have it working. – Zigrivers Oct 30 '13 at 12:08
  • Awesome.... is there a way to get this functionality without having the save and cancel buttons immediately shown? In other words, only show them if someone actually changes one of the values? – Brian Adkins Dec 9 '14 at 22:42
0

I think it could be as simple as :

Schema.DescribeFieldResult fieldResult = Account.SFA__c.getDescribe();

You don't really need to know that the Account object belongs to an Opportunity; it doesn't make any difference on the type of field SFA__c is.

1
  • The solution of mast0r is preferable. Less code. – Marcel Meijer Oct 29 '13 at 13:18

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.