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
Roshan TamrakarRoshan Tamrakar 

Getting commandbutton Parameter in controller

Hi Experts,
 
Though this question has already been asked by other, no definite answer has been made. Please help me.
 
To be straight, I have the following code:
 
Code:
<apex:page showHeader="false" controller="VehicleListControllerClass_2">
 <apex:form>
  <apex:pageBlock title="Vehicle List for {!personname}">
   <apex:pageBlockList value="{!vehicleList}" var="v">
    <apex:column value="{!v.VehicleName}"/>
    <apex:column value="{!v.PersonVehicle.Vehicle__r.Name}"/>
    <apex:column headerValue="Vehicle">
     <apex:outputLink value="/{!v.PersonVehicle.Vehicle__c}" target="_top">{!v.PersonVehicle.Vehicle__r.Name}</apex:outputLink>
    </apex:column>
    <apex:column>
     <apex:commandLink value="Click This Link" action="{!dosomething}" >
      <apex:param name="myparm" value="Link abcd"/>
     </apex:commandLink>
    </apex:column>
    <apex:column>
    <apex:commandButton value="Click This Button" action="{!dosomething}" styleClass="btn">
     <apex:param assignTo="{!MP}" name="myparm1" value="abcd"/>
     <apex:param name="myparm2" value="Button abcd"/>
    </apex:commandButton>
    </apex:column>
   </apex:pageBlockList>
  </apex:pageBlock>
 </apex:form>
</apex:page>


public class VehicleListControllerClass_2{

 public String getPersonName(){
  ID pId = System.currentPageReference().getParameters().get('id');
  String name = [Select Name from Person__c where id=:pId].Name;
  return name;
 }

 public class Vehicles{
  Person_Vehicle__c pv;
  //Constructor
  public Vehicles(Person_Vehicle__c pvin) {pv = pvin;}

  public Person_Vehicle__c getPersonVehicle() {return pv;}

  public String getVehicleName(){

   if(pv.Vehicle__r.Name == 'Maruti 800') return 'It is Maruti 800 yar';
   else return pv.Vehicle__r.Name;
  }

  Integer curYear = system.now().year();
  return (curYear - (Integer)pv.Purchase_Year__c);
  }
 }

 List<Vehicles> pvlist = new List<Vehicles>();

 public List<Vehicles> getVehicleList(){
  ID pId = System.currentPageReference().getParameters().get('id');

  pvlist.clear();
  for(Person_Vehicle__c pvv : [Select p.Person__r.Name, p.Vehicle__c,
  p.Vehicle__r.Name, p.Purchase_Year__c from Person_Vehicle__c p where p.Person__c=:pId]){
   pvlist.add(new Vehicles(pvv));
  }

  return pvlist;
 }

 String p;
 public String getMP(){
  return p;
 }

 public void setMP(String s){
  p=s;
 }
 
 public PageReference dosomething(){
  String s = System.CurrentPageReference().getParameters().get('myparm');
  String s1 = System.CurrentPageReference().getParameters().get('myparm1');
  String s2 = System.CurrentPageReference().getParameters().get('myparm2');
  System.debug(s);
  System.debug(s1);
  System.debug(s2);
  return null;
 }

}

When I click on the link 'Click This Link', String 's' returns 'Link abcd' which is correct.
 
But when I click on the link 'Click This Button', both Strings 's1' and 's2' returns null.
 
How could I get the param value in controller in case of 'commandButton'?
Please note that I Must use commandButton and I need to pass more than one parameters.
 
Also, I would like to be able to create a new Case (in another visualforce page) with few default values by clicking a commandButton. The page should open as if user clicked on 'New' button and ready to Save. Please note that the page will not be associated with 'Case' controller.
 
How could I achieve this functionality?
 
Thanks,
-Roshan
Best Answer chosen by Admin (Salesforce Developers) 
Volker_factory42Volker_factory42

I found the solution:

 

 

