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
OdedHarnivOdedHarniv 

Generic action method

Hello

 

I have a situation which I hope to find a better solution that I currently have.

 

My case is that I have four command buttons on my VF page.

I want to have only one method on my controller which all the button actions will reference and the logic will differ based on the specific button that called it.

 

Is there any way to know which button called the method or I must write four different methods?

 

Thanks

 

Oded

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

Try this you did two mistakes

 

<apex:page standardController="Campaign" extensions="CampaignContentController" showheader="false" sidebar="false" standardStylesheets="false">
 
    <style type="text/css">
       .buttonStyle {
            width:270px;
            hight:100px;
            min-height:150px;
            font-family:"arial", arial, serif;   
            font-style:normal;
            font-size:40px;
            direction:rtl;
            color:black;        
        }
    </style>

	<script>
    	function buttonClick(whichButton)
        	{
              document.getElementById('j_id0:j_id2:j_id3:hdnWhichButton').value = whichButton;//Get id of hdn from your page
        	}
	</script>
        
    <apex:form >
  
        <apex:pageBlock mode="edit">
        
            <apex:panelGrid columns="1" id="topGrid">
                
                <apex:image id="theImage" value="{!contentURL}" width="550" height="750"/>      
            <!--    <apex:image id="theImage" value="{!$Resource.myResourceImage}" width="550" height="750"/>    -->    
                    
                <apex:panelGrid columns="2" id="bottomGrid">
                
                    <apex:commandButton onclick="butoonClick('Button1')" styleClass="buttonStyle" lang="he" dir="RTL" action="{!doAction}" value="בחירה 1" id="button1" immediate="false" tabindex="0">           
                    </apex:commandButton>
                    
                    <apex:commandButton onclick="butoonClick('Button2')" styleClass="buttonStyle" lang="he" dir="RTL" action="{!doAction}" value="בחירה 2" id="button2" immediate="false" tabindex="1">           
                    </apex:commandButton>
                        
                    <apex:commandButton onclick="butoonClick('Button3')" styleClass="buttonStyle" lang="he" dir="RTL" action="{!doAction}" value="בחירה 3" id="button3" immediate="false" tabindex="2">           
                    </apex:commandButton>
                    
                    <apex:commandButton onclick="butoonClick('Button4')" styleClass="buttonStyle" lang="he" dir="RTL" action="{!doAction}" value="בחירה 4" id="button4" immediate="false" tabindex="3">           
                    </apex:commandButton>           

			        <apex:inputHidden id="hdnWhichButton" value="{!whichButton}"/>
      
                </apex:panelGrid>   
    
            </apex:panelGrid>       
        </apex:pageBlock>
    </apex:form>        
</apex:page>

 You used wrong name of javascript , you used butonClick instead of buttonClick which is name of your method

 

Second you made immediate as true which does not allows properties to get bind with page values

All Answers

Platy ITPlaty IT

You could do this by including a param tag nested inside the command button and using that tag to update a variable in your controller.  Then in the controller, you can use that variable to determine which button called it.  Something like this:

<apex:commandButton action="{!SomeAction}" value="Add">
    <apex:param value="Add" assignTo="{!strButton}"/>
</apex:commandButton>
<apex:commandButton action="{!SomeAction}" value="Delete">
    <apex:param value="Delete" assignTo="{!strButton}"/>
</apex:commandButton>

 

OdedHarnivOdedHarniv

Thanks Jessie 

 

I tried what you suggested but the value does not seem to be saved in the controller.

 

this is my code:

 

<apex:commandButton styleClass="buttonDim" action="{!doAction}" value="button 1" id="button1" immediate="true" tabindex="0">		    
	<apex:param value="b1"  assignTo="{!whichButton}"/>
</apex:commandButton>

 

public Pagereference doAction(){
	
   System.debug(Logginglevel.DEBUG, 'doAction');    		
   System.debug(Logginglevel.DEBUG, 'Button: ' + whichButton);    		
		
   return null;	}
public String whichButton {get;set;}

 

Any advice?

 

Thanks

 

Oded

Shashikant SharmaShashikant Sharma

Try this, when you will click on button1 and button2 see the label.

 

public class doActionClass{
  
