+ Start a Discussion
mpolimpoli 

Access other Apex methods through AJAX toolkit?

Hi, I'm not sure if I'm posting this in the correct forum, so please direct me to the appropriate one if this isn't it.

 

Right off the bat, I should mention that I'm writing a standard Visualforce page that utilizes Apex code on the server, so I'm NOT using any s-controls, though I am exploring the possibility of using the AJAX toolkit.

 

I'm still pretty new to Apex/Visualforce, and I was wondering if it's possible to access my Apex methods from my Visuaforce page. Specifically, I'd like to pass a large string from my javascript to an asynchronous method in my Apex code on the server side for processing. The method will parse the string and eventually update a large number of records, the reason I chose to make it asynchronous. I figured I would accomplish this via the AJAX toolkit.

 

I understand that the AJAX toolkit allows one to perform operations, such as database query, merge, and update, but is it possible to use it to access custom methods? If not, I was wondering if someone could recommend another approach I should take to send information back to the Apex code?

 

Thank you.

Best Answer chosen by Admin (Salesforce Developers) 
DevAngelDevAngel
You would not use the Ajax toolkit for this.  A combination of <apex:form> and a command link or action function with child param tag is all you need.

<apex:form>
<apex:actionFunction name="passData" action="{!myControllerMethod}" rerender="otherstuff">
<apex:param id="largeString" name="largeString" value="" assignTo="{!largeValue}" />
</apex:actionFunction>
</apex:form>

 

Action function allows you to cause a post to your controller.  In this case, the myControllerMethod method of the controller will be invoked.  You would create a public property for largeValue.

 

Public String largeValue ( get; set; }

 

When the post is made the value of the param will be posted and saved to the largeValue property.  The way that the param gets it's value is in the javascript invocation of the passData action function.

 

 

<script>
passData("my very long string");
</script>

 

Because we are using the rerender attribute of the actionFunction, this will be done asyncronously using the VF ajax capabilities.