Topic
  • 4 replies
  • Latest Post - ‏2013-07-24T16:18:38Z by Dave_Ward
GilesMetcalf
GilesMetcalf
3 Posts

Pinned topic Task ID in REST from JavaScript Adaptor

‏2013-07-04T15:21:27Z |

Gurus all,

I need to add some functionality to force a user to add a comment to a task when they complete it. There is some really helpful code already available in DW that explains how to add a comment to a case (https://www.ibm.com/developerworks/community/blogs/e8206aad-10e2-4c49-b00c-fee572815374/entry/how_to_make_comments_required_for_a_task_step_response8?lang=en), but I need to add the comment to the task... Some groping around in the documentation and I know what parameters I need to use for a task comment, but one of these is the Task ID value. This is not available in the completion payload, so I need to retrieve it using a REST call in a JavaScript adaptor.

This is where I run into trouble. I believe I can use the REST call ../CaseManager/CASEREST/v1/case/{case ID}/tasks?TargetObjectStore={object store}&Grouping=ROD to get the list of task objects. I only have one Required task (the one I want the ID for), so in theory, getting the ID should be easy...

However, this is where my puny JavaScript skills abandon me...

What I have got so far is:

var caseID = payload.caseFolderId;

caseID = caseID.substr(1,caseID.length-2);   

var note = "";
if (payload.responseName == "Start investigation")
   return payload;

else if (note = prompt("Please enter a comment for this response:"))            
{        
   var serverBase = window.location.protocol + "\/\/" + window.location.host;

   var propURL = this.iContext.io.rewriteURI(serverBase + "/CaseManager/CASEREST/v1/case/" + caseID+ "/tasks?TargetObjectStore="+ecmwdgt.getBean("spaceConfig").getTargetOS() + "&Grouping=ROD");

   var xhrGetArgs = {            
    url: propURL,            
    handleAs: "json",            
    headers: { "Content-Type": "application/json"},            
        load: function(jsonData){} ,            
        error: function(error)            
           {            
              alert ("Oops - Get error!.. " + error);            
           }                       
    };            

   var taskProps = dojo.xhrGet(xhrGetArgs);
   var taskObj = JSON.parse(taskProps);
   var taskId = taskObj.Required.TaskId;

   alert("Object count; " + taskObj.count); // Debug check

   var feedURL = this.iContext.io.rewriteURI(serverBase + "/CaseManager/CASEREST/v1/case/" + caseID+ "/comments? TargetObjectStore="+ecmwdgt.getBean("spaceConfig").getTargetOS());          
      
   var comment= {
    "CommentType":"Task",
    "CommentContext":202,
    "CommentText": note,
    "ItemId": taskId
    };            
            
   var xhrArgs = {            
    url: feedURL,            
    postData: dojo.toJson(comment),            
    handleAs: "json",            
    headers: { "Content-Type": "application/json"},            
        load: function(data){} ,            
        error: function(error)            
           {            
              alert ("Oops..." + error);            
           }            
    };    
        
   dojo.xhrPost(xhrArgs);            
   return payload;   
}

But it is throwing an (unspecified) error at the point where it tries to resolve the TaskID - so I am clearly doing something very wrong...

can anyone enlighten me what I should be doing to get the task ID, and if possible, post some sample code, please?

Thanks in advance!

Cheers,

      Giles.

 

  • Dave_Ward
    Dave_Ward
    38 Posts

    Re: Task ID in REST from JavaScript Adaptor

    ‏2013-07-12T09:47:52Z  

    Hi Giles,

    I'm no expert either, so I'm sure there are better ways to do it than this.
    I've been using this sort of code which has been working fine for me.
    I'll use your code as a start point and add a few lines / comments along the way...
      
    var myPayload;                                                          // Declare var to hold returned payload
    var xhrGetArgs = {           
        url: propURL,           
        sync: true,                                                               // Wait for the call to finish before executing any more JS
        preventCache: true,                                              // Never use cached version of call
        handleAs: "json",           
        headers: { "Content-Type": "application/json"},           
        load: function(jsonData)
            {
                myPayload = jsonData;                                // Assign the returned payload to your var
            } ,           
        error: function(error)           
            {           
                alert ("Oops - Get error!.. " + error);           
            }                      
    };   

    var deferred = dojo.xhrGet(xhrGetArgs);               // Assign the return from the call to some dummy variable

    console.log (myPayload);                                       // Dump out the contents of the payload to Firebug so you can confirm its structure

    var taskId = myPayload.Required[0].TaskId;       // Required returns as an array, so get 1st elements TaskId

    taskId = taskId.substr(1,taskId.length-2);            // Strip off curly braces

    //Add the comment

    By the way, I use these URLs as reference for xhrGet, xhrPost and xhrPut:
    http://livedocs.dojotoolkit.org/dojo/xhrGet
    http://livedocs.dojotoolkit.org/dojo/xhrPost
    http://livedocs.dojotoolkit.org/dojo/xhrPut

    Good luck!

    Dave.

  • GilesMetcalf
    GilesMetcalf
    3 Posts

    Re: Task ID in REST from JavaScript Adaptor

    ‏2013-07-12T10:54:37Z  
    • Dave_Ward
    • ‏2013-07-12T09:47:52Z

    Hi Giles,

    I'm no expert either, so I'm sure there are better ways to do it than this.
    I've been using this sort of code which has been working fine for me.
    I'll use your code as a start point and add a few lines / comments along the way...
      
    var myPayload;                                                          // Declare var to hold returned payload
    var xhrGetArgs = {           
        url: propURL,           
        sync: true,                                                               // Wait for the call to finish before executing any more JS
        preventCache: true,                                              // Never use cached version of call
        handleAs: "json",           
        headers: { "Content-Type": "application/json"},           
        load: function(jsonData)
            {
                myPayload = jsonData;                                // Assign the returned payload to your var
            } ,           
        error: function(error)           
            {           
                alert ("Oops - Get error!.. " + error);           
            }                      
    };   

    var deferred = dojo.xhrGet(xhrGetArgs);               // Assign the return from the call to some dummy variable

    console.log (myPayload);                                       // Dump out the contents of the payload to Firebug so you can confirm its structure

    var taskId = myPayload.Required[0].TaskId;       // Required returns as an array, so get 1st elements TaskId

    taskId = taskId.substr(1,taskId.length-2);            // Strip off curly braces

    //Add the comment

    By the way, I use these URLs as reference for xhrGet, xhrPost and xhrPut:
    http://livedocs.dojotoolkit.org/dojo/xhrGet
    http://livedocs.dojotoolkit.org/dojo/xhrPost
    http://livedocs.dojotoolkit.org/dojo/xhrPut

    Good luck!

    Dave.

    Thanks Dave! I'm not on site today, but I'll give this a go on Monday when I get back. I'll let you know how it went...

    Giles.

  • GilesMetcalf
    GilesMetcalf
    3 Posts

    Re: Task ID in REST from JavaScript Adaptor

    ‏2013-07-18T13:28:51Z  

    Thanks Dave! I'm not on site today, but I'll give this a go on Monday when I get back. I'll let you know how it went...

    Giles.

    The code snippet work just fine. Thanks!

    However, I am still not quite there yet, as I now have not yet managed to add a task comment successfully. This is the full JavaScript code that I wrote in an attempt to do this. It seems to work, in so far as no errors are returned, but no comment appears. I am still clearly doing something wrong. Can anyone advise?

    Cheers,

        Giles.

    // Get the case folder ID and strip off braces

    var caseID = payload.caseFolderId;        
    caseID = caseID.substr(1,caseID.length-2);           

    // Don't prompt for comments when starting investigation
            
    var note = "";         
    if (payload.responseName == "Start investigation")        
       return payload;        

    // Otherwise, prompt for a comment
            
    else if (note = prompt("Please enter a comment for this response:"))                    
    {
       // Build the common start of the URLs, and get the OS name
                    
       var serverBase = window.location.protocol + "\/\/" + window.location.host;     
       var myTOS = ecmwdgt.getBean("spaceConfig").getTargetOS();  

       // REST call to get task list
     
       var propURL = this.iContext.io.rewriteURI(serverBase + "/CaseManager/CASEREST/v1/case/" + caseID+ "/tasks?TargetObjectStore="+ myTOS + "&Grouping=ROD");         
       var myPayload;      
       var xhrGetArgs = {                
          url: propURL,                
          sync: true,
          preventCache: true,
          handleAs: "json",                
          headers: { "Content-Type": "application/json"},                
          load: function(jsonData)     
            {     
                myPayload = jsonData;     
            },                
          error: function(error)                
            {                
                alert ("Task ID retrieval error: " + error);                
            }                           
        };        
        
       var deferred = dojo.xhrGet(xhrGetArgs);   

       // Pick out the Task ID value from th payload, and strip off braces
     
       var taskId = myPayload.Required[0].TaskId;
       taskId = taskId.substr(1,taskId.length-2);

       // Build the comment payload

       var myComment = {"CommentType":"Task","CommentContext":202,"CommentText":note,"ItemId":taskId};     
         
       // Make POST call to REST service to add task comment

       var feedURL = this.iContext.io.rewriteURI(serverBase + "/CaseManager/CASEREST/v1/case/" + caseId + "/comments?TargetObjectStore=" + myTOS);          
       var xhrArgs = {          
         url:feedURL,         
         postData:dojo.toJson(myComment),        
         handleAs:"json",        
         headers:{"Content-Type":"application/json"},       
         load:function(data){},       
         error:function(error){alert ("Task comment REST error: " + error);}           
       };         
       dojo.xhrPost(xhrArgs);

       // Finally, return the original payload
             
       return payload;          
    }     

            
     

     

  • Dave_Ward
    Dave_Ward
    38 Posts

    Re: Task ID in REST from JavaScript Adaptor

    ‏2013-07-24T16:18:38Z  

    The code snippet work just fine. Thanks!

    However, I am still not quite there yet, as I now have not yet managed to add a task comment successfully. This is the full JavaScript code that I wrote in an attempt to do this. It seems to work, in so far as no errors are returned, but no comment appears. I am still clearly doing something wrong. Can anyone advise?

    Cheers,

        Giles.

    // Get the case folder ID and strip off braces

    var caseID = payload.caseFolderId;        
    caseID = caseID.substr(1,caseID.length-2);           

    // Don't prompt for comments when starting investigation
            
    var note = "";         
    if (payload.responseName == "Start investigation")        
       return payload;        

    // Otherwise, prompt for a comment
            
    else if (note = prompt("Please enter a comment for this response:"))                    
    {
       // Build the common start of the URLs, and get the OS name
                    
       var serverBase = window.location.protocol + "\/\/" + window.location.host;     
       var myTOS = ecmwdgt.getBean("spaceConfig").getTargetOS();  

       // REST call to get task list
     
       var propURL = this.iContext.io.rewriteURI(serverBase + "/CaseManager/CASEREST/v1/case/" + caseID+ "/tasks?TargetObjectStore="+ myTOS + "&Grouping=ROD");         
       var myPayload;      
       var xhrGetArgs = {                
          url: propURL,                
          sync: true,
          preventCache: true,
          handleAs: "json",                
          headers: { "Content-Type": "application/json"},                
          load: function(jsonData)     
            {     
                myPayload = jsonData;     
            },                
          error: function(error)                
            {                
                alert ("Task ID retrieval error: " + error);                
            }                           
        };        
        
       var deferred = dojo.xhrGet(xhrGetArgs);   

       // Pick out the Task ID value from th payload, and strip off braces
     
       var taskId = myPayload.Required[0].TaskId;
       taskId = taskId.substr(1,taskId.length-2);

       // Build the comment payload

       var myComment = {"CommentType":"Task","CommentContext":202,"CommentText":note,"ItemId":taskId};     
         
       // Make POST call to REST service to add task comment

       var feedURL = this.iContext.io.rewriteURI(serverBase + "/CaseManager/CASEREST/v1/case/" + caseId + "/comments?TargetObjectStore=" + myTOS);          
       var xhrArgs = {          
         url:feedURL,         
         postData:dojo.toJson(myComment),        
         handleAs:"json",        
         headers:{"Content-Type":"application/json"},       
         load:function(data){},       
         error:function(error){alert ("Task comment REST error: " + error);}           
       };         
       dojo.xhrPost(xhrArgs);

       // Finally, return the original payload
             
       return payload;          
    }     

            
     

     

    Hi Giles,

    When you say a comment doesn't appear, where are you looking? In the "Comments" form I guess? Does anything show in the Case History?

    The reason I ask is that I thought I would try the "Add Comment" right-click menu in the Inbasket widget with Firebug enabled to see what was happening from an API point of view.

    If looks like this queries / creates comments in the "Work Item" context and not in the "Task" context, so if you are expecting to see comments in the "Comments" form, I think you need to use the Work Item context.

    Regards,

    Dave.