  public string whichButton{get;set;}
  
  public PageReference  doAction()
  {
     
     system.debug('******** whichButton : ' +whichButton);
     return null;
  }
  
  
  
  
  } 

 VFP

<apex:page controller="doActionClass">
  <script>
      function butonClick(whichButton)
          {
document.getElementById('j_id0:j_id2:hdnWhichButton').value = whichButton;//Get id of hdn from your page
          }
  </script>
  <apex:form>
      <apex:commandButton styleClass="buttonDim" onclick="butonClick('Button1')"  value="Button1" action="{!doAction}">          
      </apex:commandButton>
      
      <apex:commandButton styleClass="buttonDim" onclick="butonClick('Button2')" value="Button2" action="{!doAction}">          
      </apex:commandButton>
      
      <apex:inputHidden id="hdnWhichButton" value="{!whichButton}"/>
      
      <apex:outputLabel value="{!whichButton}"></apex:outputLabel>
  </apex:form>
</apex:page>

 

Platy ITPlaty IT

It may just be that the line where you defined whichButton is after your method, so the value isn't being set until after the method is run.  Moving that line up before doAction may fix it.  

 

But, I have had scenarios where assignTo doesn't work, usually when it's called inside a repeat but maybe in other situations as well.  You can switch that param to just be a named parameter and then access it from your method like this:

Page:

<apex:commandButton styleClass="buttonDim" action="{!doAction}" value="button 1" id="button1" immediate="true" tabindex="0">		    
	<apex:param value="b1"  name="whichButton"/>
</apex:commandButton>

 

Controller:

public Pagereference doAction(){
   whichButton = Apexpages.currentpage().getParameters().get('whichButton');
   System.debug(Logginglevel.DEBUG, 'doAction');    		
   System.debug(Logginglevel.DEBUG, 'Button: ' + whichButton);    		
		
   return null;	}

 

You do not need javascript to accomplish this.

OdedHarnivOdedHarniv

The example you posted before works but mine still does not, even after I moved the whichButton declaration to the top.

 

Maybe the fact that it is inside a grid causes it not to work?

 

Can I have four params with name="whichButton"?

I assume I can't and so I won't be able to know which button was pressed, right?

Shashikant SharmaShashikant Sharma

Hi OdedHarniv,

 

If you don't mind please try my solution if i did not understand your issue correctly please let me know.

Platy ITPlaty IT

It might be that assignTo doesn't work because it's in a grid, but try getting the value using getParameters, that always works for me.  And you can have that same param repeated, since it's nested inside of the commandButton, it's only being called when that specific button is being pressed.  

OdedHarnivOdedHarniv

I really appricate your help but it is not working.

 

I can't even see this parameter in the getParameter() method. this is what I see:

 

{cid=701A00000002OyJ, com.salesforce.visualforce.ViewStateCSRF=QjWudQOVeAg37VOg3rAi69s1qgt908CEMSIFldl7jwJlB5eSxOtq0sfOlZMt_V3ZV5le5px53mggyXnGX5550Op_mCMTt6GlUn9qANNwl.c0arll0Nd0Ua_53whES0xdXHPbAavIP47TySh3aylIdiCr6osnqypfDg7D.jglaYOX2XpA, 
core.apexpages.devmode.url=1, 
j_id0:j_id2=j_id0:j_id2, 
j_id0:j_id2:j_id3:button1= Button 1, 
sfdc.tabName=01rA0000000FOGo}

 

I'm starting to feel I'm overlooking some basic small thing.

 

 

Platy ITPlaty IT

It's always something small.  Do you have the name property defined in that param tag?  The name you define should appear in getParameters.  

 

Try Shashikant's solution if you haven't already; it should work, I just always prefer to avoid javascript when possible.  Instead of hard-coding the id of that hidden Input in the javascript, I'd recommend use $Component to access it, that way it will work even if you re-arrange the elements on the page.  Example here- http://www.salesforce.com/us/developer/docs/pages/Content/pages_access.htm

Shashikant SharmaShashikant Sharma

I also never prefer hard coded id's in javascript , just used to solve the original problem , definitly using $Component.controlId id better, just try my solution It will work I hae tested it before giving to you.

