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
Олег СавинковОлег Савинков 

Obtain the value of a field in the controller

I'm going to override the standard controller for my object .  Do insertion a new record  ( record in base still there is no).  Page

<apex:page standardController="CallReportHosp__c" extensions="CtrlCallHospEdit" >
<apex:outputPanel rendered="{!IF($CurrentPage.parameters.theType = 'app', false, true)}" >
            <apex:pageBlockSection columns="2">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="{!$ObjectType.CallReportHosp__c.fields.AccountID__c.label}" for="organisation"/>
                    <apex:inputField value="{!CallReportHosp__c.AccountID__c}" id="organisation" >
                         <apex:actionSupport event="onchange"  reRender="pblock" status="status" action="{!SelectService}" />
                    </apex:inputField>
                </apex:pageBlockSectionItem>
Controller
public with sharing class CtrlCallHospEdit {


public Boolean booleanValue {get;set;}
pablic CallReportHosp__c rep;

 public CtrlCallHospEdit(ApexPages.StandardController controller)
    {
       this.booleanValue=false;
       rep = (CallReportHosp__c)controller.getRecord();

       System.debug(' bool= '+booleanValue);
       SelectService();      
    }
 
 
    
public void SelectService(){
system.debug('selectservice=');
        if(rep.AccountID__c !=null)
           { booleanValue = true; System.debug('acc= '+rep.AccountID__c); }
           else {booleanValue=false; System.debug('acc= '+rep.AccountID__c); }
    }     
}
I changed value in field AccountID__c in page, but i don't have its value in controller ( i have rep.AccountID__c = null).
I remind you that I need to get the value of the field BEFORE saving the record in the database (before the call Save).
Tell me what I'm doing wrong
 
Best Answer chosen by Олег Савинков
Dushyant SonwarDushyant Sonwar

Please try with this one

 
<apex:actionRegion>
<apex:inputfield value="{!rep.AccountID__c}" id="organisation"  required="true">
                         <apex:actionSupport event="onchange"  reRender="pblock,,message,contactPanel" status="status" action="{!SelectService}" />
</apex:inputfield>
</apex:actionRegion>

<apex:outputPanel  id="contactPanel">
    <apex:outputLabel rendered="{!Not(rep.AccountId__c == null)}" value="Contact" for="contacts"/>
    <apex:inputfield value="{!rep.ContactId__c}" rendered="{!Not(rep.AccountId__c == null)}"   />
</apex:outputPanel>

This will show contactId field when accountd has some value.
 

All Answers

Dushyant SonwarDushyant Sonwar
Change
<apex:inputField value="{!CallReportHosp__c.AccountID__c}" id="organisation" >
to
<apex:inputField value="{!rep.AccountID__c}" id="organisation" >

and
add this line
rep = new CallReportHosp__c();
public CtrlCallHospEdit(ApexPages.StandardController controller)
    {
       this.booleanValue=false;
       rep = new CallReportHosp__c();
       rep = (CallReportHosp__c)controller.getRecord();

       System.debug(' bool= '+booleanValue);
       SelectService();      
    }


Hope this helps!
Олег СавинковОлег Савинков
Hello! I do it, but have error in page:   Error: Unknown property 'CallReportHosp__cStandardController.rep'
Олег СавинковОлег Савинков
Help, please! I changed the code but still can't get the value of the field in the controller
Page:
<apex:page standardController="CallReportHosp__c" extensions="CtrlCallHospInsert" id="CallsHospEditt">
<script type="text/javascript">
function onChangeOrganisation() {
         booleanValue = true;   
         openPopup("/apex/TM_AccountsList?context=parents", "lookup", 900, 600, "width=900,height=600,toolbar=no,status=no,directories=no,menubar=no,resizable=yes,scrollable=no", true);
        }
</script>
    <apex:form >

        <apex:pageBlock mode="edit" id="pblock" >
            <apex:pageMessages id="message" />
            <apex:pageBlockButtons location="top">                                
                <apex:actionStatus id="mySaveStatus1">
                <apex:facet name="stop">
                <apex:commandButton value="Save" action="{!save}" rerender="pblock" status="mySaveStatus1"/>
                </apex:facet>
                <apex:facet name="start">
                <apex:commandButton value="Save" status="mySaveStatus1" disabled="true"/>
                </apex:facet>
                </apex:actionStatus>
                <apex:commandButton value="Cancel" action="{!cancel}"/>
            </apex:pageBlockButtons>

            <apex:pageBlockSection columns="2">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Organisation" for="organisation"/>
                    <apex:inputfield value="{!rep.AccountID__c}" id="organisation"  required="true">
                         <apex:actionSupport event="onchange"  reRender="pblock" status="status" action="{!SelectService}" />
                    </apex:inputfield>
                </apex:pageBlockSectionItem>

                <apex:pageBlockSectionItem >


