8 Replies Latest reply on Feb 11, 2019 3:39 PM by jross

    Chat.js Ajax.Request failing for comments

    jross Apprentice

      Hi all,

       

      Another hiccup on the road to getting Chat working.

      I've been able to work out a lot of the bugs with help from this community, but now I'm having trouble with an Ajax request in returning the comments in a chat to display on the window after responding/opening a chat request.

       

      So the code below is meant to generate a URL to gather the comments belonging to the currently open chat session:

      // get new comments
      function GetComments() {
        var url = '../wd/Query/List.rails?class_name=' + CommentClassName + '&attributes=Id%2C' + CommentTextAttribute + '%2C_CreateDate%2C_CreateUser%2c' + CommentToParentRelationship + '.Status.Name%2C' + CommentToParentRelationship + '.RaiseUser.Title%2C' + CommentToParentRelationship + '.CurrentAssignment.User.Title&cns=' + CommentToParentRelationship + '-e-0-_a_Id-gte-1-';;
        url += '&c0=' + SessionGuid + '&c1=' + LastCommentID;
        new Ajax.Request(url, {
        onSuccess: function(response) { GetComments_Response(response) },
        onFailure: function(response) { Ajax_Failed(ErrorGettingComments, response) }
        });
      }
      
      
      
      

       

      The URL, when entering in valid data for chat session GUID and comment ID (0 by default) and returns all the comments for that particular chat session, which is Great!

      However the above code is failing at the new Ajax.Request stage as it only returns a failure status and not a succeed to call on the GetComments_Reponse function

       

      function GetComments_Response(response) {
        ResetError();
        var NewComments = new Array();
        var NewCommentsText = '';
        var SessionEnded = false;
      
      
        var json = eval('(' + response.responseText + ')');
        console.log(json);
        if (json.objects.length > 0) {
        json.objects.each(function(item) {
        if (item.attributes['Id'] > LastCommentID) NewComments.push(item);
        });
      
      
        NewComments.sort(function(a, b) { return a.attributes['Id'] - b.attributes['Id'] });
      
      
        for (var i = 0; i < NewComments.length; i++) {
        NewCommentsText += '<br /><b>' + NewComments[i].attributes['_CreateUser'] + ' - ' + NewComments[i].attributes['_CreateDate'];
        NewCommentsText += '</b><br />' + NewComments[i].attributes[CommentTextAttribute];
        LastCommentID = NewComments[i].attributes['Id'];
        if (NewComments[i].attributes[CommentToParentRelationship + '.Status.Name'] == WaitingResponseStatus) $('StatusSpan').innerHTML = ChatPendingText;
        else if (NewComments[i].attributes[CommentToParentRelationship + '.Status.Name'] == InProgressStatus) {
        if (IsAnalystSession)
        {
        $('StatusSpan').innerHTML = ActiveAnalystSessionText.replace('{user}', NewComments[i].attributes[CommentToParentRelationship + '.CreationUser.Title']);
        }
        else {
        $('StatusSpan').innerHTML = ActiveUserSessionText.replace('{analyst}', NewComments[i].attributes[CommentToParentRelationship + '.CurrentAssignment.User.Title']);
        }
        }
        else {
        $('StatusSpan').innerHTML = SessionEndedText;
        SessionEnded = true;
        }
        }
      
      
        if (NewCommentsText != '') {
        $('ChatHistory').innerHTML += NewCommentsText;
        //alert('about to scroll');
        $('ChatHistory').scrollTop = $('ChatHistory').scrollHeight + 200;
        //alert('scrolled');
        }
      
        if (SessionEnded == true) SessionEndedDisplay();
        }
      }
      
      

       

      My colleagues who have looked this over think that the URL, or the data is gathers is not being changed to a JSON object for the GetComments_Response to use.

        • 1. Re: Chat.js Ajax.Request failing for comments
          jross Apprentice

          I've re-written the GetComments() function in the Javascript to this:

          function GetComments() {
            var params = 'class_name=' + CommentClassName + '&attributes=Id%2C' + CommentTextAttribute + '%2C_CreateDate%2C_CreateUser%2c' + CommentToParentRelationship + '.Status.Name%2C' + CommentToParentRelationship + '.RaiseUser.Title%2C' + CommentToParentRelationship + '.CurrentAssignment.User.Title&cns=' + CommentToParentRelationship + '-e-0-_a_Id-gte-1-';
            params += '&c0=' + SessionGuid + '&c1=' + LastCommentID;
            new Ajax.Request('./Query/List.rails', {
            method: 'post',
            parameters: params,
            onSuccess: function(response) {alert("This has worked!"); GetComment_Response(response) },
            onFailure: function(response) { alert("This has Failed"); Ajax_Failed(ErrorGettingComment, response) }
            });
          }
          
          
          
          
          
          
          

           

          This was based on another method I saw in the chat.js file provided with the social module. This no longer throws a server 500 error or a 404 error as I was previously getting.

          I added in alerts in the onSuccess and OnFailure sections to determine if the Ajax Request was succeeding or failing. Happy to say the GetComments() is succeeding, but it now means the issue is in the GetComments_Response(response) function:

           

          function GetComments_Response(response) {
            ResetError();
            var NewComments = new Array();
            var NewCommentsText = '';
            var SessionEnded = false;
            var json = eval('(' + response.responseText + ')');
            if (json.objects.length > 0) {
            json.objects.each(function(item) {
            if (item.attributes['Id'] > LastCommentID) NewComments.push(item);
            });
          
          
            NewComments.sort(function(a, b) { return a.attributes['Id'] - b.attributes['Id'] });
          
          
            for (var i = 0; i < NewComments.length; i++) {
            NewCommentsText += '<br /><b>' + NewComments[i].attributes['_CreateUser'] + ' - ' + NewComments[i].attributes['_CreateDate'];
            NewCommentsText += '</b><br />' + NewComments[i].attributes[CommentTextAttribute];
            LastCommentID = NewComments[i].attributes['Id'];
            if (NewComments[i].attributes[CommentToParentRelationship + '.Status.Name'] == WaitingResponseStatus) $('StatusSpan').innerHTML = ChatPendingText;
            else if (NewComments[i].attributes[CommentToParentRelationship + '.Status.Name'] == InProgressStatus) {
            if (IsAnalystSession)
            {
            $('StatusSpan').innerHTML = ActiveAnalystSessionText.replace('{user}', NewComments[i].attributes[CommentToParentRelationship + '.CreationUser.Title']);
            }
            else {
            $('StatusSpan').innerHTML = ActiveUserSessionText.replace('{analyst}', NewComments[i].attributes[CommentToParentRelationship + '.CurrentAssignment.User.Title']);
            }
            }
            else {
            $('StatusSpan').innerHTML = SessionEndedText;
            SessionEnded = true;
            }
            }
          
          
            if (NewCommentsText != '') {
            $('ChatHistory').innerHTML += NewCommentsText;
            //alert('about to scroll');
            $('ChatHistory').scrollTop = $('ChatHistory').scrollHeight + 200;
            //alert('scrolled');
            }
          
            if (SessionEnded == true) SessionEndedDisplay();
            }
          }
          
          
          

           

          Where is this failing? The idea is to get the NewCommentsText and post it into the ChatHistory box on the chat window, but all I get now is Chat telling me Loading... and no comments.

          • 2. Re: Chat.js Ajax.Request failing for comments
            jross Apprentice

            I've done some further testing of this code and firebug has determined that the javascript is working correctly for the send comments and get comments.

            The issue appears to be either with the GetComments_Response function not grabbing the comments, or somewhere in the code the comments are not displaying in the chat textbox.

             

            Any ideas?

            • 3. Re: Chat.js Ajax.Request failing for comments
              Apprentice

              Did you ever get this resolved? I am having the same issue now.

              • 4. Re: Chat.js Ajax.Request failing for comments
                jross Apprentice

                Hi DrNelson,

                 

                I eventually found that the reason for this error was to do with the Chat History attribute created in LANDesk under the Object Manager.

                In the instructions given, it advised to create this as a String (2048) type, but my LANDesk database was created in Oracle and kept converting these to an NCLOB data type.

                 

                I re-created the Chat History and set it as String(2000) which was the Oracle database limit and this seemed to fix the issue with the above code.

                However we've just migrated our database over to MS SQL server, so I will need to re-test this to see if it still works.

                 

                Hope this helps!

                • 5. Re: Chat.js Ajax.Request failing for comments
                  Apprentice

                  Thanks, we are on a SQL database, so it will be interesting to see your results!

                  • 6. Re: Chat.js Ajax.Request failing for comments
                    Apprentice

                    We decided to drop trying to use the chat feature in service desk, it is currently to clunky to be of any use. Plus people don't want to log in just to ask a question.

                    • 7. Re: Chat.js Ajax.Request failing for comments
                      John Haddad Expert

                      hello

                       

                      did you manged to resolve this ?

                      • 8. Re: Chat.js Ajax.Request failing for comments
                        jross Apprentice

                        Hi John,

                         

                        We never got this resolved as our management decided not to implement this module.

                        We have now decided to move licensing from LANDesk to Ivanti ISM (HEAT).

                         

                        Sorry