OdedHarnivOdedHarniv

I think I found the problem;

 

Shashikant , your example does work well, it works even when I put it in a grid.

But when I add a pageblock (like in my VF page) it stops working. So annoying...

 

Now I need to figure out how to overcome this, maybe I'll remove the pageblock.

 

Thank you both for the time you took to help...

 

Oded

Shashikant SharmaShashikant Sharma

Oded Harniv,

 

I tryed my solution with pageblick also it worked fine , I am sure you forgot to update the id of inputHidden when you added pageblock. Otherwise there is no case where it won't work. Thats why hard coded ids can be problematic and we should use $Component instead. But try with my solution it will work in all cases but be asure of id of inputHidden.

OdedHarnivOdedHarniv

I know I'm missing something but can't find what.

This is my code based on your example:

 

<apex:page controller="doActionClass">
  <script>
      function butonClick(whichButton)
          {
              document.getElementById('j_id0:j_id2:j_id3:hdnWhichButton').value = whichButton;
          }
  </script>
  <apex:form >
  	<apex:pageBlock mode="edit">
 
    	<apex:panelGrid columns="1" id="topGrid">
			<apex:panelGrid columns="2" id="Grid">

			      <apex:commandButton styleClass="buttonDim" onclick="butonClick('Button1')"  value="Button1" action="{!doAction}">          
			      </apex:commandButton>
			      
			      <apex:commandButton styleClass="buttonDim" onclick="butonClick('Button2')" value="Button2" action="{!doAction}">          
			      </apex:commandButton>
			      
			      <apex:inputHidden id="hdnWhichButton" value="{!whichButton}"/>
			      
			      <apex:outputLabel value="{!whichButton}"></apex:outputLabel>

			</apex:panelGrid>
		</apex:panelGrid>
  	</apex:pageBlock>
  </apex:form>
</apex:page>

 

Shashikant SharmaShashikant Sharma

Strange I just copy paste your page It worked for me, Check version of your VFP it is Salsforce.Com API 22 in my case. One more thing are you accessing this page from Sites as Guest Profile User or as in Internal User.

OdedHarnivOdedHarniv

Hi Shashikant 

 

Your code works fine now, but anything I try with the apex:param tag doesn't.

 

So I tried appyling your solution to my code but I can't get it to work.

I work as an Internal User.

 

If you have time can you take a look at it?

 

Many Thanks

 

P.S. my version was 20.0 and I changed it to 22.0.

 

<apex:page standardController="Campaign" extensions="CampaignContentController" showheader="false" sidebar="false" standardStylesheets="false">
 
    <style type="text/css">
       .buttonStyle {
            width:270px;
            hight:100px;
            min-height:150px;
            font-family:"arial", arial, serif;   
            font-style:normal;
            font-size:40px;
            direction:rtl;
            color:black;        
        }
    </style>

	<script>
    	function buttonClick(whichButton)
        	{
              document.getElementById('j_id0:j_id2:j_id3:hdnWhichButton').value = whichButton;//Get id of hdn from your page
        	}
	</script>
        
    <apex:form >
  
        <apex:pageBlock mode="edit">
        
            <apex:panelGrid columns="1" id="topGrid">
                
                <apex:image id="theImage" value="{!contentURL}" width="550" height="750"/>      
            <!--    <apex:image id="theImage" value="{!$Resource.myResourceImage}" width="550" height="750"/>    -->    
                    
                <apex:panelGrid columns="2" id="bottomGrid">
                
                    <apex:commandButton onclick="butonClick('Button1')" styleClass="buttonStyle" lang="he" dir="RTL" action="{!doAction}" value="בחירה 1" id="button1" immediate="true" tabindex="0">           
                    </apex:commandButton>
                    
                    <apex:commandButton onclick="butonClick('Button2')" styleClass="buttonStyle" lang="he" dir="RTL" action="{!doAction}" value="בחירה 2" id="button2" immediate="true" tabindex="1">           
                    </apex:commandButton>
                        
                    <apex:commandButton onclick="butonClick('Button3')" styleClass="buttonStyle" lang="he" dir="RTL" action="{!doAction}" value="בחירה 3" id="button3" immediate="true" tabindex="2">           
                    </apex:commandButton>
                    
                    <apex:commandButton onclick="butonClick('Button4')" styleClass="buttonStyle" lang="he" dir="RTL" action="{!doAction}" value="בחירה 4" id="button4" immediate="true" tabindex="3">           
                    </apex:commandButton>           

			        <apex:inputHidden id="hdnWhichButton" value="{!whichButton}"/>
      
                </apex:panelGrid>   
    
            </apex:panelGrid>       
        </apex:pageBlock>
    </apex:form>        
