Get RITM number through sys_id

Dafni Tsipra
Giga Contributor

Hello everyone!

I have a workflow in which a RITM2 is generated and I store its number to a workflow.scratchpad variable.

On the same script but later I want to write this RITM2's number to a work note of the current RITM1 but it doesn't appear.

I used gs.addInfoMessage and it shows RITM2's number correct but still work notes are not updated correct.

Here is the code:

//GET RITM2'S NUMBER

var childReqIt = new GlideRecord('sc_req_item');
childReqIt.addQuery('sys_id', workflow.scratchpad.RITM_SYSID);
childReqIt.query();

gs.addInfoMessage('RITM_SYSID ' + workflow.scratchpad.RITM_SYSID);

while (childReqIt.next()) {
gs.addInfoMessage( childReqIt.number);
}

 

//WRITE WORK NOTES ON RITM1


var parentReqIt = new GlideRecord('sc_req_item');
parentReqIt.addQuery('sys_id', current.sys_id);
parentReqIt.query();


while (parentReqIt.next()) {
parentReqIt.work_notes = (childReqIt.number + 'created' ) ;
ParentReqIt.update();

}

 

I would really appreciate any help!

1 ACCEPTED SOLUTION

@Dafni Tsipra 

So it should be like this

// your original code here

var rc = cart.placeOrder(); // this gives you REQ record

var ritm = new GlideRecord('sc_req_item');

ritm.addQuery('request.number', rc.number);

ritm.query();

if(ritm.next()){

workflow.scratchpad.ritmNumber = ritm.number;

}

/WRITE WORK NOTES ON RITM1


var parentReqIt = new GlideRecord('sc_req_item');
parentReqIt.addQuery('sys_id', current.sys_id);
parentReqIt.query();

if(parentReqIt.next()) {
parentReqIt.work_notes = workflow.scratchpad.ritmNumber + ' created' ;
ParentReqIt.update();

}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

10 REPLIES 10

Jim Coyne
Kilo Patron

Couple questions:

1. where is this script running?  In a "Run Script" Activity within the Workflow?

2. why are you searching for the child and then the parent records?  That's extremely inefficient

 

When you create the 2nd RITM, save the number in a variable within your script so you can put it in the Work notes.  Instead of:

//WRITE WORK NOTES ON RITM1

var parentReqIt = new GlideRecord('sc_req_item');
parentReqIt.addQuery('sys_id', current.sys_id);
parentReqIt.query();


while (parentReqIt.next()) {
  parentReqIt.work_notes = (childReqIt.number + 'created' ) ;
  parentReqIt.update();
}

You should be able to use something like:

current.work_notes = childNumber + " created";  //childNumber being the variable that contains the Number from when you created the 2nd RITM earlier
current.update();

No need to search for any RITMs.