6

I've called a JS Remoting function on document.ready(). It works fine intermittently. But throws this error in Console when sometimes I come back to a page after a day and it was loaded a fresh:

Uncaught TypeError: Cannot read property 'tid' of undefined                VFRemote.js:85

Does having cache=true have any drastic effect on the page where JS Remoting is employed?

Edit: <- Code added below ->

<script type="text/javascript">
    $j = jQuery.noConflict();
    var mtid = '{!JSINHTMLENCODE(mi)}';
    $j(document).ready(function(){
        advpm.RemotingController.getListItems(new Date( $j('.s-date').text() ).toUTCString(), new Date( $j('.e-date').text() ).toUTCString(), mtid, $j('.opt1').is(':checked'), $j('.opt2').is(':checked'), $j('.opt3').is(':checked'), $j('.opt4').is(':checked'), $j('.opt5').is(':checked'), function(result, event){
            if (event.status && result){
                //do something with result here...
            } else {
                //error added to page here...
            }
        }, {escape:true});
    });
</script>

APEX Method code:

@ReadOnly @RemoteAction 
global static list<wListItems> getListItems(Date da0, Date da1, string matId, boolean showEvents, boolean showMyActivitiesOnly, boolean showMyMTOnly, boolean showActivitiesWithMTOnly, boolean showRelatedMT)
{
    list<wListItems> allitems = new list<wListItems>();
    Datetime frmDt = Datetime.newInstance(da0.year(), da0.month(), da0.day());
    Datetime toDt = Datetime.newInstance(da1.year(), da1.month(), da1.day());

    if (showEvents) 
    {
        list<Event> eventsList      = new list<Event>();
        string qry                  = 'select Id, Subject, WhatId, What.Name, ActivityDate, StartDateTime, EndDateTime, IsAllDayEvent, Owner.FirstName, Owner.LastName from Event where ActivityDate >= '+frmDt.format('yyyy-MM-dd')+' and ActivityDate <= '+toDt.format('yyyy-MM-dd')+(showMyActivitiesOnly == true ? ' and OwnerId = \''+ Userinfo.getUserId() +'\'' : '')+(matId != null && matId != '' ? ' and WhatID =: matId' : (showActivitiesWithMTOnly == true ? ' and WhatID IN (select Id from Case) and What.Type = \'Case\'' : '')+' order by StartDateTime');
        for(Event e : (list<Event>)database.query(qry))
        {
            allitems.add( new wListItems(e.Id,e.Subject,'',e.ActivityDate,'Event','') );
        }
    }
    return allitems;
}

This call when viewed in Chrome Console (the request headers):

action: "advpm.RemotingController"
ctx: {,…}
data: [Sun, 27 Jul 2014 00:00:00 GMT, Sun, 3 Aug 2014 00:00:00 GMT, , true, false, false, false, true]
method: "getListItems"
tid: 5
type: "rpc"

The parameters view in Chrome Console:

0: "Sun, 27 Jul 2014 00:00:00 GMT"
1: "Sun, 3 Aug 2014 00:00:00 GMT"
2: ""
3: true
4: false
5: false
6: false
7: true

Chrome Console "Preview" tab - http://screencast.com/t/wJMB4ixwx:

action: "Unknown"
data: ""
message: "Remoting request invalid for your session.  Refresh page and re-submit request"
method: "Unknown"
ref: false
statusCode: 402
type: "exception"
vfDbg: true
vfTx: true
where: ""

and the dreaded Console Exception logged - http://screencast.com/t/pDFZQqJ6F9:

