VISUALFORCE : Set () METHOD

salesforce visualforce set method

Visualforce : Set () Method 

In the previous Episode, we have discussed the visualforce Get () method with examples.
In this episode, we are gonna discuss visualforce another important method i.e. set() method. 

set() Method :
The set() method will take the value from the visualforce page and stores to apex variable name.

This is called a read/write operation on the variable.
Example 1 :
=============================================

   public void setAge(Interger age) 

   {

     this.age = age ;

   }

=============================================

In the above method, It will accept the Integer value of age and map it with another variable age which is defined somewhere in a class.

Let's consider another example of set() method.

Example 2 :
=============================================
public void setName(String name)
{
 this.name = name ;
}
================================================


In the above method, It will accept the string name and map it with another variable name which is defined somewhere in a class.
You will understand more about this in the next example which demonstrate setter method by passing the value and saving the value to apex variable.
Apex Class :
================================================
public class Example1
{
  public string name ; //#1
  public string getName() //This method will get name of type string
  {
    return name ;
  }
  
  public void setName(string name) //This method will accept input name of type    string and map it with #1 name
  {
    this.name = name ;
  }
}
================================================

Visualforce Page :
================================================
<apex:page controller="Example1">
  <apex:form>
   <apex:outputlabel>

      Enter Name 

   </apex:outputlabel>

    <apex:inputtext value="{!name}"/> //For dispalying input text 
     <apex:commandbutton value="click" reRender="one"/> //For button
      <apex:outputlabel id="one"> 

        your name is {!name}

     </apex:outputlabel>
    </apex:form>
   </apex:page>
================================================


In the above visualforce code, <apex:inputtext> tag provide input field to enter name and call {!name} method from controller Example1.

<apex:commandbutton> tag will provide button to submit name.

reRender tag will refresh the value and provide {!name} in the next line.

When you hit on visualforce page preview, You will see the output as 
Output :
================================================
salesforce visualforce set method

================================================

Now in input field enter and name and hit click.
================================================
salesforce visualforce set method
=================================================

Coool !! Now you can pass input value from the visualforce page and your apex method will get the value and then map it with string variable name.

We can also define setter and getter methods in a single line like this 
=================================================
public Integer {set; get;}
=================================================

In the next Episode, we are gonna discuss about how to call apex methods in visualforce page when you click on the button.

WOHOOO !! YOU HAVE JUST COMPLETED VISUALFORCE: GET () METHOD EPISODE
If you like this salesforcekid learning platform please let me know in the Comment section...Also, Share with your salesforce folks wish you 
Happy learning ☁️⚡️ (Learn. Help. Share.)


<< PREVIOUS                                                            NEXT >>

VISUALFORCE : Set () METHOD VISUALFORCE : Set () METHOD Reviewed by on Rating: 5

1 comment:

  1. The way analyse and preparing this content is awesome.just keep it u
    p

    ReplyDelete

HELP !! SHARE !! SUGGEST !!

Powered by Blogger.