0

I'm trying to pass JavaScript array to the Apex controller to save it in the Salesforce. First of all I'm unable to use Array or List in the controller, it gives type mismatch kind of error. Which says can't assign string to Array or List. Below is my code.

It isn't even printing text on console using System.debug("Whatever!");

Visualforce

<apex:actionFunction name="userSelectedDates" action="{!saveSelectedDates}" rerender="errors">
      <apex:param name="userSelectedDatesParam" value="" />
</apex:actionFunction>

HTML Select control

<apex:pageBlock title="Calendar" id="calendarBlock">
         <div align="right">
           <select id="selectedDates" ></select>
           <button id="saveCalendar"><span>Save Calendar</span></button> 
         </div>
         <div id="myCalendar"></div> 
</apex:pageBlock>

JavaScript

var backupDates = [];
    var allSelectedDates = [];
        $("#saveCalendar").click(function () {
            backupDates = document.getElementById('selectedDates').options;
            for(var k=0; k<backupDates.length; k++){
                allSelectedDates.push(backupDates[k].text);
                console.log("Dates Backup : "+backupDates[k].text);
            }
            console.log(allSelectedDates);
            userSelectedDates(allSelectedDates);
        });

Apex Controller

public with sharing class EventsPageController {

    public Event__c Event {get; set;}
    private String thisCalendarDates {get;set;}

    public PageReference saveSelectedDates(){
        String selectedDates;
        thisCalendarDates = ApexPages.currentPage().getParameters().get('userSelectedDatesParam');
        System.debug('thisCalendarDates'+thisCalendarDates);
        System.debug(System.now());
     return null;
    }
}
4
  • The error say it all..... You are assigning an array to a string variable. Change private String thisCalendarDates {get;set;} to private String [] thisCalendarDates {get;set;}.... Jan 14, 2015 at 11:05
  • I was changed it. If you look at the JS code. I'm creating an array and trying to send it to controller. Now how an array will be assigned to a String as shown in the code above. If we forgot this thing for a moment that all the array data will transfer or not. I can't see anything on debug. @shaileshPatil
    – Superman
    Jan 14, 2015 at 11:15
  • @ShaileshPatil this line trying to assign JS array. ApexPages.currentPage().getParameters().get('userSelectedDatesParam');
    – Superman
    Jan 14, 2015 at 11:16
  • when passing to controller use allSelectedDates.toString() then in the controller you may be able to use1 JSON.serialize(thisCalendarDates, List<String>.class)` even if that does not work you will have the string in the controller that you can manipulate
    – Eric
    Aug 21, 2015 at 13:13

1 Answer 1

1

Try this controller code.

    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]);                
}

}

And VF like:

<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" />

7
  • Error: Illegal assignment from String to LIST<String>; on following line. thisCalendarDates = ApexPages.currentPage().getParameters().get('userSelectedDatesParam');
    – Superman
    Jan 14, 2015 at 11:27
  • I think there is no connection between JS and controller. It is not even printing System.now() :(
    – Superman
    Jan 14, 2015 at 11:30
  • its not printing System.now() because there is error before it reaches that line. Jan 14, 2015 at 12:57
  • Try adding System.debug('DT' + ApexPages.currentPage().getParameters().get('userSelectedDatesParam')) ; as first line of saveSelectedDates function and check what is getting passed Jan 14, 2015 at 12:59
  • here is the log. And I can't see anything related to USER_DEBUG in this log. I'm looking at logs which comes under debug logs. in setup.
    – Superman
    Jan 14, 2015 at 13:20

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.