You need to sign in to do that
Don't have an account?
swapna9
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
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
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 :
.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; } }
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
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 :)
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
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.....
got the compile error with the code shared for action function