UI action - create requested item from an incident

mitzaka
Mega Guru

Hi SNC,

I want to create a RITM from an incident, using an UI action. Below is my code, my questions are in red:

createRITM();

// update the record once, after the function call

current.active = false;

current.state = 9; //setting it to closed state

current.updateWithReferences();

  function createRITM() {               //create the new Requested Item Record

  var newReq = new GlideRecord("sc_req_item");

  newReq.initialize();

  newReq.opened_by=current.opened_by;

  newReq.assignment_group='4e868fe1508381002cf0066911d9982c';

  newReq.cmdb_ci=current.cmdb_ci;

  newReq.u_business_service=current.u_business_service;

  newReq.u_customer_defined_urgency=current.u_customer_defined_urgency;

  newReq.variables=current.variables; //I am storing catalog item variables in a formatter, called Requested Item Variable Editor, and I wanted to transfer the variables from the incident into the RITM, but it does not happen

  newReq.description=current.description;

  newReq.short_description=current.short_description;

  newReq.contact_type='2';

  newReq.state=current.state;

  newReq.priority=current.priority;

  newReq.watch_list=current.watch_list.getDisplayValue().toString();

  newReq.parent=current.number; //I can't get this working - cannot populate the Parent field in the RITM with the ID of the incident

  newReq.insert();

  // now direct to the new Request

  gs.addInfoMessage("Incident closed, Requested Item "   + newReq.number + " created.");

action.setRedirectURL(newReq);

action.setReturnURL(current);

  }

Would you throw a sanity check eye on this?

Thanks!

16 REPLIES 16

Hi Dimitar,



That might be because 'current' is an object and it will not return sys_id but undefined/null.



Please try with


newReq.parent=current.sys_id;




This should work




Thanks,


Tanaji


Oh Geez... I found where I was going wrong. I had tried with 'newReq.parent=current.sysID', but it was a typo.. it should have been 'newReq.parent=current.sys_id'



Tried and it worked now. Parent is populated.One down!



My only remainder now is to copy the variables from the variable editor of the Requested Item into the Variable Editor of the Incident (when I convert the Requested Item into an incident). I tried what's in that link: Copying variables field from Requested Item to a new record



But I still can't get the code right for my conversion - RITM to Incident, and vice-versa.


Record producer uses a different table to store the variables value .. It is question_answer table ... Try changing your code to insert records into that table...


Hi Dimitar,



Can you try with the below shown code. I have modified/added few lines of code.


This code will copy your variables form INC to RITM. To do vice-versa you need to reverse the logic written. That is you need to query the 'sc_item_option_mtom' table, go to respective records on the 'sc_item_option' table and depending on these 'sc_item_option' records create new records in 'question_answer' table for the newly created INC.



//create the new Requested Item Record


              var newReq = new GlideRecord("sc_req_item");


              newReq.initialize();


              newReq.opened_by=current.opened_by;


              newReq.assignment_group='4e868fe1508381002cf0066911d9982c';


              newReq.cmdb_ci=current.cmdb_ci;


              newReq.u_business_service=current.u_business_service;


              newReq.u_customer_defined_urgency=current.u_customer_defined_urgency;


            //newReq.variables=current.variables;


              newReq.description=current.description;


              newReq.short_description=current.short_description;


              newReq.contact_type='2';


              newReq.state=current.state;


              newReq.priority=current.priority;


              newReq.watch_list=current.watch_list.getDisplayValue().toString();


              newReq.parent=current.sys_id;


              var newRITM = newReq.insert();




  var questionAnswer= new GlideRecord('question_answer');


  questionAnswer.addQuery('table_sys_id', current.sys_id);


  questionAnswer.query();


  while(questionAnswer.next()) {



  var newScItemOption = new GlideRecord('sc_item_option');


  newScItemOption.initialize();


  newScItemOption.item_option_new = questionAnswer.question;


  newScItemOption.value = questionAnswer.value;


  newScItemOption.order = questionAnswer.order;


  var scOption = newScItemOption.insert();



  var newMtom = new GlideRecord('sc_item_option_mtom');


  newMtom.initialize();


  newMtom.request_item =   newRITM;


  newMtom.sc_item_option = scOption;


  newMtom.insert();


  }



// now direct to the new Requested Item


gs.addInfoMessage("Incident closed, Requested Item "   + newReq.number + " created.");        


action.setRedirectURL(newReq);


action.setReturnURL(current);



Hope this gives you required output



Thanks,


Tanaji


Actually I tried but nothing came over in the Incident Variable Editor tab...