Class: ... //Set variable for parameter coming from VF-Page String p; public String getMP(){ return p; } public void setMP(String s){ p=s; } public PageReference ViewPage() { PageReference pageRef= new PageReference('/apex/VF-Page?id='+ p ); return pageRef; } ... Testmethod: public static testMethod void testController() { //Declare Variables Date todaydate = System.today(); Classname ext1 = new Classname(new Object__c(Start__c = todaydate,End__c = todaydate)); Test.setCurrentPageReference(new PageReference('Page.VF-Page')); ext1.setMP('set'); system.assertequals('set',ext1.getMP()); PageReference p1 =ext1.ViewPage(); System.assert(p1 != null); }

 

 

 

All Answers

Ron HessRon Hess
can you describe the relation between PersonVehicle and Vehicle__c
is that a simple lookup
Roshan TamrakarRoshan Tamrakar

Person_Vehicle__c has a 'Lookup Relationship' with Vehicle__c (not 'Master_Detail Relationship').

I have mainly 3 custom objects: Person__c, Person_Vehicle__c and Vehicle_List__c.

Person__c is the Master and Person_Vehicle__c is its child. A person can have many vehicles which are recorded in Person_Vehicle__c.

So Person_Vehicle__c has two major fields, one is 'Person' (Data Type=Master-Detail) and the other is 'Vehicle' (Data Type=Lookup).

Person

Id         Name

P001    Tirtha

P002    Roshan

 

Vehicle

Id         Name

V001   Maruti 800

V002   Alto

 

Person Vehicle

Id                     Person (Master-Detail)             Vehicle (Lookup)

PV001             P001                                        V001

PV002             P001                                        V002

---

PV003             P002                                        V002

So here 'Tirtha' has two vehicles and 'Roshan' has only one vehicle.

Hope this is clear now.

Thanks,
-Roshan

Ron HessRon Hess
i believe the commandButton should send those paramaters, it may be a bug that has been fixed in summer 08, when a button is inside a table.
we can check after friday in sandbox

The other requiement will need a visualfoce page with case custom controller, and extension to read the paramaters and assign them to the new case record ( fetched from the standardcontroller ) , then loaded with the values fetched via getParameters() . i believe this will then look like the user has clicked new and entered some fields of data.
Volker_factory42Volker_factory42
Hi Ron,

do you know if there's is a solution to use parameters and commandbuttons yet? Is this bug maybe fixed in Srping09 release?


wesnoltewesnolte

*Bump*

 

I am also having an issue where using a commandButton with params returns null values, but simply changing to commandLink resolves it.

 

Is this a known bug?

Volker_factory42Volker_factory42

I found a good solution to solve this problem. I'm using a commandlink instead of a commandbutton, but with button stylesheet:

 

... styleClass="btn" style="padding:2px 5px 2px 5px; text-decoration:none;" ...

 

Now I can use target, parameters and so on, and in VF it looks like a button...

 

 

Message Edited by Volker_SF42 on 01-28-2009 03:45 AM
wesnoltewesnolte

Nice workaround!

 

I hope this gets fixed tho.

Volker_factory42Volker_factory42

I'm trying to write a testclass for getting this parameter liked mentioned above.

 

 

String p; public String getMP(){ return p; }

 

public void setMP(String s){ p=s; }

 

 

But I don't find a way to test it 100%.

 

This is what tried:

 

 

 

ApexPages.currentPage().getParameters().put('MP','set');

 

Does someone have an idea?

 

Volker_factory42Volker_factory42

I found the solution:

 

 

Class: ... //Set variable for parameter coming from VF-Page String p; public String getMP(){ return p; } public void setMP(String s){ p=s; } public PageReference ViewPage() { PageReference pageRef= new PageReference('/apex/VF-Page?id='+ p ); return pageRef; } ... Testmethod: public static testMethod void testController() { //Declare Variables Date todaydate = System.today(); Classname ext1 = new Classname(new Object__c(Start__c = todaydate,End__c = todaydate)); Test.setCurrentPageReference(new PageReference('Page.VF-Page')); ext1.setMP('set'); system.assertequals('set',ext1.getMP()); PageReference p1 =ext1.ViewPage(); System.assert(p1 != null); }

 

 

 

This was selected as the best answer
Big EarsBig Ears

Volker,

 

Thanks for this workaround. It's something I've been struggling with for some time.

 

Andy 

hector.asp2hector.asp2

<apex:page showHeader="false" sidebar="false"  controller="clsBal">

 

 <apex:sectionHeader title="XyZ" />

    <apex:form>

    <apex:pageBlock title="XYZ  Manager" mode="Read">

      <apex:pageBlockTable var="DLProducts"  value="{!Product}" >

       <apex:column headerValue="Products" value="{! DLProducts.name}"/>

        <apex:column>

                <apex:commandLink value="Buy"  action="{! dosomething }">

                        <apex:param   id="PName" value="arun test"/>

               </apex:commandLink>

         </apex:column>         

      </apex:pageBlockTable>

    </apex:pageBlock>

    

 </apex:form>

</apex:page>

 

public PageReference dosomething ()

   {

       Product_Name=System.CurrentPageReference().getParameters().get('PName');

         System.debug('Product_Name:' + Product_Name);

         return null;

}

 

 

 

hi folks , 

i am not able to catch  "PName" param  :smileysad:  , please help me or suggest any alternative...


 

Volker_factory42Volker_factory42

Hi,

 

try to use a getter / setter instead of getting this parameter from URL

String p; public String getPName(){ return p; } public void setPName(String s){ p=s; } public PageReference dosomething () { System.debug('Product_Name:' + p); return null; }

 


 

 

UmapenUmapen

Thanks for this post.

I am using CommandButton  disabled="{!MyObj.Id ==Null}" and wanted to user apex:param

 

apex:param is not working with commandButton so changed to commandLink StyleClass="btn" as suggested.

 

How can I use disabled on commandlink?

 

Appreciate any suggestions?

 

Vivek ViswanathanVivek Viswanathan

you can also use this workaround, which is using a partial page refresh.

 

you can also use this workaround, which is using a partial page refresh.

<apex:page>
<apex:panelGrid columns="2" id="grid">
<apex:outputText value="param key: "/>
<apex:outputText value="{!$CurrentPageReference.parameters.key}"/>
</apex:panelGrid>
<apex:form>
<apex:commandButton value="click me!" rerender="grid"><apex:param name="key" value="value"/></apex:commandButton>
<apex:commandButton value="clear" rerender="grid"><apex:param name="key" value=""/></apex:commandButton>
</apex:form>
</apex:page>

 

Regards

 

Vivek Viswanathan

Developer Support Engineer

Salesforce.com

suresh.csksuresh.csk

Hi.

 

I'm also having the same issue

Please help where I am wrong.

 

VF

 

<apex:commandButton value="{!cartValue}" action="{!dispParam}">
<apex:param name="param" value="{!pd.productobj.ID}"  assignTo="{!paramValue}"/>
</apex:commandButton>

 

Apex

 

public String getparamValue(){
        return p;
    }

    public void setparamValue(String s){
        p=s;
    }
public PageReference dispParam(){
 PageReference pageRef= new PageReference('/apex/SiteLogin?id='+p );
 return pageRef;

}

 

I am getting p as null.

Thanks

 

suresh.csksuresh.csk

Hi

 

I am very sorry to recommence the same issue since thread confused me.

But I got fixed.

 

1.Got the parameters from VF using the [ get set ] methods

2.Chaged the command button to command Link and made some styling.

 

The complete clear code is

 

VF

 

<apex:page controller="MyTestController" sidebar="false" >
<apex:form >
<apex:pageBlock >
<apex:commandLink value="Click" action="{!dosomething}" styleClass="btn" style="padding:2px 5px 2px 5px; text-decoration:none;"  >
<apex:param assignTo="{!MP}" name="myparm1" value="123"/>
</apex:commandLink>
</apex:pageBlock>
</apex:form>

 

 

Apex

 

public class MyTestController {
String p;
    public String getMP(){
        return p;
    }

    public void setMP(String s){
        p=s;
    }


public PageReference doSomething(){
System.debug('Product_Name:' + p);
return null;
}
}

 

Thanks a lot for Volker.