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
LithiumsLithiums 

Method not getting called

I was trying to call the click() function, when a value is selected the selectoption.

 

But I dont see the call going through, I see the javascript function being call and than call goes to apex:function.

In the debug I can see getStageValue() called again on selecting value in selectoption.  Is the selectoption stopping the call?

 

<apex:page standardController="Opportunity" extensions="LSOpportunityDetailsController">
    
  <script type="text/javascript">
            function getStageValue(id){
                displayFunction(document.getElementById(id).value);   
            }
  </script>  
 
  <apex:form >
      <apex:actionFunction action="{!click}" name="displayFunction"  rerender="pageblock">
                <apex:param name="stageValue" value=""/>
      </apex:actionFunction>
      
      <apex:pageBlock  id="pageblock">
            <apex:outputlabel value="Stage:" />
            <apex:selectList value="{!stage}" multiselect="false" size="1" id="stageValue" onchange="getStageValue('{!$Component.stageValue}')">
                <apex:selectOptions value="{!StageValue}" />
            </apex:selectList>    
            
        </apex:pageBlock>  
  </apex:form>
</apex:page>

------------------------------------

public with sharing class LSOpportunityDetailsController  {



    private final Opportunity opp;
    private final String procName;
    private Id currentPhase;
    String [] stage = new String []{};
    String para {get;set;}
    List<Goal__c> goalList {get;set;}
    private String stageVal;
    
   
    public LSOpportunityDetailsController(ApexPages.StandardController stdController) {

        System.debug('Called LSOpportunityDetailsController CTOR');
        String i = stdController.getId();
        if (i == null) {
            procName =  'No Id';
            this.opp = new Opportunity();
        }
        else {            
            this.opp = [select id, name, ProcessId__c from Opportunity where id = :i];        
            if (opp.ProcessId__c != null) {
                procName = [SELECT Name FROM Sales_Process__c where Id=:opp.ProcessId__c].Name;
                
            }
        }
                  
    }
    
    public LSOpportunityDetailsController(Id oppId) {
        this.opp = [select id, name, ProcessId__c from Opportunity where id = :oppId];
                
        if (opp.ProcessId__c != null) {
            procName = [SELECT Name FROM Sales_Process__c where Id=:opp.ProcessId__c].Name;
        }
    }
    
    public String[] getStage(){
        return stage;
    }
    
    public void setStage(String [] stage){
        this.stage = stage;
    }
    
    public  List<SelectOption> getStageValue(){
        
        
        List<SelectOption> options = new List<SelectOption>();
        List <Phase__c> phaseList = [select Name from Phase__c where Sales_Process__c=:opp.ProcessId__c ];
        
        for(Phase__c phase:phaseList){
                options.add(new SelectOption(phase.Name,phase.Name));
        }
        return options;
        
    }
    
    public PageReference  click(){
        system.debug('Test' + ApexPages.CurrentPage().getParameters().get('stageValue'));
        stageVal = ApexPages.CurrentPage().getParameters().get('stageValue');
        return null;
    }
    

}

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

So I've taken your code that you posted and added a cut down version to my dev org.  Initially I saw the same behaviour that you did, so I added an <apex:pageMessages /> component and rerendered that, which displayed an error that the stage property couldn't be found.  This is because the stage property is declared as an array of strings, but if the selectlist isn't multi-select, then it needs to be a single string.  The following page and code work correctly (at least in my chrome browser):

 

Page:

 

<apex:page standardController="Opportunity" extensions="Test">

  <apex:pageMessages id="msgs"/>
  <apex:form id="theform">
      <apex:pageBlock id="pageblock">
            <apex:outputlabel value="Stage:" />
            <apex:selectList value="{!stage}" multiselect="false" size="1" id="stageValue">
                <apex:selectOptions value="{!StageValue}" id="sloptions"/>
                <apex:actionsupport event="onchange" action="{!click}" rerender="stageout, msgs">
                </apex:actionsupport>
            </apex:selectList>      
            <br/>
            <apex:outputText value="{!stage}" id="stageout"/>
        </apex:pageBlock>  
  </apex:form>
</apex:page>

 

Code:

 

public with sharing class Test {
        
        private final Opportunity opp;
        String stage;
         
        public Test(ApexPages.StandardController stdController) {

        String i = stdController.getId();

            this.opp = [select id, name from Opportunity where id = :i];        
                  
        }
    
    public String getStage(){
        return stage;
    }
    
    public void setStage(String stage){
        this.stage = stage;
    }
    
    public  List<SelectOption> getstageValue(){
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Name1', 'Name1'));
        options.add(new SelectOption('Name2', 'Name2'));
        options.add(new SelectOption('Name3', 'Name3'));
        options.add(new SelectOption('Name4', 'Name4'));
        
        return options;
        
    }
    
    public void click(){
        system.debug('In -------------->');
    }
}

 

