Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Clear RITM variable data

Henrik Jutterst
Kilo Sage

Hi there,

I'm looking for a way to erase the values of a variable in a RITM that is more then 90 days old. After a year from creation I want to delete the RITM (but that's no issue since I assume that all related variables will be deleted when the RITM is deleted)

So, if a RITM variable is called 'user' I'm looking for a way to clear that value if the RITM is more then 90 days old. This will be a scheduled activity in some way.

Any ideas?

1 ACCEPTED SOLUTION

Hi and thanks for the feedback.

I ended up with this script but if was never really put into production. If it's helpful to anyone feel free to use it:

 

//add variable question into function below to clear it
clearVariable('Name');


function clearVariable(variableName){

	try{

		var ritmSysID;
		var ritm = new GlideRecord('sc_req_item');
		ritm.addQuery('cat_item.name','Background check');
		ritm.query();

		while(ritm.next()){
			ritmSysID = ritm.sys_id;
			//gs.print('ritmSysID: ' + ritmSysID);

			//use this sys_id to look up variable
			var varTable = new GlideRecord('sc_item_option_mtom');
			varTable.addQuery('request_item',ritmSysID);
			varTable.addQuery('sc_item_option.item_option_new.question_text',variableName);
			varTable.query();

			while(varTable.next()){
				gs.print('Q: ' + varTable.sc_item_option.item_option_new.question_text + ' A: ' + varTable.sc_item_option.value + ' ');
				varTable.sc_item_option.value = '';
				varTable.sc_item_option.update();
			}
		}
	}
	
	catch(ex){
		gs.info('Exception is: ' + ex);	
	}
}

 

Kind Regards

View solution in original post

9 REPLIES 9

SatheeshKumar
Kilo Sage

I think you need delete the entries  from sc_item_option_mtom by passing  Parent  (Ritm number).

The related variables are stored as dependent items.

find_real_file.png

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Henrik,

you can have a scheduled job for this; which runs daily and Run as System Admin and have following script

variable values are stored in sc_item_option_mtom table; try running this first by giving one query as

ritmRec.addEncodedQuery('number=RITM0001');

then visit the sc_item_option_mtom table and check records exist or not

clearValues();

function clearValue(){

try{

var ritmRec = new GlideRecord('sc_req_item');
ritmRec.addEncodedQuery('sys_created_on<javascript:gs.beginningOfLast3Months()');
ritmRec.query();
while(ritmRec.next()){

var ritmSysId = ritmRec.sys_id;

var gr = new GlideRecord('sc_item_option_mtom');
gr.addQuery('request_item', ritmSysId);
gr.query();
while(gr.next()){
gr.deleteRecord();
}

}

}
catch(ex){
gs.info('Exception is: ' + ex);
}


}

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

Hi Ankur and big thanks for the feedback!

Regarding the [sc_item_option_mtom] table query. Do you know if there is a record for each variable related to a RITM?

Let's say that I want to clear (not delete) what is written into a variable called 'user'. How is that done?

Hi Henrik,

So if you want to clear the value only then it is stored in this table; update code as below and test once

clearValues();

function clearValue(){

try{
var ritmRec = new GlideRecord('sc_req_item');
ritmRec.addEncodedQuery('sys_created_on<javascript:gs.beginningOfLast3Months()');
ritmRec.query();
while(ritmRec.next()){

var ritmSysId = ritmRec.sys_id;

var gr = new GlideRecord('sc_item_option_mtom');
gr.addQuery('request_item', ritmSysId);
gr.query();
while(gr.next()){
var refRecord = gr.sc_item_option.getRefRecord();
refRecord.value = '';
refRecord.update();
}

}
}
catch(ex){
gs.info('Exception is: ' + ex);
}


}

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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