Uncaught TypeError: Cannot read property 'tid' of undefined VFRemote.js:85
VFExt3.Direct.VFExt3.extend.getTransaction VFRemote.js:85
VFExt3.Direct.RemotingEvent.VFExt3.extend.getTransaction VFRemote.js:88
(anonymous function) VFRemote.js:131
a.Event.fire VFRemote.js:52
a.Observable.fireEvent VFRemote.js:47
VFExt3.direct.RemotingProvider.VFExt3.extend.onData VFRemote.js:94
VFExt3.extend.handleResponse VFRemote.js:75
a VFRemote.js:39
(anonymous function) VFRemote.js:40
VarunC
  • 5,940
  • 11
  • 59
  • 105
  • can you post your whole code? – Mohith Shrivastava Aug 02 '14 at 06:22
  • @Mohith - I've added code and relevant Debug info that I've been receiving at my end in chrome. The thing is, as soona s I [ress F5 while console is ON, it gets resolved and no error and my call works ok. Then when I most of the time close my browser, and then come back again after login it comes back. Also, I didn't get it, the most troubling part, why does the "Preview" of Remoting call shows Something different error than what JS Console shows ... – VarunC Aug 02 '14 at 08:08
  • Can you post apex method code? – Ashwani Aug 02 '14 at 08:28
  • sry forgot to add that earlier, APEX class code added as well now. – VarunC Aug 02 '14 at 08:55
  • Based on http://stackoverflow.com/questions/21614184/visualforce-uncaught-typeerror-cannot-read-property-tid-of-undefined, the problem is likely to relate to the remote method arguments. I suggest you check all of them in the JavaScript: you can console.log them or use the JavaScript debugger: see e.g. http://salesforce.stackexchange.com/questions/36715/how-do-i-start-to-debug-my-own-visualforce-javascript. – Keith C Aug 02 '14 at 09:18
  • @KeithC - I've verified all the variables already, all are having values, nothing being left. Yes, one variable can sometime be left empty string, but I suppose that should not be an issue, it is a String parameter, so I can pass an Empty string in it, right? .... though I am worried about why the JS Remoting http Response is showing me error "Remoting request invalid for your session. Refresh page and re-submit request", while the Console is logging an error "Uncaught TypeError: Cannot read property 'tid' of undefined". – VarunC Aug 02 '14 at 09:22
  • @VarunC Sorry I now notice the detail you already posted in your question... So the request never gets to the server right? Hopefully someone who has personally experienced this and solved it will answer. – Keith C Aug 02 '14 at 09:30
  • @KeithC : I'm hoping so :-) ... though, in your experience have you seen any issues of JS Remoting on a VF page with Cache Enabled? – VarunC Aug 02 '14 at 09:42
  • Are you absolutely sure NONE of the parameters are being passed in as 'undefined'. You said that you may be passing in an empty string, which should be ok if it is '' but not if it is 'undefined' – Eric Aug 02 '14 at 23:39
  • yes, absolutely sure it is empty and not 'undefined'. – VarunC Aug 03 '14 at 05:38
  • Interesting. FF errors with "d is undefined" while Chrome and IE errors with "tid is undefined" (both on line 85). Same sandbox org, same code, no changes otherwise, except the browser. – sfdcfox Oct 07 '14 at 23:45
  • Not sure if you've solved this or not, but I've found several things w/ relation to this: If you're buffering, and the buffer exceeds ~750k, or if you're not buffering, and you've got too many requests, and there was another one I forgot because I'm old. – HungryBeagle Apr 09 '15 at 19:14
  • Oh yeah - You said it throws the error after coming back to the same page after a day? So is the session expired at that point? – HungryBeagle Apr 09 '15 at 19:16

5 Answers5

2

It's been long sine this question has been posted but still writing in case any future readers. The key here is to add { buffer: false, escape: true, timeout: 30000 }

What this line does is it will not allow remote manager wouldn't to batch up the remoting calls. Why this is helpful because sometimes we just don't have data to pass to a remoting function when bootstrapping happens for JS code resulting in undefined objects being passed to remoting calls and thus the the above error.

Incase wondering where to add it:

scope.getParentId = function() {

        EsignCongaSigningUrlRemoting.getEsignUrlParentIdFromObjectId($scope.objectId, 1, function(response, event) {
            if(event.status) {
                console.log('parentId in response = ',response);
                $scope.parentId = response;
                $scope.signingUrl($scope.parentId);

            } 
            else {
                $scope.parentId = '';
            }   


        },{ buffer: false, escape: true, timeout: 30000 }); 

    };
Cyclotron3x3
  • 171
  • 1
  • 6
0

Disabling buffering can definitely help depending on what operations you're performing, but in a large code base this can also get unwieldy. It seems that it is a case of bad parameters that generally causes this, but if you've got a lot of requests being bundled together (17 in my instance) then updating all of them not to buffer to find the culprit may not be desirable.

I managed to find the culprit in my code by putting a break point on line 2070 of VFremote.js:

combineAndSend: function() {
    var d = this.callBuffer.length; // break on this line

If your calls are buffered, then by inspecting this.callBuffer you can see the list of pending calls, along with the data that will be passed to them:

List of remoting calls to be called

By checking the data member for each call you should be able to locate the one that has a dreaded undefined where real data should be.

(Interestingly I seem to have some double-ups in this list, will have to investigate if they're evaluated on the backend more than once given they have no parameters and are read-only operations).

Matt Lacey
  • 25,618
  • 7
  • 66
  • 149
0

One for reason for this error is you are declaring the remoting function inside the VF page code which re-renders.

To resolve this, declare the script tag (containing the remote function) outside the apex:form tag.

Vikas
  • 113
  • 5
0

Came through same issues. Hope the answer below helps others.

There can be multiple issues here, related to one thing: RemoteAction. It can be:
- Static Resource contains the RemoteAction
- Accessing remoteAction from different package may be
- RemoteAction not defined

So, please look for issue related to RemoteAction to fix this.

ipradhansk
  • 2,327
  • 4
  • 26
  • 50
0

From my recent experience, this was because my remoting function was no longer on the page (so not defined). I had written a messy script that was removing references to it and then calling it.

arlon
  • 13
  • 6