All Answers

asish1989asish1989

Hi 

   Try using 

           <apex:selectList value="{!stage}" multiselect="false" size="1" id="stageValue" 

<apex:actionSupport event = "onchange" action = " {!click}" rerender = "pageblock" />
                <apex:selectOptions value="{!StageValue}" />
            </apex:selectList>   

 

Dont use <apex:actionFunction > and javascript  . I am not saying you are doing wrong . But This is  the easy way .

 

 

Thanks

asish

LithiumsLithiums

Thanks Ashish for the quick reply

 

I tried action support and it doesnt work, I think this is a salesforce bug. I tried several options and it does not work.Looks like apex:selectoption and action functions have an issue when used together.

asish1989asish1989

Hi

   Action Support works. I have checked . may be there is some mistake .

   This is page .You saved and run. Then You will get some idea how action support woks .

 

        <apex:page standardController="Case" recordSetvar="cases">
    <apex:pageBlock >
        <apex:form id="theForm">
            <apex:outputPanel id="list">
            <apex:panelGrid columns="2">
                <apex:outputLabel value="View:"/>
                <apex:selectList value="{!filterId}" size="1">
                    <apex:actionSupport event="onchange" rerender="list" action={!yourmethodincontroller}/>
                    <apex:selectOptions value="{!listviewoptions}"/>
                </apex:selectList>
            </apex:panelGrid>
            <apex:pageBlockSection >
                <apex:dataList var="c" value="{!cases}" >
                {!c.subject}</apex:dataList>
            </apex:pageBlockSection>
            </apex:outputPanel>
           
        </apex:form>
    </apex:pageBlock>
</apex:page>    

 

Thanks

asish

LithiumsLithiums
I was calling a method in controller class, that is where I am facing issue. Sent from my iPhone
asish1989asish1989

Hi

  try by using ActionFunction .Your code is Write I think . I would like to Add some point .

   please declair variable like this

    public String stageval{get;set;}

   

you have written like this

     stageVal = ApexPages.CurrentPage().getParameters().get('stageValue');

 

    one more point is write System in the place of Apex .

     stagevalue = System.currentPageReference().getParameters().get(stageValue);

 

 

Did this post answers your questions ...If so please mark it solved so that It can be refered .

 

thanks

asish

 

  

 

   

 

bob_buzzardbob_buzzard

Are you using Internet Explorer?  That handles onchange events differently to other browser and only fires them when the element lose focus.

LithiumsLithiums
I am using Firefox and chrome. Sent from my iPhone
bob_buzzardbob_buzzard

I can second that actionsupport works, as I'm using exactly that technique on one of my pages.

 

Here's the sample code:

 

<apex:selectList value="{!entry.entType}" size="1">
  <apex:selectOptions value="{!entryTypes}"/>
  <apex:actionSupport action="{!entryTypeChanged}" event="onchange" rerender="timesheet, msgs" status="working">
    <apex:param name="entryTypeChanged" value="{!entry.id}" assignTo="{!chosenEntry}" />
  </apex:actionSupport>
</apex:selectList>

 The only difference I can see after a quick scan is that I am rerendering an apex:messages component in case of errors and I use the assignTo attribute to populate the parameter in the controller.

LithiumsLithiums
I will give it another try, thanks a ton bob Sent from my iPhone
LithiumsLithiums

Bob, I tried to  use the code which you have given and it doesnt work. Do you happen to know if salesfoce has made any changes? I used the same concept without selectoptions and it works fine, looks like there is an issue with select options.

 

I am just trying to call the click() method in the extension.But when a value is changing in selectoptions it ends up calling the getstagevalue() method and stops there.

 

Is there any other way I can accomplish this?

 

<apex:page standardController="Opportunity" extensions="Test">

  <apex:form id="theform">
      <apex:pageBlock id="pageblock">
            <apex:outputlabel value="Stage:" />
            <apex:selectList value="{!stage}" multiselect="false" size="1" id="stageValue">
                <apex:selectOptions value="{!StageValue}" id="sloptions"/>
                <apex:actionsupport event="onchange" action="{!click}" rerender="">
                </apex:actionsupport>
            </apex:selectList>      
        </apex:pageBlock>  
  </apex:form>
</apex:page

-------------------------------------------

