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
dfpittdfpitt 

Controller Field not updated

Hello,

 

I have been looking at an unusual situation.

 

I'm editing a record, specifically a lookup field on the record (which has up to this point been null). I have an action support (onchange) on the field that triggers a rerender on a picklist field that gets its options from a function in the controller that uses the lookup field value in a SOQL query.

 

The problem is that the value of the lookup field is still NULL when the query runs in the function.

 

However, if I fill in the lookup field, save and edit again, this time if I change the lookup field, the value IS up to date in the controller when I go and do the query.

 

What can be causing this? what can I do?

 

How can I get the value in the input field in a reliable way?

 

Here is the APEX Page and the Controller. (nPrescripcion.Medicamento__c is the lookup field that is not up to date in the controller when I run the query, I know this based on a system.debug statement)

 

<apex:page standardController="Prescripcion_Paciente__c" showHeader="false" sidebar="false" extensions="PrescripcionExtension">
    <apex:form style="padding-left:25px;padding-right:25px;padding-top:25px;">
        <apex:pageBlock title="Prescripción" tabstyle="Tratamiento__c"  mode="edit" >
            <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" value="Save" /> 
                <apex:commandButton action="{!cancel}" value="Cancelar" id="btn_Cancel"/>
                </apex:commandButton> 
            </apex:pageBlockButtons>  
            <apex:pageBlockSection id="MedicamentoVE" title="Medicamento" showHeader="true" collapsible="false" columns="2" >
                <apex:inputField label="Principio Activo" id="iPrincipioActivo"  required="true" value="{!Prescripcion_Paciente__c.Medicamento__c}">
                    <apex:actionSupport event="onchange" reRender="iMarca1,iMarca2" />
                </apex:inputField>
                <apex:selectList value="{!Prescripcion_Paciente__c.Marca_Comercial_1__c}" size="1" id="iMarca1" required="false">
                    <apex:selectOptions value="{!MarcasComerciales}"/>
                </apex:selectList>
            </apex:pageBlockSection>     
        </apex:pageBlock>
    </apex:form>  
</apex:page>

 

public class PrescripcionExtension {
    private Prescripcion_Paciente__c nPrescripcion;
    public ApexPages.StandardController stdCtrl { set; get; }
   
    public PrescripcionExtension (ApexPages.StandardController controller) {
        this.nPrescripcion= (Prescripcion_Paciente__c)controller.getRecord();
        stdCtrl=controller;
    }
    
    public List<SelectOption> getMarcasComerciales()
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('','--Ninguna--'));
        options.add(new SelectOption('Otra','Otra'));
        system.debug(nPrescripcion.medicamento__c);
        Tratamiento__c[] nMeds = [Select marcas__c from Tratamiento__c where id=:nPrescripcion.medicamento__c];
        if (nMeds.size()>0){
            if (nMeds[0].marcas__c!=null && nMeds[0].marcas__c!='')
            {
                String[] marcas = nMeds[0].marcas__c.split(';');
                for (Integer j=0;j<marcas.size();j++)
                {
                  options.add(new SelectOption(marcas[j],marcas[j]));
                }
            }
        }
        return options;
    }
}

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Edwin VijayEdwin Vijay

try this, wrap your inputfield and the rerender blocks into an actionregion using the apex:actionregion tag. Not sure, but might help

All Answers

Edwin VijayEdwin Vijay

try this, wrap your inputfield and the rerender blocks into an actionregion using the apex:actionregion tag. Not sure, but might help

This was selected as the best answer
Nantha_WipNantha_Wip

 

Record you have tried to load has null value for this field then it wont show up.

 

Can you check the value of {!Prescripcion_Paciente__c.Medicamento__c}.

If it has value then it should show up in your query

 

 

dfpittdfpitt

@Nantha_Wip the value is NULL on the first edit, but on the 2nd edit it works perfectly, not only is not null but it takes in the changes

 

Example:

- First Edit:

 - Change the field from BLANK --> ABC (The value in the controller is NULL)

- Second Edit:

 - Change the field from ABC --> DEF (the value in the controller is DEF)

dfpittdfpitt

@Edwin1

you were right!!!!, I put an actionregion around the fields and it work :-)

thanks a lot!

 

 

<apex:page standardController="Prescripcion_Paciente__c" showHeader="false" sidebar="false" extensions="PrescripcionExtension">
    <apex:form style="padding-left:25px;padding-right:25px;padding-top:25px;">
        <apex:pageBlock title="Prescripción" tabstyle="Tratamiento__c"  mode="edit" >
            <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" value="Save" /> 
                <apex:commandButton action="{!cancel}" value="Cancelar" id="btn_Cancel"/>
                </apex:commandButton> 
            </apex:pageBlockButtons>
            <apex:actionRegion>
            <apex:pageBlockSection id="MedicamentoVE" title="Medicamento" showHeader="true" collapsible="false" columns="2" >
                <apex:inputField label="Principio Activo" id="iPrincipioActivo"  required="true" value="{!Prescripcion_Paciente__c.Medicamento__c}">
                    <apex:actionSupport event="onchange" reRender="iMarca1,iMarca2" />
                </apex:inputField>
                <apex:selectList value="{!Prescripcion_Paciente__c.Marca_Comercial_1__c}" size="1" id="iMarca1" required="false">
                    <apex:selectOptions value="{!MarcasComerciales}"/>
                </apex:selectList>
            </apex:pageBlockSection>
            </apex:actionRegion>     
        </apex:pageBlock>
    </apex:form>  
</apex:page>

 

Edwin VijayEdwin Vijay

Cool!!