You need to sign in to do that
Don't have an account?
Get JSRemoting to execute on page load
Here is the example I got, but it triggers off of ONCLICK. I want to be able to use the JSRemoting to run an APEX class which would return data and ultimately the data would be shown on the first rendering of the visualforce page. I've tried a dozen different things, but no luck. What do I need to do to this code to get the desired result ...
<apex:page controller="MyJSRemoting">
<script type="text/javascript">
function getAccountJS() {
var accountNameJS = document.getElementById('accountname').value;
MyJSRemoting.getAccount( accountNameJS, function(result, event)
{
if (event.status)
{
document.getElementById("{!$Component.theBlock.pbs.pbsi2.accId}")
.innerHTML = result.Id;
document.getElementById("{!$Component.theBlock.pbs.pbsi1.name}")
.innerHTML = result.Name;
}
}, {escape:true});
}
</script>
<input id="accountname" type="text"/>
<button onclick="getAccountJS();">Get Account</button>
<apex:pageblock id="theBlock">
<apex:pageblocksection columns="2" id="pbs">
<apex:pageblocksectionitem id="pbsi1">
<apex:outputtext id="name">
</apex:outputtext>
</apex:pageblocksectionitem>
<apex:pageblocksectionitem id="pbsi2">
<apex:outputtext id="accId">
</apex:outputtext>
</apex:pageblocksectionitem>
</apex:pageblocksection>
</apex:pageblock>
</apex:page>
TIA, Mike
You might try to use the action="" keyword on the Page header. That will call a method in the controller that is called before the constructor is called.
All Answers
You might try to use the action="" keyword on the Page header. That will call a method in the controller that is called before the constructor is called.
Okay, I'll try that. Actually, I'm not wedded to the idea of using JavaScript, but as far as I can tell, it's the only way to invoke an APEX class from within a visualforce page. Probably not true, but I'm very new to this.
The action="{!method}" attribute does just that: it calls Apex Code on the server after the page's constructor, but before rendering.
You can also use the constructor of your class to call other code before the page loads.
Finally, when using extensions, you can also call code in their constructors:
The "controller" in this case gives you access to the current record you're working on.
Okay, I've ditched the JSRemoting and am now trying to use the action like this.
<apex:page standardController="Opportunity"
extensions="OpportunityControllerExtension,JSONParserUtil"
showHeader="false" renderAs="{!$CurrentPage.parameters.renderAs}"
action="{!parseJSONResponse}">
here is the class
The question is: how do I return response.getBody() to be displayed on my visualforce page?
thx
Nevermind, I think I found some examples.
Thanks