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
marafluxmaraflux 

Call a Controller method passing a parameter from VF page?



Hi

I need to be able to call a method in a Custom Controller and pass it a parameter. I could not find a way to do this.

So basically, I came up with a workaround using a CustomComponent, passing it an Attribute. Then my CustomController for the Component uses that Attribute to determine its output.

My question is, is this slow and/or heavy?! Is there a better/easier way to do this with VisualForce page and Apex?

Here is a sample use of how I have it set up now.

VF page:
<td class="whee"><c:TranslateComponent translate="{!myVaryingText}" /></td>

 

Component:

<c:TranslateComponent:
<apex:component controller="TranslateController">
    <apex:attribute name="Translate" type="String" description="Text to be translated." required="required" assignTo="{!Translate}" />
    {!Translated}
</apex:component>

Message Edited by maraflux on 07-31-2009 03:47 PM
Best Answer chosen by Admin (Salesforce Developers) 
snugglessnuggles

There is a better way (if i understand you correctly), using member variables.   i wrote you a little sample..

 

PAGE:

 

 <apex:page controller="PassingInfocontroller">
    <apex:form>
        <apex:inputTextarea value="{!preTranslatedValue}"/>
        <apex:commandButton action="{!doTranslation}" value="Translate" reRender="postTranslationOutputText"/>
    </apex:form>
    <apex:outputText id="postTranslationOutputText" value="{!postTranslatedValue}"></apex:outputText>
</apex:page>

 

CONTROLLER:

 

 public class PassingInfocontroller {
    public string preTranslatedValue{get;set;}
    public string postTranslatedValue{get;set;}
    public PassingInfocontroller(){
        preTranslatedValue = 'initialized';
    }
    public void doTranslation(){
        postTranslatedValue = preTranslatedValue + ' is now translated!';
    }
}

All Answers

snugglessnuggles

There is a better way (if i understand you correctly), using member variables.   i wrote you a little sample..

 

PAGE:

 

 <apex:page controller="PassingInfocontroller">
    <apex:form>
        <apex:inputTextarea value="{!preTranslatedValue}"/>
        <apex:commandButton action="{!doTranslation}" value="Translate" reRender="postTranslationOutputText"/>
    </apex:form>
    <apex:outputText id="postTranslationOutputText" value="{!postTranslatedValue}"></apex:outputText>
</apex:page>

 

CONTROLLER:

 

 public class PassingInfocontroller {
    public string preTranslatedValue{get;set;}
    public string postTranslatedValue{get;set;}
    public PassingInfocontroller(){
        preTranslatedValue = 'initialized';
    }
    public void doTranslation(){
        postTranslatedValue = preTranslatedValue + ' is now translated!';
    }
}

This was selected as the best answer
marafluxmaraflux

Hi

 

Thanks. That looks cleaner.

 

My problem is that I need to call my method (passing a parameter) many times from a single page, passing different values and I don't have any user action that initiates the action. Basically, I could perform all my calls server side, before the page is passed back to the client. I am translating words in my page.  

SuvraSuvra

Hi, I have one Controller function which takes String type var as its parameter. I have the list of alphabet, hyperlinked in VF page. Once i click of any of them, need to call the controller method with parameter as the alphabet i clicked.

Say, i am click on "A". So, need to call the method with param "A" from VF page.

 

How to accomplish this?? Please help..

 

 

Thanks in advance,

Suvra

snugglessnuggles
This one is a little trickier.  You have to use the <apex:param> tag to pass the value to the controller using the assignTo attribute.  look up "param" in the visualforce developers guide and let me know if that will work for you.
snugglessnuggles

the example in the doc doesn't use the assignTo, here is a basic examlpe:

 

<apex:page standardController="Contact">
<apex:outputLink value="http://google.com/search">
Search Google
<apex:param name="q" assignTo="{!customVariableWithGetterSetters}"/>
</apex:outputLink>
</apex:page>

 

Dea73Dea73

Hi i need some help.

 

Below is my VF page and controller.

The idea is to make a selection in the picklist and pass the id to the controller butit doesn't work.Any idea why????

 

<apex:page standardController="Central_Office__c" extensions="OfficeTotal" tabstyle="Central_Office_Info__c">
<apex:actionRegion >
<apex:pageBlock title="Central Office Information">
<apex:form >
Choose a Central Office:&nbsp;&nbsp; <apex:actionRegion><apex:selectList value="{!locationName}" size="1" id="COselection">
                         <apex:selectOptions value="{!items}"/>
                         </apex:selectList>&nbsp;
                         <apex:actionSupport event="onselect" action="{!selectCO}" rerender="thePageBlock">
                         <apex:param name="loc" value="{!id}" AssignTo="{!LocationName"/>
                         </apex:actionSupport>
                         </apex:actionRegion>
                        
</apex:form>
</apex:pageBlock>
</apex:actionRegion>
<apex:outputPanel id="thePageBlock" rendered="COSelection == true">
<apex:pageBlock title="Information"  >
<apex:pageBlockSection columns="2">
<apex:outputField value="{!Central_Office__c.Location_Details__c}" id="add"/>
<apex:outputLabel value="Number of DS1s" for="dstot">&nbsp;
<apex:outputText value="{!TotalDS1}" id="dstot"/>
</apex:outputLabel>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:outputpanel>
</apex:page>

 

 

public class OfficeTotal {
   
   
    public String locationName{get;set;}
    private final Central_Office__c centralOfficeObj;
    public double TotalDS1 {get;set;}
    public OfficeTotal(ApexPages.StandardController controller) {
    this.centralOfficeObj = (Central_Office__c)controller.getRecord();
    
     /*locationName = ApexPages.currentPage().getParameters().get('loc');*/
       
         getTotals();  
    }
   
public PageReference selectCO()
{
 locationName = System.currentPageReference().getParameters().get('loc');
 return null;
}   
   

public List<SelectOption> getItems() {
        List<SelectOption> lo = new List<SelectOption>();
        /* Add a null option to force the user to make a selection. */
        lo.add(new SelectOption('','- None -'));
        for(Location__c p : [SELECT Name
                             FROM Location__c
                             WHERE Location_Type__c = 'CO' and Status__c = 'Lit'
                             ORDER BY Name])
        lo.add(new SelectOption(p.Id, p.Name));
        return lo;
}  
   
/*Central_Office__c[] ds1 = */

public double getTotals(){
 TotalDS1 = 0;
for(Central_Office__c tot:[SELECT DS_Quantity__c
FROM Central_Office__c
WHERE Central_Office__c.Location__c =: LocationName])
{
if(tot.DS_Quantity__c != null)
{

TotalDS1 += tot.DS_Quantity__c;
}
}
return TotalDS1;
}
}