public with sharing class Test {
        
        private final String procName;
        private final Opportunity opp;
        String [] stage = new String []{};
         
        public Test(ApexPages.StandardController stdController) {

        String i = stdController.getId();

            this.opp = [select id, name, ProcessId__c from Opportunity where id = :i];        
            if (opp.ProcessId__c != null) {
                procName = [SELECT Name FROM Sales_Process__c where Id=:opp.ProcessId__c].Name;
                
            }
                  
        }
    
    public String[] getStage(){
        return stage;
    }
    
    public void setStage(String [] stage){
        this.stage = stage;
    }
    
    public  List<SelectOption> getstageValue(){
        List<SelectOption> options = new List<SelectOption>();
        List <Phase__c> phaseList = [select Name from Phase__c where Sales_Process__c=:opp.ProcessId__c  Order by Order__c];
        
        for(Phase__c phase:phaseList){
                options.add(new SelectOption(phase.Name,phase.Name));
        }
        return options;
        
    }
    
    public void click(){
        system.debug('In -------------->');
    }
}

bob_buzzardbob_buzzard

As you have an empty rerender condition, no part of the page will be redrawn once the request completes, so the revised values never make it to the page. I'd rerender 'pageblock' if I were you.

LithiumsLithiums

Done, but still no difference.

 

I gave it a try with apex:selectRadio as well, but no improvement

bob_buzzardbob_buzzard

I don't think its a Salesforce problem, as I've just used my page again and its all working fine.

LithiumsLithiums

I have run out of options now,I dont know what to do.

 

bob_buzzardbob_buzzard

What are you expecting to happen with the latest incarnation of the code posted above?  I'd expect the selectlist to remain as is, but highlight the option you have chosen.

LithiumsLithiums

I was expecting to call the method in the controller where I would have my business logic built which would return a list of values based on the selectoption value.

bob_buzzardbob_buzzard

But that's not what the code up above would do - that would simply log a message and go back to the page.

LithiumsLithiums

I havent put in the logic yet (I have to change the return type and return the list, i didnt want to confuse with too much of info in there so i trimmed it), my first step was to get the call to the method which itself is not happening.

bob_buzzardbob_buzzard

So you aren't seeing the click debug in the log?

LithiumsLithiums

No, I dont see the click debug. When the value changes the getStageValue() is getting called and it ends there.

 

That is the issue I am facing, if the call gets there my issue is resolved.

bob_buzzardbob_buzzard

getStageValue should be one of the last things that happens, as the getters are executed when the revised version of the page is rendered.  I'd expect the setter first, then the pagereference, then the getter.

LithiumsLithiums

Thats right bob, but it ends after that, the call doesnt go to the click() method.

 

Thanks for your patience and time

LithiumsLithiums

So looks like we are at a dead end, if you can probably give an example in your blog sometime later. It would help someone like  me.

bob_buzzardbob_buzzard

So I've taken your code that you posted and added a cut down version to my dev org.  Initially I saw the same behaviour that you did, so I added an <apex:pageMessages /> component and rerendered that, which displayed an error that the stage property couldn't be found.  This is because the stage property is declared as an array of strings, but if the selectlist isn't multi-select, then it needs to be a single string.  The following page and code work correctly (at least in my chrome browser):

 

Page:

 

<apex:page standardController="Opportunity" extensions="Test">

  <apex:pageMessages id="msgs"/>
  <apex:form id="theform">
      <apex:pageBlock id="pageblock">
            <apex:outputlabel value="Stage:" />
            <apex:selectList value="{!stage}" multiselect="false" size="1" id="stageValue">
                <apex:selectOptions value="{!StageValue}" id="sloptions"/>
                <apex:actionsupport event="onchange" action="{!click}" rerender="stageout, msgs">
                </apex:actionsupport>
            </apex:selectList>      
            <br/>
            <apex:outputText value="{!stage}" id="stageout"/>
        </apex:pageBlock>  
  </apex:form>
</apex:page>

 

Code:

 

public with sharing class Test {
        
        private final Opportunity opp;
        String stage;
         
        public Test(ApexPages.StandardController stdController) {

        String i = stdController.getId();

            this.opp = [select id, name from Opportunity where id = :i];        
                  
        }
    
    public String getStage(){
        return stage;
    }
    
    public void setStage(String stage){
        this.stage = stage;
    }
    
    public  List<SelectOption> getstageValue(){
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Name1', 'Name1'));
        options.add(new SelectOption('Name2', 'Name2'));
        options.add(new SelectOption('Name3', 'Name3'));
        options.add(new SelectOption('Name4', 'Name4'));
        
        return options;
        
    }
    
    public void click(){
        system.debug('In -------------->');
    }
}

 

This was selected as the best answer
LithiumsLithiums

Thats awesome !!!!!!!!!!

 

Thanks a mill, as always you did it again.