Tony Chatfield1
Kilo Patron

Sharing this as I haven't seen it mentioned anywhere, and new users may find it helpful.

Updating a record over and over (and over) in order to trigger\test BR code can be frustrating,
especially early in your developer journey when you are more likely to experience small syntax errors.
Lucky there is a very simple way to test BR code in the background script window,
as it is possible to instantiate a variable object called 'current' and assign an existing (reference) record to it.
This allows you to easily test the tricky stuff like, dot.walking and updating related records, manipulating dateTime,
and also test OOB (or other users) code without having to find\replace 'current' in the script before you start\finish.

It's not perfect and may not suit all circumstances, as you cannot instantiate a 'previous' object,
but I find it quick\easy and use it often.
Method changes() works, but only if you set a field change in your script (you don't have to save\commit it).

  • I haven't had reason to test methods other than changes(), so please validate any before use.

All you need is GlideRecord get()

var current = new GlideRecord('targetTable);
current.get('targetRecSys_id');

But make sure your reference record actually exists and is returned correctly,
as using get() without validating the result can\will cause unexpected behavior if your reference record is not found.

Example with OOB Change Request CHG0000001

//Instantiate the 'current' object
var current = new GlideRecord('change_request');
current.get('a9e9c33dc61122760072455df62663d2');

//Update short_description for testing field change()
current.short_description = current.short_description + ' test';

//Code from your BR
if(current.short_description.changes()) {
//Do something
	gs.info('My Change Request ' + current.number);

	var gr = new GlideRecord('change_task');
	gr.addQuery('change_request', current.sys_id);
	gr.query();

	while(gr.next()){
		gs.info("Change Task " + gr.number + " | assigned to " + gr.getDisplayValue('assigned_to'));

		gr.planned_start_date = current.start_date;
		gr.update();
	}
}
Version history
Last update:
‎08-28-2021 12:58 AM
Updated by: