+ Start a Discussion
swapna9swapna9 

javascript in salesforce

Hi,

 

I am new to javascript in salesforce.my 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....

 

Thanks

LakshmanLakshman

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: http://www.salesforce.com/us/developer/docs/ajax/apex_ajax.pdf

 

Regards,

Lakshman

 

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">
<apex:form>
<!-- Define the JavaScript function sayHello-->
<apex:actionFunction name="sayHello" action="{!sayHello}" rerender="out"
status="myStatus"/>
</apex:form>
<apex:outputPanel id="out">
<apex:outputText value="Hello "/>
<apex:actionStatus startText="requesting..." id="myStatus">
<apex:facet name="stop">{!username}</apex:facet>
</apex:actionStatus>
</apex:outputPanel>
<!-- Call the sayHello JavaScript function using a script element-->
<script>window.setTimeout(sayHello,2000)</script>
<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:outputPanel>
<apex:form>
<apex:actionFunction action="{!methodOne}" name="methodOneInJavascript"
rerender="showstate">
<apex:param name="firstParam" assignTo="{!state}" value="" />
</apex:actionFunction>
</apex:form>
</apex:page>

/*** 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:form>
<apex:outputpanel id="counter">
<apex:outputText value="Click Me!: {!count}"/>
<apex:actionSupport event="onclick"
action="{!incrementCounter}"
rerender="counter" status="counterStatus"/>
</apex:outputpanel>
<apex:actionStatus id="counterStatus"
startText=" (incrementing...)"
stopText=" (done)"/>
</apex:form>
</apex:page>


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

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.

 

 

Regards,

Shravan

HarpreetHarpreet

Hi Swapna,

 

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

 

/e?acc17city=MyNewBillingCityCanComeHere

For example, if your button is pulling up a New Account record, and it's present URL is:
https://eu0.salesforce.com/001/e

 

change it to:

https://eu0.salesforce.com/001/e?acc17city=MyNewBillingCityCanComeHere

 

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

https://eu0.salesforce.com/001/e?acc17city=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:

https://na1.salesforce.com/help/doc/en/salesforce_formulas_cheatsheet.pdf

 

 

Hope it helps :)

 

cwall_sfdccwall_sfdc

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.

 

http://www.salesforce.com/us/developer/docs/pages/Content/pages_js_remoting.htm

srutmsrutm

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 salesforce.com. Remove the usage and try again. at line 14 column 26"..

any help.....

srutmsrutm

got the compile error with the code shared for action function