Query SCTASK from Request

Bryan Cole
Kilo Expert

I'm deploying a UI action that allows users to convert an INC to a Request item in the service catalog. Upon creation, I want to redirect the fulfiller to the newly created SCTASK (Note: Not the REQ or RITM).

function serverResolve(){

var cartId = GlideGuid.generate(null);

var cart = new Cart(cartId);

var item = cart.addItem('17c590e9047531006802a6e8a2359514'); // sys_ID for the request sc_catalog item

cart.setVariable(item, 'u_requested_for', current.u_requested_for);

cart.setVariable(item, 'description', current.comments.getJournalEntry(-1));

var rc = cart.placeOrder();

if(!rc){

gs.addInfoMessage("Error: Request was not created.");

}

//Resolve the current incident upon request creation

current.state = 6; //Resolved

current.close_code = "Closed/Resolved by Caller";

current.close_notes = "Converted to Request: " + rc.number;

current.update();

current.resolved_by = gs.getUserID();

// Retrieve SCTASK Number (Request -> SC Task ->Task number)

var sctask = new GlideRecord('sc_task');

sctask.addQuery('request_item', '=',rc.number);

sctask.query();

var task = new GlideRecord('task');

task.addQuery('number','=',sctask.number);

task.query(callback);

function callback(gr){

while (gr.next()){

gs.addInfoMessage("Request " + task.number + " created.");

}

}

gs.addInfoMessage("Incident " + incident.number + " has been resolved.");

action.setRedirectURL(task);

action.setReturnURL(current);

}

I know the issue is in 23-27, I'm probably querying things in the wrong order.

Thoughts?

7 REPLIES 7

Sharique Azim
Mega Sage

Hi Bryan,



Please rather use the code:



  1. var sctask = new GlideRecord('sc_task');  
  2. sctask.addQuery('request_item', rc);     //assuming rc stores the sys id of the RITM
  3. sctask.query()// else all values are returned
  4. if(sctask.next()){
  5. var task = new GlideRecord('task');  
  6. task.addQuery('number','=',sctask.number);  
  7. task.query(callback);  
  8. //var nu
  9.   }
  10. function callback(gr){  
  11. while (gr.next()){     // can be replaced by if
  12. gs.addInfoMessage("Request " + task.number + " created.");  
  13. //return gr.sys_id;
  14. }
  15. }  
  16.  
  17.  
  18. gs.addInfoMessage("Incident " + incident.number + " has been resolved.");  
  19. action.setRedirectURL("sc_task.do?sys_id=" +sctask.sys_id);  
  20. action.setReturnURL(current); // not required    
  21. }  



I hope this helps..


vinothkumar
Tera Guru

Hi Bryan,



I beleive issue is in the line no 24 rc.number, since you are querying the sc_task table with RITM number, however you are entering request number(REQ) over there instead of request item number(RITM)


veena_kvkk88
Mega Guru

Hello Bryan,



2 things:



1. rc is the object pointing to the REQ, not RITM. So, in your code line 24, replace 'request_item' with 'request'


2. After line 25 - sctask.query(), there's no statement that takes the object to point to the first record in the query.



If you add sctask.next() after line 25, and update line 24 as mentioned above, it should work.