[SalesForce] JavaScript Remoting on Standard Page Layout

I have a couple of JavaScript remoting functions used to count records and to display that count to the end user. The JavaScript remoting functions are on a Visualforce page, and the Visualforce page is embedded on a page layout of a custom object.

What is happening is that the javascipt remoting functions are supposed to count the number of records available after filters are set. However intermittently the user will receive the error “Unable to connect to server”.

I have a couple of JavaScript remoting functions are working against a custom object, and there are other remoting functions working on the Task object. These functions are doing nothing more than counting records in the system. The JavaScript remoting function counting records on the Task object are working more constistently than the JavaScript remoting functions counting records on the custom object.

I’m worried that this error may a result of the fact that the custom object is not indexed, and therefore counting records for that object may require more time than what is allowed for JavaScript remoting functions. If this is the case, should I do something to get the custom object indexed?

Also, I’m concerned that the “Unable to connect to server” error is a known bug, from what I have gathered on the forums. If this is the case is there a known fix?

Below is an example of the JavaScript remoting function on the Task object, to count the number of task records for the given parameters. This function is failing some of the time, mostly in the morning when first logging into the system.

//Javascript Remoting Function
controllerExampleCounter.asyncReviewedClosed(String("{!Example_Custom_Setting_Validation__c.OwnerId}"), function(result, event) {
    if (event.status) {
        $("#reviewedClosed").text(addCommas(result));
    } else if (event.type === "exception") {
        $("#reviewedClosed").text("Error: " + event.message);
    } else {
        $("#reviewedClosed").text("Error: " + event.message);
    }
}, {escape:true});

//Apex Function
@RemoteAction @ReadOnly
global static Integer asyncReviewedClosed(String owner_id)
{
    Integer incomplete = 0;

    try{
        incomplete = [Select count() 
            From Task 
            Where RecordType.Name = 'Qualification Task' 
                And CreatedById = :String.escapeSingleQuotes(owner_id) 
                And IsClosed = true 
                And CreatedDate = THIS_YEAR];
    } catch(Exception e) {
    }

    return incomplete;
}

Best Answer

The following is not officially support, so be careful, and is applied to all requests. Also, be aware that Salesforce.com's general HTTP request timeout may come into play.

<script>
_Visualforce.Ajax.timeout=120000;
...
</script>

And, BTW, Visualforce Remoting is a popular feature of Visualforce.

Related Topic