function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Luca Benedettini 25Luca Benedettini 25 

Passing an array from Javascript to Apex

Hi,
I have a little problem but I'm not able to find a solution.I'm trying to pass an array variable "d" from javascript to a variable "Value" in the apex controller. Here my code:
javascript:
function setVal(param){
    param =param.replace("[","");
    param =param.replace("]","");
    var mylist = new Array()
    mylist = param.split(',');
    alert(mylist);
    var d =new Array();
    for( var i = 0; i < mylist.length; i++ ){
    var appoggio = mylist[i].trim();
    var strAppoggio = document.getElementById( appoggio ).innerHTML;
    strAppoggio = strAppoggio.substring(strAppoggio.lastIndexOf("*") + 1, strAppoggio.lastIndexOf("+"));
    d.push(strAppoggio);              
   }
   afConMethod(d.join());  
   alert('dToString: ' + dToString);           
   }

visualforce:
<apex:commandButton value="Get" onclick="setVal('{!firstrow}');" reRender="output"/>
<apex:actionFunction name="afConMethod" action="{!MyConMethod}" >
<apex:param value="" name="theValue" assignTo="{!Value}" />

apex controller:
public List<String> values {get;set;}
private String Value = 'no';
   public void setValue(String n) {
        Value = n;
   System.debug('Set Passed in Value: '+ Value);     
    }
    public String getValue() {
   System.debug('Get Passed in Value: '+ Value);
        return Value;
       
    }
public void MyConMethod() {
        system.debug('Start');             
       convertStrToList(Value);
        
    }
 
    public void convertStrToList(String str) {
    String[] listToReturn = new List<String>();
    if(String.isNotBlank(str)) {
        for(String eachStr : str.split(',')) {
            listToReturn.add(eachStr);
        }
    }
    system.debug(listToReturn);
    }
The debug logs shows the message: Passed in value: null
I really don't understand what's going wrong. Why Value doesn't contain the array in d? Help me please
 
Best Answer chosen by Luca Benedettini 25
sandeep madhavsandeep madhav
Hi,
Just pass the string to apex and parse the string as list in apex, as your doing in function in vf page.

The variable you declared as
public List<String> values {get;set;}

but your assigning to "Value"
<apex:param value="" name="theValue" assignTo="{!Value}" />

It should be :
<apex:param value="" name="theValue" assignTo="{!values}" />

Mark this as best answer if this helps

All Answers

sandeep madhavsandeep madhav
Hi,
Just pass the string to apex and parse the string as list in apex, as your doing in function in vf page.

The variable you declared as
public List<String> values {get;set;}

but your assigning to "Value"
<apex:param value="" name="theValue" assignTo="{!Value}" />

It should be :
<apex:param value="" name="theValue" assignTo="{!values}" />

Mark this as best answer if this helps
This was selected as the best answer
Luca Benedettini 25Luca Benedettini 25
Thank you sandeep. I solve the problem using an <paex:inputhidden> tag to pass the String and ,as you said, parse it with the function convertStrToList . It works, but also your solution works as well. tahnk you again