1

I got a page with standard controller and extension.

<apex:page id="thePage" standardcontroller="Unit__c" extensions="Unit_Ext" ....

Then I have blinded output field with a child relationship.

<apex:outputPanel id="fields" ....
    <apex:outputfield value="{!Unit__c.Tenant__r.AccountId}" ....

There is a dropdown field for Tenant__c and it re-renders above output field when changed.

<apex:selectList multiselect="false" size="1" value="{!Unit__c.Tenant__c}">
    <apex:actionSupport event="onchange" rerender="fields"/>
    <apex:selectOptions value="{!Tenants}"/>
</apex:selectList>

When it rerender, Unit__c.Tenant__c is getting updated but Unit__c.Tenant__r.AccountId remain same.

How do i fix this ?

10
  • Have you queried this value - Unit__c.Tenant__r.AccountId, in controller?
    – Raul
    Commented Mar 27, 2017 at 4:52
  • @RahulSharma I'm using the standard controller. I didn't query anything
    – ChamingaD
    Commented Mar 27, 2017 at 4:55
  • May be because both are binded to same object Unit__c. Try binding it to different field. Commented Mar 27, 2017 at 5:26
  • @RohitMourya Unit__c,Tenant__r,AccountId are three differnt objects
    – ChamingaD
    Commented Mar 27, 2017 at 5:29
  • 2
    After the change of dropdown value, if you are not changing the value of Unit__c.Tenant__r.AccountId, Unit__c.Tenant__r to be precise, your outputField will not change. It will hold the same old value. Commented Mar 27, 2017 at 7:09

1 Answer 1

2

try something like this,

Public class OppExtention{
    public Map<id,Contact> conts { get; set;}
    public Opportunity opp {get; set;}
    public string contId {get; set;}
    public OppExtention(ApexPages.StandardController controller) {
        List<String> fields = new List<String>{'id','contact__c','contact__r.AccountId','contact__r.name','name'}; 
        conts = new Map<id,Contact>([Select id, name, AccountId from contact where AccountId <> null limit 5]);
        controller.addFields(fields);
        opp = (Opportunity)controller.getRecord();
    }

    public List<SelectOption> getOptions() {
      List<Selectoption> opt = new List<SelectOption>();

      for(String key: conts.keySet())
          opt.add(new SelectOption(key,conts.get(key).Name));
        return opt;
    }

    public void onChangeOption() {
        opp.contact__r.AccountId = conts.get(opp.contact__c).AccountId;
    }

}

 <apex:selectList multiselect="false" size="1" value="{!opp.contact__c}" >
      <apex:selectOptions value="{!options}"/>
           <apex:actionSupport event="onchange" rerender="fields" action="{!onChangeOption}">
      </apex:actionSupport>
 </apex:selectList>
4
  • So, according to this code, If I referer more fields in Contact__c object, Do I have to query field each in conts map?
    – ChamingaD
    Commented Mar 27, 2017 at 23:13
  • 1
    yes you need to query all the field you required.
    – sfdcfolk
    Commented Mar 28, 2017 at 5:42
  • Thanks. Also, do i have to specify all the fields of Opportunity obj. in fields list ?
    – ChamingaD
    Commented Mar 28, 2017 at 21:47
  • Only those which are required to you
    – sfdcfolk
    Commented Mar 29, 2017 at 5:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .