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
Ashish BiswasAshish Biswas 

Passing parameter from VF page to controler method

Hi,
I have controler method that takes 2 string parameter as input and accordingly after processing internal logic return a boolean value. For Ex. the below
UserCredentialcheck(){
        if(ud <> UID && pw <> PWD){
           return false;
        }
        else{
            return true;
        }
}

Here UID and PWD are predefined values defined earlier on top of the class.
I would like to call this method from VF page along with the parameter. How shall I acheive this. Please suggest and guide.

Thanks in advance
Ashish
Siddharth ManiSiddharth Mani
Please check usage of <apex:param> tag:

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_param.htm

You can use this to pass values from your page to your custom controller for business logic.
mritzimritzi
Sample code, please modify it as per your actual requirement

Apex:
public class TestClass2{
    // variables referred in VF
    public String UID{get;set;}
    public String PWD{get;set;}
    //variables initialized in controller (Apex class)
    // make sure to use same data types for (ud,UID) & (pw,PWD) in apex and in VF
    String ud;
    String pw;
    public TestClass2(){
            // followinf values just for example
            ud='someValue';
            pw='someOtherValue';
    }
    // function called from VF on button click
    public void buttonClick(){
        Boolean var = UserCredentialcheck();
    }
    // your function
    public Boolean UserCredentialcheck(){
        If(UID!=ud && PWD!=pw)
            return false;
        else
            return true;
    }
}

VF:
<apex:page controller="TestClass2">
<apex:form >
    <apex:pageblock >
        <apex:pageblockSection columns="4">
            UID: <apex:inputText value="{!UID}"/>
            PWD: <apex:inputText value="{!PWD}"/>
        </apex:pageblockSection>
        <apex:pageblockButtons >
            <apex:commandButton value="Check" action="{!buttonClick}"/>
        </apex:pageblockButtons>
    </apex:pageblock>
</apex:form>
</apex:page>

Mark this as Best Answer, if this helps solve your problem.
Gabriel ArribasGabriel Arribas
Hi Ashish,
here is an example with a button, I hope it help you.

VISUALFORCE CODE:
<apex:commandButton value="Send" action="{!doSomething}">
        <apex:param assignTo="{!parameter1}" value="{Object.field}" />
</apex:commandButton>

CONTROLLER CODE:
public Integer parameter1 { get; set; }

public yourTye Send() {
     String str= ApexPages.currentPage().getParameters().get('parameter1');
     ....
     ....
}
Ashish BiswasAshish Biswas
Thanks for the solution, I have modified the code as shown belo -
VF -
<apex:page controller="apxLoginCheck">
<apex:form >
    <apex:pageblock >
        <apex:pageblockSection columns="4">
            UID: <apex:inputText value="{!UID}"/>
            PWD: <apex:inputText value="{!PWD}"/>
        </apex:pageblockSection>
        <apex:pageblockButtons >
            <apex:commandButton value="Check" action="{!buttonClick}" oncomplete="show()"/>
        </apex:pageblockButtons>
        <script type="text/javascript">
            function show(){
                var res = '{!isValidCredential}';
                if(var==true){
                    alert('Login Success');
                    }
            }
        </script>
    </apex:pageblock>
</apex:form>
</apex:page>

Controler -
public class apxLoginCheck {

// variables referred in VF
    public String UID{get;set;}
    public String PWD{get;set;}
    public boolean isValidCredential{get; set;}
    //variables initialized in controller (Apex class)
    // make sure to use same data types for (ud,UID) & (pw,PWD) in apex and in VF
    String ud;
    String pw;
    public apxLoginCheck(){
            // followinf values just for example
            ud='somename';
            pw='****';
    }
    // function called from VF on button click
    public void buttonClick(){
        isValidCredential = UserCredentialcheck();
    }
    // your function
    public Boolean UserCredentialcheck(){
        If(UID!=ud && PWD!=pw)
            return false;
        else
            return true;
    }

}

Still the JS alert message is not working

Regards
Ashish
 
mritzimritzi
Use following VF code, everything should work just fine:
(added reRender attribute in commandbutton tag, assigned Ids to VF tags)
<apex:page id="pg" controller="TestClass2">
<apex:form id="form1">
    <apex:pageblock id="pb1">
        <apex:pageblockSection columns="4">
            UID: <apex:inputText value="{!UID}"/>
            PWD: <apex:inputText value="{!PWD}"/>
        </apex:pageblockSection>
        <apex:pageblockButtons >
            <apex:commandButton value="Check" id="btn" action="{!buttonClick}" oncomplete="show();" rerender="form1"/>
        </apex:pageblockButtons>
        <script type="text/javascript">
            function show(){
                var res = '{!isValidCredential}';
                if(res=='true'){
                    alert('Login Success');
                    }
            }
        </script>
    </apex:pageblock>
</apex:form>
</apex:page>

Mark this as Best Answer, if this solves your problem.