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
Michael3.BrownMichael3.Brown 

I Need Help Passing a Parameter to an Apex Page and Calling a Java Function

Hello, I am working on a test involving the passing of variables. Basically, a user is on a splash page where they input some values in an HTML form, which are then passed to my Apex page to be used as parameters in Java Functions.

 

I am new to this, so I've created a simple test. I set up a Splash Page where a user uses a dropdown to select the make of a car. Based on their selection, this parameter is passed to my Java-Interacting Apex Page.  So far, I got this part working as the parameter of my HTML form was in my URL:

http://a.salesforce.com/apex/CarSales?carmake=Ford

 

Now, this is where I"m getting stuck. I want to use my apex Page to send this variable to one of my Apex Classes where it will call a Java Function.

 

Here is the Apex Code I have from the web interface. carmake represents the variable that was passed from the previous HTML form.

<apex:page controller="Cars">
{!getMake(carmake)}
</apex:page>

 Here is my Cars Class

public class Cars
{

String make;
    
    public String getMake(String c)
    {
        if (c == 'Ford')
        {
             make = 'You have selected Ford';
        }

        else
        {
             make = 'You have not selected Ford';
        }

        return make;
    }
}

 

 

However, the variable carmake does not seem to be recognized, as I am getting the error:

Error: Unknown property 'Cars.carmake'

If anyone could provide any insight, I would really appreciate it.

Thank you,

Mike

Best Answer chosen by Admin (Salesforce Developers) 
DodiDodi

I am not sure if you re getting the parameter from the url string.....if so you can do something like this to get the value of the param(in my case I am getting the account name from the query string.

 

 sfAccount = ApexPages.currentPage().getParameters().get('sfAccount');

 

If you are trying to get them from a selected item on the page you need getters and setters in your controler class. For instnace if I have a drop down of months, I need a method to provide the values...

 

public List<SelectOption> getMonths() {
            List<SelectOption> months = new List<SelectOption>();
                
             months.add(new SelectOption('NA','--'));
             months.add(new SelectOption('January','January'));
             months.add(new SelectOption('February','February'));
             months.add(new SelectOption('March','March'));
             months.add(new SelectOption('April','April'));
             months.add(new SelectOption('May','May'));
             months.add(new SelectOption('June','June'));
             months.add(new SelectOption('July','July'));
             months.add(new SelectOption('August','August'));
             months.add(new SelectOption('September','September'));
             months.add(new SelectOption('October','October'));
             months.add(new SelectOption('November','November'));
             months.add(new SelectOption('December','December'));
             
             return months;
            
        }

 

I will also need getters and setters like so.....

 

public String getSelectedMonth() {
            return selectedMonth;
       }

 

Also attaching the visual force markup code to do this.

 
             <apex:selectList value="{!selectedMonth}" size="1" title="Month" >
                <apex:selectOptions value="{!months}"/>

 

Hope this helps,

 

Dodi

All Answers

DodiDodi

I am not sure if you re getting the parameter from the url string.....if so you can do something like this to get the value of the param(in my case I am getting the account name from the query string.

 

 sfAccount = ApexPages.currentPage().getParameters().get('sfAccount');

 

If you are trying to get them from a selected item on the page you need getters and setters in your controler class. For instnace if I have a drop down of months, I need a method to provide the values...

 

public List<SelectOption> getMonths() {
            List<SelectOption> months = new List<SelectOption>();
                
             months.add(new SelectOption('NA','--'));
             months.add(new SelectOption('January','January'));
             months.add(new SelectOption('February','February'));
             months.add(new SelectOption('March','March'));
             months.add(new SelectOption('April','April'));
             months.add(new SelectOption('May','May'));
             months.add(new SelectOption('June','June'));
             months.add(new SelectOption('July','July'));
             months.add(new SelectOption('August','August'));
             months.add(new SelectOption('September','September'));
             months.add(new SelectOption('October','October'));
             months.add(new SelectOption('November','November'));
             months.add(new SelectOption('December','December'));
             
             return months;
            
        }

 

I will also need getters and setters like so.....

 

public String getSelectedMonth() {
            return selectedMonth;
       }

 

Also attaching the visual force markup code to do this.

 
             <apex:selectList value="{!selectedMonth}" size="1" title="Month" >
                <apex:selectOptions value="{!months}"/>

 

Hope this helps,

 

Dodi

This was selected as the best answer
Michael3.BrownMichael3.Brown

Hi Dodi,

 

Where would I insert the code: sfAccount = ApexPages.currentPage().getParameters().get('sfAcc​ount'). I'm having getting it to work.

 

Also, I went to try out the sample code you provided regarding the Month dropdown, but I am getting an error:

Read only property '[Cars].selectedMonth' 

 and I'm not sure what that means.

 

Thanks,

Mike

Imran MohammedImran Mohammed

Insert it before the line in method or constructor where you will be about to use sfAccount variable.

Michael3.BrownMichael3.Brown

Thanks Dodi! While I struggled to get the Months List working, I was able to properly pass and receive my parameter based upon sfAccount = ApexPages.currentPage().getParameters().get('sfAcc​ount');

 

 

DodiDodi

No prob, glad to help