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

javascript in salesforce



I am new to javascript in issue is i need to write a javascript when i click on a button some data  should store in one text field.For example i created one custom button in detail page in Account object.when i click on this button i want to store some data in Billing City field.


any one guide me....




There are two aproaches:

1) If you are using apex:commandButton, then write an action function in the controller and do your necessary DML operation.

2) If you have a simple html button then write an onclick function and pass your desired data and store it throgh salesforce ajax toolkit function. For more information using this toolkit visit here:





Shashikant SharmaShashikant Sharma

You should use either apex:actionFunction or apex:actionSupport, below are explanation with example, let me know if any issue in it.

1)actionFunction : provides support for invoking controller action methods directly from JavaScript code using an AJAXrequest


Used when we need to perform similar action on varioud events. Een though you can use it in place of actionSupport as well where only event is related to only one control.


Example :

actionFunction : provides support for invoking controller action methods directly from JavaScript code using an AJAXrequest

Example :
<!-- Page: -->
<apex:page controller="exampleCon">
<!-- Define the JavaScript function sayHello-->
<apex:actionFunction name="sayHello" action="{!sayHello}" rerender="out"
<apex:outputPanel id="out">
<apex:outputText value="Hello "/>
<apex:actionStatus startText="requesting..." id="myStatus">
<apex:facet name="stop">{!username}</apex:facet>
<!-- Call the sayHello JavaScript function using a script element-->
<p><apex:outputText value="Clicked? {!state}" id="showstate" /></p>
<!-- Add the onclick event listener to a panel. When clicked, the panel triggers
the methodOneInJavascript actionFunction with a param -->
<apex:outputPanel onclick="methodOneInJavascript('Yes!')" styleClass="btn">
Click Me
<apex:actionFunction action="{!methodOne}" name="methodOneInJavascript"
<apex:param name="firstParam" assignTo="{!state}" value="" />

/*** Controller ***/
public class exampleCon {
String uname;
public String getUsername() {
return uname;
public PageReference sayHello() {
uname = UserInfo.getName();
return null;
public void setState(String n) {
state = n;
public String getState() {
return state;
public PageReference methodOne() {
return null;
private String state = 'no';


.2)ActionSupport : A component that adds AJAX support to another component, allowing the component to be refreshed asynchronously by theserver when a particular event occurs, such as a button click or mouseover.


Used when we want to perform an action on a perticular eventof any control like onchange of any text box or picklist.

Example : 


<!-- Page: -->
<apex:page controller="exampleCon">
<apex:outputpanel id="counter">
<apex:outputText value="Click Me!: {!count}"/>
<apex:actionSupport event="onclick"
rerender="counter" status="counterStatus"/>
<apex:actionStatus id="counterStatus"
startText=" (incrementing...)"
stopText=" (done)"/>

/*** Controller: ***/
public class exampleCon {
Integer count = 0;
public PageReference incrementCounter() {
return null;
public Integer getCount() {
return count;

Hi Swapna,


I hope you are asking on the standard detail page. If so for your custom button select "Execute Javascript" behaviour. Content source = "Onclick Javascript"


you're saying some data to be captured. I donno wat that some data is? I hope It may be static value or the value on other field.


You can write the javascript code to update the field with that value. (please follow Ajax toolkit to do this)


If this is not the case I hope the above solutions work for you.






Hi Swapna,


To fill the Billing city automatically, Just Edit the Detail page button, and append the following in the Button's URL value:



For example, if your button is pulling up a New Account record, and it's present URL is:


change it to:


To auto populate it with a custom field, for eg: myCustomField__c


It would open the Edit page, with Billing City automatically populated to MyNewStreetAddressCanComeHere, or myCustomField__c, or anything you want. Similarly, you can auto populate other Fields as well (acc17state for billing state, acc17street for billing street, etc.)


To get further information regarding it, download the below mentioned PDF:



Hope it helps :)



All the above suggestions are great.  Another option is Visualforce's Javascript Remoting (see link below).  Javasript Remoting is especially well-suited when rerendering is not needed.


when i tried "actionFunction" using the below shared code,i am getting compile error as"Error: exampleCon Compile Error: The method System.PageReference incrementCounter() is referenced by Visualforce Page (actionsupp) in Remove the usage and try again. at line 14 column 26"..

any help.....


got the compile error with the code shared for action function