+ Start a Discussion
OlbrestOlbrest 

How to send value from visualforce page to apex class ?

Hello.

 

How to send value from visualforce page to apex class ?

 

I tried to use this code, but it does not work.

 

 

global class Bibizanka { public String getBibizankaValue(String Bibiz){ return Bibiz; } }

 

 

<apex:page cache="true" showHeader="false" sidebar="false" controller="Bibizanka"> Bibizanka value = {!BibizankaValue('test')} </apex:page>

 

Thanks.

 

aalbertaalbert

I recommend starting with the Tutorials here.

 

OlbrestOlbrest

I asked a direct question.

Why you give me link to the tutorials ?

 

I'm read it and don't found the answer.

 

After that I asked on this board.

aalbertaalbert

Well, the code was missing several basic components so I thought the Tutorials would help. But regardless, here is a sample of using an inputText box and a button to post back. In short, you need to invoke some event or action that calls the controller. 

 

 

global class Bibizanka { public String BibizankaValue {get;set;} public PageReference doSomething(){ return null; } }

 

 

<apex:page cache="true" showHeader="false" sidebar="false" controller="Bibizanka"> <apex:form > <apex:outputLabel value="Enter BibizankaValue:" for="theTextInput"/> <apex:inputText value="{!BibizankaValue}" id="theTextInput"/> <apex:commandButton rerender="panelId" action="{!doSomething}" value="Post Back" id="theButton"/> <br></br> <apex:outputPanel id="panelId" > <apex:outputLabel value="BibizankaValue:" for="textId"/> <apex:outputText value="{!BibizankaValue}" id="textId" /> </apex:outputPanel> </apex:form> </apex:page>

 

 

saldmsaldm

I'm not sure if my question is the same as the one poised by Olbrest, but is there a way to directly pass a parameter into an apex extensionMy extension currently includes the following, which is an ugly work-aroundI want to determine the month and year as a string formatted a specific way (for this month, next month, and the next next month).

public String MonthLettersWOffset(Integer n) {
DateTime myDT = DateTime.now();
return myDT.addMonths(n).format('MMM yy');
} //public String getMonthLettersWOffset(Integer n) {
public String getThisMonthLetters() { return MonthLettersWOffset(0); }
public String getNextMonthLetters() { return MonthLettersWOffset(1); }
public String getNextNextMonthLetters() { return MonthLettersWOffset(2); }

 My Visual Force page looks like

{!ThisMonthLetters}
<!-- other code -->
{!NextMonthLetters}
<!-- still other code -->
{!NextNextMonthLetters}

 I'd love to be able to pass an offset directly from the Visual Force page

 

{!MonthLetters(0)}
<!-- other code -->
{!MonthLetters(1)}
<!-- still other code -->
{!MonthLetters(2)}

 

I did consider pulling all three at the same time as a list but I am unclear on whether or not I can access an individual element of a list from the Visual Force page code.
aalbertaalbert

You can use the <apex:param> to pass the value from the Page to the controller.

<apex:param name="give_it_a_name" assignTo="{!propertyName_to_set_in_controller}" value="value_goes_here" />

 

 

Jeremy-KraybillJeremy-Kraybill

Using param tags won't achieve what saldm is trying to do. Here is a solution that does most of what he needs without relying on controller code at all:

 

 

<apex:outputText value="string: {0,date,MMM yy}" > <apex:param value="{!NOW() + 30}"/> </apex:outputText>

 

HTH

 

Jeremy Kraybill

Austin, TX

 

saldmsaldm

I have been unsuccessful getting the param tag to work for me but if someone has example controller code I should try, I'm certainly willing to explore it further.  I feel that being able to pass a parameter into the controller will be helpful in general.

 

<apex:param name="monthOffset" assignTo="{!monthOffset}" value="1" />

Would I then need to create a class variable named monthOffset in my extension, which would have both a get and a set method?

I have not had any luck with this .

 

 

Thank you for the code Jeremy-Kraybill. 

If I'm correct, you are adding 30 days to the DateTime instance of this moment and formatting the output. 

 

My concern with this code is that at 1:00pm on January 1st, adding 30 days will put us at 1:00pm January 31st (not February).

Additionally, in the case today is January 31st, adding 30 days would put us in March (not February).

Jeremy-KraybillJeremy-Kraybill

