Copy RITM Short Description to REQ Short Description (Business Rule)

TempestT-37
Tera Contributor

I am trying to copy the RITM Short Description to REQ Short Description. I have 'when to run' set to async - insert. I have tried both before and after with little success.  Action has nothing set. in the advanced tab my code is below. This code will work on some of my request but not most of them. Thank for any help.

 

var gr = new GlideRecord('sc_request');
gr.addEncodedQuery('short_descriptionISEMPTY');
gr.query();

while (gr.next()){
	var gRitm = new GlideRecord('sc_req_item');
	gRitm.addQuery('request',gr.sys_id);
	gRitm.query();
	while(gRitm.next()){
		gr.short_description = gRitm.short_description;
		gr.update();
	}
}

 

 

 

6 REPLIES 6

Punit S
Giga Guru

It looks like your script should copy the RITM Short Description to REQ Short Description for all requests where the REQ Short Description field is empty. However, you mentioned that the code is not working for some requests.

One possible reason for this could be that some of the requests already have a value in the REQ Short Description field, which means that the script won't copy the RITM Short Description to that field.

To fix this issue, you can modify the script to check if the REQ Short Description field is empty or not before copying the RITM Short Description. Here's an updated version of your script that should work:

var gr = new GlideRecord('sc_request');
gr.addEncodedQuery('short_descriptionISEMPTY');
gr.query();

while (gr.next()){
    if (gr.getValue('short_description') == '') {
        var gRitm = new GlideRecord('sc_req_item');
        gRitm.addQuery('request',gr.sys_id);
        gRitm.query();
        while(gRitm.next()){
            gr.short_description = gRitm.short_description;
            gr.update();
        }
    }
}

 

This updated script checks if the REQ Short Description field is empty by calling getValue('short_description') on the gr  GlideRecord object. If the field is empty, the script proceeds to copy the RITM Short Description to the field.

Also note that since you're using "async - insert" as the "When to run" option, the script will only run when a new record is inserted into the sc_request table, and not when an existing record is updated. If you want the script to update existing records as well, you should change the "When to run" option to "async - update".

 

Please mark my answer correct/helpful in case it adds value and moves you a step closer to your desired ServiceNow solution goal. 

Thanks,
Punit

Thank you for the quick reply. 

Having an if statement is a good idea, but the REQ short Description is still not getting populated and is still blank. In my testing, I am creating a new RITM and a new REQ, and when I check the REQ record the short description is empty, sometime it will populate past requests with the right data but still leaves the current REQ blank. 

Bert_c1
Kilo Patron

I guess your code is in a Business Rule that runs 'async' and on 'Insert', defined on the sc_req_item table? Please be clear.  As you script seems meant to run elsewhere.  I tried your script logic:

 

var gr = new GlideRecord('sc_request');
gr.addEncodedQuery('short_descriptionISEMPTY');
gr.query();

while (gr.next()){
	var gRitm = new GlideRecord('sc_req_item');
	gRitm.addQuery('request',gr.sys_id);
	gRitm.query();
	while(gRitm.next()){
		gr.short_description = gRitm.short_description;
		gs.info("Will set sd: " + gRitm.short_description + " on: " + gr.number + ".");
//		gr.update();
	}
}

 

and that worked for me when I un-commented the 'gr.update();' and ran that in Scripts - Background

I have this code in the advanced area of an business code. I am using async and on insert. these settings are the only ones that seem to work sometimes. I have not had any success with before or after.

In my flow I am doing a 'update request item record' sc_req_item, in there I am setting the short description to values entered into the request. This works every time for the sc_req_item. I want to copy the sc_req_item short description to the sc_request short description and was hoping the business rule was the way to do that. This code works sometimes on a couple of requests but for most of the requests it does not work. I am not sure if something is overwriting it or if it is when. Most of the time what is entered in the sc_request short description is the default short description of the catalog. Thank again