-1

I am trying to get the values from javascript which is in array, to the apex controller and later save it.

When I check the debug logs, I see null value in it. Need help. Below is my code. I tried various methods available at the controller end, but luck. Below is the code for VF, JS & Apex.

VF code I have:-

<apex:form>
  <apex:actionFunction action="{!saveAccountIds}" name="sendAccountIds">
     <apex:param name="accountIds" value=""/>
  </apex:actionFunction>
</apex:form>

JavaScript Code:-

_getAccountsList: function()
 {
varaccountIds = [];
if (this.mainDirectionDiv.children.length > 0 && this.mainDirectionDiv.children[0].children.length > 0)
{
    varliChildren = this.mainDirectionDiv.children[0].children;
    for (i = 0; i < liChildren.length; i++)
    {
        accountIds.push(liChildren[i].getAttribute("liAccountId"));
    }

    console.log(accountIds);
}
sendAccountIds(accountIds);
}

Controller end:-

public void saveAccountIds()
{
        List<String> Id = (List<String>)    (Apexpages.currentPage().getParameters().get('accountIds'), List<String>.class);
    Id.add(Apexpages.currentPage().getParameters().get('accountIds'));
    System.debug('>>>>>>>>>>>>> HERE IS THE VALUE --> ' + Id);
}

When I check the debug logs, i see as

>>>>>>>>>>>>> HERE IS THE VALUE --> null

Once I confirm it in the debug logs, I can later think of pushing it in the object or wherever. Null value seems to be a major problem for me right now.

1
  • 1
    in javascript - create your array as JSON, use apex:actionFunction + apex:param assignTo a controller setter and then Json.deserialize into a list in controller in controller action method
    – cropredy
    Commented Jan 5, 2016 at 18:03

2 Answers 2

1

One more way use assignTo

<apex:actionFunction action="{!saveAccountIds}" name="sendAccountIds">
     <apex:param name="accountIds" value="" assignTo="{!strAccountIds}"/>
  </apex:actionFunction>

class

public String strAccountIds {get;set;}
public void saveAccountIds()
{
    System.debug('>>>>>>>>>>>>> HERE IS THE VALUE --> ' + strAccountIds );
}
6
  • Got the error in debug log :( 17:25:31.032 (32044428)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object
    – Viraj
    Commented Jan 5, 2016 at 11:56
  • @Mr.Orange just remove second debug ie.System.debug('>>>>>>>>>>>>> HERE IS THE VALUE --> ' + strAccountIds .split(';')); and check
    – Ratan Paul
    Commented Jan 5, 2016 at 11:57
  • Doesn't seem to be working, still the same error. Trying out till the the Tushar's way out till then.
    – Viraj
    Commented Jan 5, 2016 at 12:09
  • @Mr.Orange where you are getting error ?
    – Ratan Paul
    Commented Jan 5, 2016 at 12:14
  • in the debug logs...
    – Viraj
    Commented Jan 5, 2016 at 12:15
1

Not sure about your current approach. But you can try a different one

_getAccountsList: function()
 {
varaccountIds = '';
if (this.mainDirectionDiv.children.length > 0 && this.mainDirectionDiv.children[0].children.length > 0)
{
    varliChildren = this.mainDirectionDiv.children[0].children;
    for (i = 0; i < liChildren.length; i++)
    {
        accountIds = accountIds +liChildren[i].getAttribute("liAccountId")+';';
    }

    console.log(accountIds);
}
sendAccountIds(accountIds);
}

now in your controller split this using (';'). You will again get the list.

public void saveAccountIds()
{
       string str= Apexpages.currentPage().getParameters().get('accountIds');
    System.debug('>>>>>>>>>>>>> HERE IS THE VALUE --> ' + str.split(';'));
}
3
  • actually my js file is in the resource bundle and not within the vf page. that's the issue. I'm sure that the values are being reaching till the js, but further not traversing till the controller. Well about spiting, I did read about and tried but not working. Getting errors in mavens.
    – Viraj
    Commented Jan 5, 2016 at 11:43
  • try my method also is your actionfunction get called? Commented Jan 5, 2016 at 11:44
  • actionfunction is the only way am trying right now, as that's the only way to reach the JS file. I have also included the <apex:includescript for including that JS file which is in the resource bundle, but no luck as of now.
    – Viraj
    Commented Jan 5, 2016 at 11:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .