1

I have a situation where I am using <apex:actionFunction> oncomplete attribute to call a JS function and i am sending some data as parameters but this data is very heavy, having around 90,000 characters and I am not able to pass it as parameter in actionFunction's oncomplete, actually the JS functin on oncomplete is not getting called.

My actionFunction :

<apex:actionFunction name="GetAllData" action="{!GetDataController}" reRender="none" oncomplete="GetProjectCompleted('{!data1}')">
    <apex:param id="userAndDomainProj" name="userAndDomainProj" value="" />
</apex:actionFunction>

The data I am trying to get from Apex Class is {!data1}. If data1 has small data I am able to fetch it easily and if data is large I am not even able to call function.

What can be the solution.

Is there anty other way to send this heavy data from Apex class to visualforce page.

10
  • I'd be willing to bet the issue is not necessarily with data size, rather, the larger data set has a syntax error in it such as a single quote that is causing an issue with the script execution. Do you see any errors in the browser developer console? If so can you make sure you provide that error for us.
    – gNerb
    Commented Sep 19, 2018 at 15:42
  • No there was no error in browser's developer console Commented Sep 19, 2018 at 15:43
  • When you view the source of the page do you see any data in that part of the source code. The {!data1} should be replaced with the raw data you're trying to pass to your JS method. Can you provide a copy of the HTML output including the big data so we can take a look?
    – gNerb
    Commented Sep 19, 2018 at 15:45
  • But one thing surely happened that I printed the data in System.Debug and copied the data and pasted it in a string variable and I noticed that it had some special characters which weren't escaped and giving error, but this was the case when I manually pasted the string data. can this be the case? Commented Sep 19, 2018 at 15:46
  • That is a very likely cause, the best way to find out is to do what my previous comment says.
    – gNerb
    Commented Sep 19, 2018 at 15:46

1 Answer 1

2

I was able to get the desired behavior with "small data" by creating the sample page below:

<apex:page controller="testPageController" doctype="html-5.0">
    <apex:form id="none">
        <apex:actionFunction name="actionFunction" action="{!doIt}" reRender="none" oncomplete="alert('{!data}')">
            <apex:param name="param" value=""/>
        </apex:actionFunction>
    </apex:form>
    <script>
    actionFunction('hello world!');
    </script>
</apex:page>

Controller:

public class testPageController {
    public String data {get; set;}

    public testPageController() {
        this.data = '';
    }

    public void doIt() {
        String param = ApexPages.currentPage().getParameters().get('param');
        system.debug(param);
        this.data = param;
    }
}

To be honest I'm surprised this even works because if you view the source of the page this is what the action function looks like:

<script id="j_id0:none:j_id1" type="text/javascript">
    actionFunction=function(param){
        A4J.AJAX.Submit(
            'j_id0:none',
            null,
            {
                'similarityGroupingId':'j_id0:none:j_id1',
                'oncomplete':function(request,event,data){
                    alert('')
                },
                'parameters':{
                    'param':(typeof param!='undefined'&&param!=null)?param:'',
                    'j_id0:none:j_id1':'j_id0:none:j_id1'
                 } 
             } 
         )
     };
</script>

Notice the alert has an empty string parameter. The fact that my page can actually alert the "hello world" even though that parameter renders as empty even after the form is re-rendered is what surprises me.

Anyway, since this works my only conclusion is the one I mentioned in the comments. there must be something in the string that is causing a syntax error. This is verified by the fact that you said when you copy/paste the debug into the script and run it you get errors.

I was also able to replicate your issue where there are no developer console errors by manually setting the value of data to '\'test' in the apex code:

 public void doIt() {
    this.data = '\'test';
}

doing this actually allows the HTML to render the attribute:

alert(''test')

In which there is the very obvious syntax error, but this does not throw any developer console errors for a reason I can't ascertain.

Conclusion

Something in {!data1} is causing a syntax error.

7
  • In this case how can special characters be handled, as here it can be seen that you have escaped the quote but still it gives error Commented Sep 19, 2018 at 16:21
  • I think it depends on your data, the safest bet would be to remove the characters altogether. You can also consider doing a string.replace(data1, '\'', '"') to replace all single quotes with double quotes if you think that makes sense for your data.
    – gNerb
    Commented Sep 19, 2018 at 16:24
  • You could also consider JSON.serialize(data1) to convert it into a properly formatted json string and then pass the json directly to your method by removing the single quotes oncomplete="alert({!data})" but again, that depends on your data.
    – gNerb
    Commented Sep 19, 2018 at 16:26
  • 1
    There is one more solution, and that is to use JavaScript remoting or the SFDC javascript apis to handle the bulk of the work in JS. I can't help but feel that your issue here is that you're taking advantage of a behavior that probably shouldn't work at all. There's a risk of this getting "fixed" breaking your code.
    – gNerb
    Commented Sep 19, 2018 at 16:31
  • 1
    JavaScript remoting worked for me and its easier to use than calling oncomplete and stuff. Thanks for the answer. Commented Sep 19, 2018 at 19:51

You must log in to answer this question.

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