</apex:page>

 

public with sharing class CampaignContentController {

    /**
     *
     */
    public String whichButton {get; set;}
                                              
        
    public CampaignContentController(ApexPages.StandardController stdController){
        
        this.mCampaign = (Campaign)stdController.getRecord();
    }
    /**
     *
     */
    public PageReference  doAction(){
                          
        system.debug(Logginglevel.DEBUG, 'doAction: ' + Apexpages.currentpage().getParameters());           
        system.debug(Logginglevel.DEBUG, 'Button: ' + whichButton);         
        
        return null;
    }
}

 

Shashikant SharmaShashikant Sharma

Try this you did two mistakes

 

<apex:page standardController="Campaign" extensions="CampaignContentController" showheader="false" sidebar="false" standardStylesheets="false">
 
    <style type="text/css">
       .buttonStyle {
            width:270px;
            hight:100px;
            min-height:150px;
            font-family:"arial", arial, serif;   
            font-style:normal;
            font-size:40px;
            direction:rtl;
            color:black;        
        }
    </style>

	<script>
    	function buttonClick(whichButton)
        	{
              document.getElementById('j_id0:j_id2:j_id3:hdnWhichButton').value = whichButton;//Get id of hdn from your page
        	}
	</script>
        
    <apex:form >
  
        <apex:pageBlock mode="edit">
        
            <apex:panelGrid columns="1" id="topGrid">
                
                <apex:image id="theImage" value="{!contentURL}" width="550" height="750"/>      
            <!--    <apex:image id="theImage" value="{!$Resource.myResourceImage}" width="550" height="750"/>    -->    
                    
                <apex:panelGrid columns="2" id="bottomGrid">
                
                    <apex:commandButton onclick="butoonClick('Button1')" styleClass="buttonStyle" lang="he" dir="RTL" action="{!doAction}" value="בחירה 1" id="button1" immediate="false" tabindex="0">           
                    </apex:commandButton>
                    
                    <apex:commandButton onclick="butoonClick('Button2')" styleClass="buttonStyle" lang="he" dir="RTL" action="{!doAction}" value="בחירה 2" id="button2" immediate="false" tabindex="1">           
                    </apex:commandButton>
                        
                    <apex:commandButton onclick="butoonClick('Button3')" styleClass="buttonStyle" lang="he" dir="RTL" action="{!doAction}" value="בחירה 3" id="button3" immediate="false" tabindex="2">           
                    </apex:commandButton>
                    
                    <apex:commandButton onclick="butoonClick('Button4')" styleClass="buttonStyle" lang="he" dir="RTL" action="{!doAction}" value="בחירה 4" id="button4" immediate="false" tabindex="3">           
                    </apex:commandButton>           

			        <apex:inputHidden id="hdnWhichButton" value="{!whichButton}"/>
      
                </apex:panelGrid>   
    
            </apex:panelGrid>       
        </apex:pageBlock>
    </apex:form>        
</apex:page>

 You used wrong name of javascript , you used butonClick instead of buttonClick which is name of your method

 

Second you made immediate as true which does not allows properties to get bind with page values

This was selected as the best answer
OdedHarnivOdedHarniv

Hi

 

I was on the buton/button issue a sec after I hit the post But the immediate issue solved the problem.

 

I wish there was a way to do it without hard coding it but by now I'm fed up with this page :-)

 

If you ever find the cause I'll be glad to know, for now I will stick with your solution.

 

Thank you both very much.

 

Oded

Shashikant SharmaShashikant Sharma

Please let me know Which harcoding is bothering you , And please mark it as solution so that it could help others as well.

 

 

OdedHarnivOdedHarniv

This: j_id0:j_id2:j_id3 might change if I change the HTML