Get Dialog from Collection by a giving Dialog ID

Version 1
    This document was generated from CDN thread

    Created by: Juan Othmaro Menjivar on 16-12-2013 03:47:17 PM
    Good day.

    I'm currently developing my own Call Control gadget. and in the Answer Button I want to get (from the user.getDialogs() Collection)
    the Dialog with an specific ID, to be able to perform the required action on Dialog.Actions.ANSWER for that Dialog.

    the problem is that when iterating the objects coming from user.getDialogs(), it gives me an error that says: "dialog.getId() is undefined".

    I want to know if there is any other workaround to get this done.  I'll really appreciate your help.


    Thanks in advance


    Subject: RE: Get Dialog from Collection by a giving Dialog ID
    Replied by: Juan Othmaro Menjivar on 16-12-2013 06:02:50 PM
    Thank you for replying back Mr. 

    Horvath

    I just want to confirm than Miguel Cubias and I are working together and he replied because I was offline. Hope you can help us to clarify our issue.

    Subject: RE: Get Dialog from Collection by a giving Dialog ID
    Replied by: Joseph Horvath on 16-12-2013 05:21:19 PM
    How are you getting access to the elements in the Collection? I would think that you would register an event on the Collection for the Dialog to be added to the Collection. Then, the function that is called gets the Dialog element itself. You should be able to call "getId()" on that object.

    Subject: RE: Get Dialog from Collection by a giving Dialog ID
    Replied by: Miguel Cubias on 16-12-2013 05:52:46 PM
    I'm trying to get access to the Collection on the "onClick" event of a button. in this function is passed the ID of the dialog I want to perform the action on. Also, I have a registered function on the "onCollectionAdd" callback as an object property for the user.getDialogs().
    in that function, I only get the ID of the Dialog with dialog.getId() (it works perfect there); But I want to perform the action in the onClick event of the button
    not in the onCollectionAdd.

    Here is my snippet code for the onClick event of the Button:

    handleAnswerCall : function(_pDialogId) {           
        console.log("CallManagement Answer Call :: ID :: " + _pDialogId); // Works Good. ID is received              
        dialogs = user.getDialogs(); // Here I want to get all the Dialog objects associated for this user           
        var oSelectedDialog = null;
        for (var oDialog in dialogs) {
              if (oDialog.getId() == _pDialogId) { // an Error is thrown here. getId is undefined
                   oSelectedDialog = oDialog;
              }           
        }           
        oSelectedDialog.requestAction(user.getExtension(), finesse.restservices.Dialog.Actions.ANSWER, {               
               success: function(oResponse) {                   
                    console.log("Success Status :: " + oResponse.status);                   
                    console.log("Success Content" + oResponse.content);               
               },                
               error: function(oErrorResponse) {                   
                    console.log("on Error Status" + oErrorResponse.status);                    
                    console.log("Error content" + oErrorResponse.content);                   
                    console.log("Error Type" + oErrorResponse.error.errorType);                   
                    console.log("Error Message" + oErrorResponse.error.errorMessage);               
               }           
        });       
    }

    I need to do it this way because is in a button group (Answer, End, Transfer, etc)  in which the Agent will be able to perform all the option for the call (of a list of available calls)

    I hope to set myself clear and thanks for your reply.

    Subject: RE: Get Dialog from Collection by a giving Dialog ID
    Replied by: Joseph Horvath on 16-12-2013 08:31:44 PM
    Most likely your Dialog object is not loaded yet. Typically, one would setup a function to be called upon having the Finesse JavaScript library add a new Dialog to the User's Dialog Collection. This function gets called when Finesse knows that there is a new call. In this function, you should register another function that gets called when the Dialog object is actually loaded. That final function should be able to get the contents of the Dialog object.

    A very simple example follows:

     1if (!finesse)
     2    console.error ("Finesse library is not available");
     3    alert ("Finesse library is not available");
     4else
     5{
     6    var finesse = finesse || {};
     7    finesse.gadget = finesse.gadget || {};
     8    finesse.container = finesse.container || {};
     9
    10    finesse.gadget.Config = (function () {
    11        var _prefs = new gadgets.Prefs();
    12
    13       return {
    14                authorization: _prefs.getString("authorization"),
    15                host: _prefs.getString("host"),
    16                restHost: "localhost"
    17       };
    18    }());
    19
    20    finesse.modules = finesse.modules || {};
    21
    22    finesse.modules.FinesseTest = (function ($) {
    23
    24    var handleDialogLoad = function (dialog) {
    25        var id = dialog.getId ();
    26    };
    27
    28    var handleNewDialog = function (dialog) {
    29        dialog.addHandler ("load", handleDialogLoad);
    30    };
    31
    32    var handleUserLoad = function (user) {
    33        user.getDialogs (
    34            {
    35              onCollectionAdd : handleNewDialog
    36                    }
    37            );
    38    };
    39
    40    return {
    41      init : function () {
    42            var prefs = new gadgets.Prefs ();
    43            var id = prefs.getString ("id");
    44
    45            gadgets.window.adjustHeight ();
    46
    47            finesse.clientservices.ClientServices.init (finesse.gadget.Config);
    48
    49            user = new finesse.restservices.User (
    50                {
    51                  id: id,
    52                        onLoad : handleUserLoad
    53                        }
    54                );
    55        }
    56    };
    57}(jQuery));
    58}


    Subject: RE: Get Dialog from Collection by a giving Dialog ID
    Replied by: Juan Othmaro Menjivar on 17-12-2013 01:27:46 PM
    Thank you for replying back Joseph.

    We've already implemented the logic to listen and handle incoming Dialogs,  we are basically listing them for an Agent and then he being able anwser them on demand by picking them from that list.
    Our current issue is when the Agent picks a call and we need to recover its information, we're trying to match the selected Dialog ID against the Dialog.getId() from the Dialogs collection. Bellow I'm copying part of our code for reference:

     dialogs = user.getDialogs(); // Here we want to get all the Dialog objects associated to this user      
     1:   var oSelectedDialog = null;
     2:   for (var oDialog in dialogs) {
     3:        if (oDialog.getId() == _pDialogId) {  // an Error is thrown here. getId saing that this property is undefined, 
    _pDialogId is our variable of the picked call

    The only way we've been able to implement this is creating our internal array of dialogs and manage it as a global variable.

    Hope this helps to clarify our point.

    Juan




    Subject: RE: Get Dialog from Collection by a giving Dialog ID
    Replied by: Juan Othmaro Menjivar on 17-12-2013 02:23:28 PM
    Ok thank you.

    User objset is set during the init method.

    According to dialog count we get the correct number of dialogs "available" just before checking .getId()

    Is there know way to pull all existing Dialogs for an user?
    Shouldn't this method return the list of all dialogs or is it limited to certain state/status?

    Subject: RE: Get Dialog from Collection by a giving Dialog ID
    Replied by: Joseph Horvath on 17-12-2013 02:08:32 PM
    Be careful with these JavaScript objects. This "user" object may be a freshly created object that does not know anything about any existing Dialogs. Check the length of the returned Dialog Collections and ensure that there is a Dialog there to iterate over before you actually iterate. Otherwise, the object that you are calling "getId()" on may not be a Dialog object.

    Subject: RE: Get Dialog from Collection by a giving Dialog ID
    Replied by: Gary Olmsted on 17-12-2013 09:37:37 PM
    The Dialogs is an object based on the RestCollectionBase.  It is not a collection itself.  You have to call a method getCollection() to get the collection of Dialog objects from it.  We did a lot of work in 10.0 (available now) to clean up our JS Documentation.  You can get the new stuff from DevNet.  Try something like this:
        /**
         * Handler for when GET Dialogs has loaded
         *
         */
        _handleDialogsLoaded = function() {
            var dialogCollection, dialogId;
            //Render any existing dialogs
            dialogCollection = _dialogs.getCollection();
            for (dialogId in dialogCollection) {
                if (dialogCollection.hasOwnProperty(dialogId)) {
                    _handleDialogAdd(dialogCollection);
                }
            }
        }

    Subject: RE: Get Dialog from Collection by a giving Dialog ID
    Replied by: Juan Othmaro Menjivar on 18-12-2013 10:28:10 AM
    Thank you Gary.

    That definetelly solved the issue, we've tested it and it worked fine!

    We'll also check on the new API version (v10.0) and test it. A better documentation would be great!

    Appreciate your collaboration.