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 ?

  • Have you queried this value - Unit__c.Tenant__r.AccountId, in controller? – Raul Mar 27 '17 at 4:52
  • @RahulSharma I'm using the standard controller. I didn't query anything – ChamingaD Mar 27 '17 at 4:55
  • May be because both are binded to same object Unit__c. Try binding it to different field. – Rohit Mourya Mar 27 '17 at 5:26
  • @RohitMourya Unit__c,Tenant__r,AccountId are three differnt objects – ChamingaD Mar 27 '17 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. – Santanu Halder Mar 27 '17 at 7:09
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>
  • 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 Mar 27 '17 at 23:13
  • 1
    yes you need to query all the field you required. – sfdcfolk Mar 28 '17 at 5:42
  • Thanks. Also, do i have to specify all the fields of Opportunity obj. in fields list ? – ChamingaD Mar 28 '17 at 21:47
  • Only those which are required to you – sfdcfolk Mar 29 '17 at 5:22

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.