{"id":2643,"date":"2012-01-15T01:00:38","date_gmt":"2012-01-14T19:30:38","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=2643"},"modified":"2012-01-15T01:00:38","modified_gmt":"2012-01-14T19:30:38","slug":"passing-parameter-in-actionfunction-in-visualforce","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/passing-parameter-in-actionfunction-in-visualforce\/","title":{"rendered":"Passing multiple Parameters in ActionFunction in Visualforce"},"content":{"rendered":"<p style=\"text-align: justify;\">Calling Apex Method from the Visualforce page is the one of the most required functionality in application development in Salesforce. <strong>&lt;apex:actionFunction&gt;<\/strong> is one of the method used to achieve this functionality.<\/p>\n<p style=\"text-align: justify;\">Most often we need to supply the arguments in <strong>&lt;apex:actionFunction&gt;<\/strong> and in this article I will demonstrate the way in which we can pass one or more than one parameter.<br \/>\nThe output of the example will be like below screen.<\/p>\n<figure id=\"attachment_2647\" aria-describedby=\"caption-attachment-2647\" style=\"width: 356px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/01\/Passing-Parameter-in-ActionFunction-in-Visualforce.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2647\" title=\"Passing Parameter in ActionFunction in Visualforce\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/01\/Passing-Parameter-in-ActionFunction-in-Visualforce.png?resize=356%2C176&#038;ssl=1\" alt=\"Passing Parameter in ActionFunction in Visualforce\" width=\"356\" height=\"176\" \/><\/a><figcaption id=\"caption-attachment-2647\" class=\"wp-caption-text\">Passing Parameter in ActionFunction in Visualforce<\/figcaption><\/figure>\n<p>Create Apex class with following code:<!--more--><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npublic with sharing class PassParameterActionFunction {\n    public String val{get;set;}\n    public String enteredText1{get;set;}\n    public String enteredText2{get;set;}\n\n    public void echoVal()\n    {\n     val = 'You have entered : 1 - '+enteredText1+' 2 -'+enteredText2;\n    }\n}\n<\/pre>\n<p>In above code, the variables &#8220;<strong>enteredText1<\/strong>&#8220;\u009d and &#8220;<strong>enteredText2<\/strong>&#8220;\u009d will be the parameters supplied by the javascript and variable &#8220;<strong>val<\/strong>&#8220;\u009d will display the concatenated result.<br \/>\nNow create the Visualforce page with code:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;apex:page controller=&quot;PassParameterActionFunction&quot;&gt;\n&lt;style type=&quot;text\/css&quot;&gt;\n.pointer\n{\n    cursor:pointer;\n    border:1px solid #ccc;\n    padding:5px;\n}\n&lt;\/style&gt;\n&lt;apex:form id=&quot;frm&quot;&gt;\n&lt;apex:outputPanel id=&quot;resultPanel&quot;&gt;\n&lt;apex:actionStatus startText=&quot;requesting...&quot; stopText=&quot;&quot; id=&quot;myStatus&quot; \/&gt;\n&lt;br \/&gt;\n&lt;b&gt;&lt;apex:outputLabel value=&quot;{!val}&quot; \/&gt;&lt;\/b&gt;\n&lt;\/apex:outputPanel&gt;\n&lt;br \/&gt;\nEnter Value 1 :\n&lt;apex:inputText id=&quot;txt1&quot; \/&gt;\n&lt;br \/&gt;\nEnter Value 2 :\n&lt;apex:inputText id=&quot;txt2&quot; \/&gt;\n&lt;br \/&gt;\n&lt;br \/&gt;\n&lt;br \/&gt;\n&lt;span class=&quot;pointer&quot; onclick=&quot;callActionMethod()&quot;&gt; Click Me !!! &lt;\/span&gt;\n\n&lt;apex:actionFunction name=&quot;echo&quot; action=&quot;{!echoVal}&quot; reRender=&quot;resultPanel&quot; status=&quot;myStatus&quot;&gt;\n&lt;apex:param name=&quot;firstParam&quot; assignTo=&quot;{!enteredText1}&quot; value=&quot;&quot; \/&gt;\n&lt;apex:param name=&quot;secondParam&quot; assignTo=&quot;{!enteredText2}&quot; value=&quot;&quot; \/&gt;\n&lt;\/apex:actionFunction&gt;\n&lt;\/apex:form&gt;\n&lt;script type=&quot;text\/javascript&quot;&gt;\nfunction callActionMethod()\n{\n var txtVal1 = document.getElementById(&quot;{!$Component.frm.txt1}&quot;).value;\n var txtVal2 = document.getElementById(&quot;{!$Component.frm.txt2}&quot;).value;\n \/*Below Method is generated by &quot;apex:actionFunction&quot; which will call Apex Method &quot;echoVal&quot; *\/\n echo(txtVal1,txtVal2);\n}\n&lt;\/script&gt;\n\n&lt;\/apex:page&gt;\n\n<\/pre>\n<p style=\"text-align: justify;\">The below code snippet is used to define the &#8220;<strong>actionFunction<\/strong>&#8220;\u009d in visual force page.<br \/>\nTo supply the parameter, we have to use &#8220;<strong>apex:param<\/strong>&#8220;\u009d tag. Attribute &#8220;<strong>assignTo<\/strong>&#8220;\u009d will assign the parameter to variable name specified in Apex code. Here we have assigned the value to variable &#8220;<strong>enteredText1<\/strong>&#8220;\u009d and &#8220;<strong>enteredText2<\/strong>&#8220;\u009d.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;apex:actionFunction name=&quot;echo&quot; action=&quot;{!echoVal}&quot; reRender=&quot;resultPanel&quot; status=&quot;myStatus&quot;&gt;\n&lt;apex:param name=&quot;firstParam&quot; assignTo=&quot;{!enteredText1}&quot; value=&quot;&quot; \/&gt;\n&lt;apex:param name=&quot;secondParam&quot; assignTo=&quot;{!enteredText2}&quot; value=&quot;&quot; \/&gt;\n&lt;\/apex:actionFunction&gt;\n<\/pre>\n<p>The resulting JavaScript function created by the visualforce will be &#8220;<strong>echo<\/strong>&#8220;\u009d because we have set that name for the &#8220;<strong>apex:actionFunction<\/strong>&#8220;\u009d.<br \/>\nAttribute &#8220;<strong>action<\/strong>&#8220;\u009d will call the method specified on Apex class and &#8220;<strong>status<\/strong>&#8220;\u009d will show the Ajax request status.<br \/>\nBelow JavaScript method is used to call the generated method by &#8220;<strong>apex:actionFunction<\/strong>&#8220;\u009d.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nfunction callActionMethod()\n{\n var txtVal1 = document.getElementById(&quot;{!$Component.frm.txt1}&quot;).value;\n var txtVal2 = document.getElementById(&quot;{!$Component.frm.txt2}&quot;).value;\n \/*Below Method is generated by &quot;apex:actionFunction&quot; which will call Apex Method &quot;echoVal&quot; *\/\n echo(txtVal1,txtVal2);\n}\n\n<\/pre>\n<p>As you can see that we have called the method &#8220;<strong>echo<\/strong>&#8221; with two arguments, because in &#8220;<strong>apex:actionFunction<\/strong>&#8221; we have specified the parameters for the method.<\/p>\n<hr \/>\n<p><strong>Method 2:<\/strong><\/p>\n<p>In this method, instead of creating two temporary variable in Apex page and assigning it using\u00a0attribute &#8220;<strong>assignTo<\/strong>&#8221;\u00a0of we can directly get the value in Apex code by something like using<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nApexpages.currentPage().getParameters().get('paramName');\n<\/pre>\n<p>So the resultant <strong>Apex code<\/strong> will be:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic with sharing class PassParameterActionFunction {\n    public String val{get;set;}\n\n    public void echoVal()\n    {\n     val = 'You have entered : 1 - '+Apexpages.currentPage().getParameters().get('firstParam')+' 2 -'+Apexpages.currentPage().getParameters().get('secondParam');\n    }\n}\n<\/pre>\n<p><strong>Visualforce code:<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;apex:page controller=&quot;PassParameterActionFunction&quot;&gt;\n&lt;style type=&quot;text\/css&quot;&gt;\n.pointer\n{\n    cursor:pointer;\n    border:1px solid #ccc;\n    padding:5px;\n}\n&lt;\/style&gt;\n&lt;apex:form id=&quot;frm&quot;&gt;\n&lt;apex:outputPanel id=&quot;resultPanel&quot;&gt;\n&lt;apex:actionStatus startText=&quot;requesting...&quot; stopText=&quot;&quot; id=&quot;myStatus&quot; \/&gt;\n&lt;br \/&gt;\n&lt;b&gt;&lt;apex:outputLabel value=&quot;{!val}&quot; \/&gt;&lt;\/b&gt;\n&lt;\/apex:outputPanel&gt;\n&lt;br \/&gt;\nEnter Value 1 :\n&lt;apex:inputText id=&quot;txt1&quot; \/&gt;\n&lt;br \/&gt;\nEnter Value 2 :\n&lt;apex:inputText id=&quot;txt2&quot; \/&gt;\n&lt;br \/&gt;\n&lt;br \/&gt;\n&lt;br \/&gt;\n&lt;span class=&quot;pointer&quot; onclick=&quot;callActionMethod()&quot;&gt; Click Me !!! &lt;\/span&gt;\n\n&lt;apex:actionFunction name=&quot;echo&quot; action=&quot;{!echoVal}&quot; reRender=&quot;resultPanel&quot; status=&quot;myStatus&quot;&gt;\n&lt;apex:param name=&quot;firstParam&quot;  value=&quot;&quot; \/&gt;\n&lt;apex:param name=&quot;secondParam&quot;  value=&quot;&quot; \/&gt;\n&lt;\/apex:actionFunction&gt;\n&lt;\/apex:form&gt;\n&lt;script type=&quot;text\/javascript&quot;&gt;\nfunction callActionMethod()\n{\n var txtVal1 = document.getElementById(&quot;{!$Component.frm.txt1}&quot;).value;\n var txtVal2 = document.getElementById(&quot;{!$Component.frm.txt2}&quot;).value;\n \/*Below Method is generated by &quot;apex:actionFunction&quot; which will call Apex Method &quot;echoVal&quot; *\/\n echo(txtVal1,txtVal2);\n}\n&lt;\/script&gt;\n\n&lt;\/apex:page&gt;\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Example and Source code of  multiple Parameters  Parameter in ActionFunction in Visualforce &#8211; Salesforce<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[9,18],"tags":[331,336],"class_list":["post-2643","post","type-post","status-publish","format-standard","hentry","category-salesforce","category-visualforce","tag-salesforce","tag-visualforce"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1080,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/one-visualforce-page-as-an-iframe-in-another-visualforce-page-salesforce-com\/","url_meta":{"origin":2643,"position":0},"title":"One visualforce page as an iframe in another visualforce page &#8211; Salesforce.com","author":"Jitendra","date":"September 15, 2010","format":false,"excerpt":"Example to include one visualforce page into another visual force with the help of iframe and getting back the request parameter in visual force page.","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2501,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/very-useful-tips-and-tricks-of-the-apex-salesforce-interview-questions-part-4\/","url_meta":{"origin":2643,"position":1},"title":"Latest Salesforce Interview Questions &#8211; Part 4 &#8211; Related to Dynamic Apex","author":"Jitendra","date":"November 27, 2011","format":false,"excerpt":"Most Frequently Asked interview questions of Apex, Dynamic Apex, SOSL, Visualforce, SOQL in Salesforce.com SFDC","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2470,"url":"https:\/\/www.jitendrazaa.com\/blog\/webtech\/salesforce-tutorial-create-simple-ajax-based-visualforce-page\/","url_meta":{"origin":2643,"position":2},"title":"Salesforce Tutorial &#8211; Create Simple Ajax based Visualforce page","author":"Jitendra","date":"October 17, 2011","format":false,"excerpt":"Salesforce Tutorial - Step by step tutorial to create AJAX based application in visualforce page with Apex class","rel":"","context":"In &quot;Web Technology&quot;","block_context":{"text":"Web Technology","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/webtech\/"},"img":{"alt_text":"Simple AJAX demo in salesforce using visualforce","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/10\/Simple-AJAX-demo-in-salesforce-using-visualforce.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":28,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-interview-questions\/","url_meta":{"origin":2643,"position":3},"title":"Salesforce Interview Questions &#8211; Part 1","author":"Jitendra","date":"May 6, 2010","format":false,"excerpt":"Set of most often asked questions on the salesforce.com developement","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":4102,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-faq-part-20-lightning-questions\/","url_meta":{"origin":2643,"position":4},"title":"Salesforce interview question related to Lightning framework &#8211; Part 20","author":"Jitendra","date":"February 4, 2015","format":false,"excerpt":"Salesforce interview questions for Salesforce developers and admin , mostly related to newly released Salesforce Lightning components and applications","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":5662,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/send-visualforce-as-an-email-attachment-from-apex-trigger-alternate-design\/","url_meta":{"origin":2643,"position":5},"title":"Send Visualforce as an email attachment from Apex Trigger &#8211; Alternate design","author":"Jitendra","date":"August 27, 2016","format":false,"excerpt":"Process Builder & InvocableMethod- Alternative to send Visualforce as an email attachment from Apex Trigger","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Send Visualforce as an email attachment from Apex Trigger","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/08\/Send-Visualforce-as-an-email-attachment-from-Apex-Trigger.jpg?fit=922%2C369&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/08\/Send-Visualforce-as-an-email-attachment-from-Apex-Trigger.jpg?fit=922%2C369&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/08\/Send-Visualforce-as-an-email-attachment-from-Apex-Trigger.jpg?fit=922%2C369&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/08\/Send-Visualforce-as-an-email-attachment-from-Apex-Trigger.jpg?fit=922%2C369&ssl=1&resize=700%2C400 2x"},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2643","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=2643"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2643\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=2643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=2643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=2643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}