<apex:outputPanel rendered="{!booleanValue}" >
    <apex:outputLabel value="Contact" for="contacts"/>
    <apex:inputfield value="{!rep.ContactId__c}"   />
</apex:outputPanel>


                </apex:pageBlockSectionItem>

               
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Date" for="datetime"/>
                    <apex:inputfield value="{!rep.Date__c}" id="theDate"/>
                </apex:pageBlockSectionItem>
                
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Time" for="time"/>
                    <apex:inputfield value="{!rep.CallTime__c}" id="theTime"/>
                
                </apex:pageBlockSectionItem>
                
                
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Type of visit" for="type"/>
                    <apex:inputfield value="{!rep.CallType__c}" id="type"/>
                    </apex:pageBlockSectionItem>
    
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Duration" for="duration"/>
                    <apex:inputfield value="{!rep.Duration__c}" id="duration"/>
                </apex:pageBlockSectionItem>
              
              <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Plan/fact" for="planned"/>
                    <apex:inputfield value="{!rep.Planned__c}" id="planned"/>
                </apex:pageBlockSectionItem>
              
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Speciality" for="speciality"/>
                    <apex:inputfield value="{!rep.Speciality_Hosp__c}" id="planned"/>
                </apex:pageBlockSectionItem>
                
             </apex:pageBlockSection>
              
            
              <apex:pageBlockSection title="Discussed Products" columns="1" >
              <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Discussed Products" for="products"/>
                    <apex:inputfield value="{!rep.Field1_del__c}" id="products"/>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
          
        </apex:pageBlock>
        
        
    </apex:form>
    
</apex:page>

Controller:
public class CtrlCallHospInsert {

public Boolean booleanValue {get;set;}
public CallReportHosp__c rep {get; set;}


private ApexPages.StandardController controller { get; set; }

    public CtrlCallHospInsert(ApexPages.StandardController controller) {
         this.controller = controller;
//         this.rep = new CallReportHosp__c();
         this.rep = (CallReportHosp__c)controller.getRecord();   
         this.booleanValue=false;       
         System.debug(' bool= '+booleanValue);
         SelectService();
    }




public void SelectService(){

system.debug('selectservice=');
this.rep = (CallReportHosp__c)controller.getRecord();
String Acc=rep.AccountID__c; System.debug('value= '+Acc);
        if (String.isBlank(Acc))
           {this.booleanValue=false; System.debug('acc= '+Acc);
           }
           else {this.booleanValue = true; System.debug('acc= '+Acc);  }
    }   

public PageReference save(){
        
        try {
            insert rep;
        }catch (Exception e){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, '"Something went wrong !"'));
        }
        PageReference pageRef = Page.CallsHospList;
        pageRef.setRedirect(true);
        return pageRef;
    }
    
 public PageReference cancel() {
        PageReference pageRef = Page.CallsHospList;
        pageRef.setRedirect(true);
        return pageRef;        
    }
    
}
I need to get the value of the field AccountID__c  after some value is entered in it. I tried different code I found on the Internet, but nothing works.


 
Dushyant SonwarDushyant Sonwar
remove below line from select service method as you already binding  rep object.
this.rep = (CallReportHosp__c)controller.getRecord();

public void SelectService(){

system.debug('selectservice=');
System.debug(rep.AccountID__c + ' this is my accountId');
String Acc=rep.AccountID__c; System.debug('value= '+Acc);
        if (String.isBlank(Acc))
           {this.booleanValue=false; System.debug('acc= '+Acc);
           }
           else {this.booleanValue = true; System.debug('acc= '+Acc);  }
    }

also rerender the pageblock message in case any error
<apex:actionSupport event="onchange"  reRender="pblock,message" status="status" action="{!SelectService}" />
<apex:pagemessages id="message" />
it will show you the error and it will be make easy to debug.

Hope this helps!
Олег СавинковОлег Савинков
I corrected my code, but it still not work. I have Acc= null. And i haven't any errors.
I want that when entering any value in the field AccountID__c (but before writing to the database) the variable  booleanValue changes so that another input field  ContactId__c is displayed.
Dushyant SonwarDushyant Sonwar

Please try with this one

 
<apex:actionRegion>
<apex:inputfield value="{!rep.AccountID__c}" id="organisation"  required="true">
                         <apex:actionSupport event="onchange"  reRender="pblock,,message,contactPanel" status="status" action="{!SelectService}" />
</apex:inputfield>
</apex:actionRegion>

<apex:outputPanel  id="contactPanel">
    <apex:outputLabel rendered="{!Not(rep.AccountId__c == null)}" value="Contact" for="contacts"/>
    <apex:inputfield value="{!rep.ContactId__c}" rendered="{!Not(rep.AccountId__c == null)}"   />
</apex:outputPanel>

This will show contactId field when accountd has some value.
 
This was selected as the best answer
Олег СавинковОлег Савинков
It works! Thank you!!!!
Dushyant SonwarDushyant Sonwar
Welcome ,Glad It helped you :)