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 List variable "values" 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="{!theValue}" />

apex controller:

   public String  theValue {get;set;}
    public List<String> values {get;set;}
    //controller
    public ApexPages.StandardController stdController;
    public excelPageController(ApexPages.StandardController stdcontroller) {
    //this.stdController = stdController;
        oppId =ApexPages.currentPage().getParameters().get('ID');//Current opportunity ID;
    }
     system.debug('Start');
        //theValue=Apexpages.currentPage().getParameters().get('theValue');      
        system.debug('Passed in theValue: '+ theValue);
        values = convertStrToList(theValue);
        system.debug('Passed in value2: '+ values);

    }

    public String[] convertStrToList(String str) {
    String[] listToReturn = new List<String>();
    if(String.isNotBlank(str)) {
        for(String eachStr : str.split(',')) {
            listToReturn.add(eachStr);
        }
    }
    return listToReturn;
    }

The debug logs shows the message: Passed in value: null

I really don't understand what's going wrong.

  • Why d.toString() when you call your controller method in javascript ? – SF_user Oct 27 '16 at 15:08
  • Because I wanted to pass the array as a String using afConMethod. And then split it into a List<String> in the apex Code. I do it in the past with a string variable so I think: trasform it into a String a follow the same path. – L.benedettini Oct 27 '16 at 15:12
  • You normally can pass the array directly to your controller ! – SF_user Oct 27 '16 at 15:18
  • I tried but it gave me the same result, could you please explain me better maybe i don't understand what you mean precisely. Thank you for your help – L.benedettini Oct 27 '16 at 15:23
  • see my answer, it will solve your issue – Santanu Boral Oct 27 '16 at 15:27

Use this code in myMethod() to capture the value that is being passed from actionFunction.

public void MyConMethod() {
theValue=Apexpages.currentPage().getParameters().get('theValue');
System.debug('Passed in value: '+theValue);
values = convertStrToList(theValue);
} 
  • I tried Santanu but The logs show me the same debug, now I post the code with the changes I made so you can tell me if I do somenthing wrong – L.benedettini Oct 27 '16 at 15:31
  • I tried both the solutions that you suggested me more than once, but the results is still the same, when I see the debug log, thevalue seems to be " null". I edit my code see it please – L.benedettini Oct 28 '16 at 8:00

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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