Yeah, my code was just example code. Remember in VF templates you have full access to SF's templating language. You would want to do something more sophisticated, based on a function more like

 

DATE(YEAR(NOW()), MONTH(NOW())+1, DAY(NOW()))

And then also add an IF() statement based on month to catch when you need to increment the year.

 

HTH

 

Jeremy Kraybill

Austin, TX

 

saldmsaldm

Jeremy-Kraybill - thank you and I do see how with the full power of the templating language this can be done.

 

I am still interested in being able to pass a parameter into an extension, if there are any thoughts on that idea.

 

aalbertaalbert

This blog posting has an example of using the apex:param component to pass in variables from VF page to the controller's variables. 

 

In this example, the apex:param components are contained within the <apex:actionSupport> component which executes an action (ie calls the controller method named doLogin) and also calls the "setters" for the param variables, apiSessionId and apiServerURL. 

 

 

saldmsaldm

Based on this blog posting and the other comments in this thread, am I correct that one can NOT pass a parameter to the controller in the context I originally mentioned?

 

{!MonthLetters(0)} <!-- other code --> {!MonthLetters(1)} <!-- still other code --> {!MonthLetters(2)}

 

 

 

Jeremy-KraybillJeremy-Kraybill

That's correct, the templating language cannot pass parameters to the controller like that.

 

Jeremy Kraybill

Austin, TX

adgroadgro

So is there a solution to send a value to the apex controller class?

 

The following example is not working. The setter of CurrentClient is not called.

 

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

PAGE

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

 

 

<apex:page controller="ClientOnderzoekController">

    <apex:dataTable value="{!clients}" var="item" id="theTable" rowClasses="odd,even" styleClass="tableClass">
            <apex:facet name="caption">Clients Onderzoek</apex:facet>
            <apex:facet name="header">Here are the clients.</apex:facet>  
            <apex:column >
                    <apex:facet name="header">Name</apex:facet>
                    <apex:facet name="footer">---</apex:facet>
                    <apex:outputText value="{!item.Name}"/>
            </apex:column>
            <apex:column >
                    <apex:facet name="header">External</apex:facet>
                    <apex:facet name="footer">---</apex:facet>
                    <apex:outputText value="{!item.External__c}"/>
            </apex:column>
          <apex:column >
                    <apex:facet name="header">Cases</apex:facet>
                    <apex:facet name="footer">...</apex:facet>
                    <apex:param name="setItemToCurrentClient" assignTo="{!CurrentClient}" value="{!item}" />
                    <apex:dataTable value="{!casesOnderzoek}" var="caseItem" id="theTable" rowClasses="odd,even" styleClass="tableClass">
                        <apex:column >
                                <apex:facet name="header">Description</apex:facet>        
                                <apex:outputText value="{!caseItem.Description__c}"/>
                        </apex:column>
                    </apex:dataTable>                       
           </apex:column>               
    </apex:dataTable>
   
</apex:page>

 

 

 

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

CONTROLLER

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

 

public class ClientOnderzoekController {
                       
        List<ClientOnderzoek__c> clientsLst;

        public List<ClientOnderzoek__c> getClients() {
                if (clientsLst==null) clientsLst= [select Name, External__c, ID__c from ClientOnderzoek__c];
                return clientsLst;
        }
       
        public ClientOnderzoek__c CurrentClient
        {
            get { return currentClientVar; }
            set {
                currentClientVar = value;
                System.debug('CurrentClient was set');
                }
        }
        ClientOnderzoek__c currentClientVar;
       
        public List<CaseOnderzoek__c> getCasesOnderzoek() {       
            System.debug('getCasesOnderzoek was called');
            if (currentClient == null) {
                return new List<CaseOnderzoek__c>();
            } else {           
                List<CaseOnderzoek__c> casesLst = [select Name, Description__c from CaseOnderzoek__c cc where cc.ClientOnderzoek__r.Id=:CurrentClient.Id];
                return casesLst ;                                       
            }
        }
}

ShikibuShikibu

I wrestled with trying to create a list of records (pageBlockTable) with a checkBox selector per row, and then passing data about which rows are selected back to the controller.

 

The solution I came up with was to create a hidden inputText, use javascript to create a string array of the checkBox states and stuff it into the inputText, then use apex to parse the string into an array.

 

pedrosampaiopedrosampaio

has anyone solved this issue? is there a better way to pass values to an apex class?

 

thanks.