4

Sorry for a simple question. I'm totally new to Salesforce.

I want to pass JavaScript array to Apex to save its data in the Salesforce custom object. I want to know the best approach to do it without JavaScript remoting. Following is the JavaScript code.

$("#saveCalendar").click(function () {
        getDatesBackup = document.getElementById('selectedDates').options;
        for(var k=0; k<getDatesBackup.length; k++){
            allSelectedDates.push(getDatesBackup[k].text);
            console.log("Dates Backup : "+getDatesBackup[k].text);
        }
        console.log(allSelectedDates);
    });
3
  • 1
    salesforce.stackexchange.com/questions/24666/… .... May help you....! Jan 13, 2015 at 8:32
  • @ShaileshPatil In that question the value is coming from the VF. In my case this is only between JS and Apex. What should be the Apex to retrieve that array from JS?
    – Superman
    Jan 13, 2015 at 8:52
  • You have to define an Apex variable and set the Javascript value to it. How to do that is written in the topic mentioned by @ShaileshPatil Jan 13, 2015 at 8:59

1 Answer 1

6

You can use ActionFunction for this. Here is sample for your reference.

Controller:

public class ActionFunctionCLS {

    public void IWantToDebug() {
        list<String> ls = ( List<String> )JSON.deserialize( Apexpages.currentPage().getParameters().get('node'), List<string>.class ) ;        
        System.debug('======================= ' + ls[0] );
        System.debug('======================= ' + ls[1]);
        System.debug('======================= ' + ls[2]);                
    }
}

VF Page:

    <apex:page controller="ActionFunctionCLS"  >

<script type="text/javascript">
    function doSave(node){
        var arr = ["foo", "bar", "baz"];
        paraFunction(JSON.stringify(arr));
    }       
</script>

<apex:form >
    <apex:actionFunction name="paraFunction" action="{!IWantToDebug}" rerender="view">      
         <apex:param id="anode" name="node" value="" />
   </apex:actionFunction> 
</apex:form>

 <input type="button" onclick="doSave();" value="test" />

Note that we are calling IWantToDebug Action from JS using paraFunction Action Function.

4
  • 1
    while useful, it doesn't address OP's requirement of passing a JS array to VF controller
    – cropredy
    Nov 26, 2015 at 0:07
  • 2
    updated the answer with better example. hope that brings more clarity. Nov 26, 2015 at 7:03
  • 2
    Excellent! The JSON.stringify is the key point here as Javascript objects and Apex objects need some common interchange format
    – cropredy
    Nov 26, 2015 at 16:16
  • This was very helpful. I had to pass a two dimensional string from Javascript and I just had to change List<String>.class to List<List<String>>.class and it worked fine. Thanks!
    – javanoob
    Aug 21, 2016 at 4:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.