Adding in work_notes

Aditya2
Kilo Expert

Hi all,

I have a requirement where if I add worknotes on change form if anyone adds or removes incidents from incidents caused by change related tab on change record.

Like if I add/remove incident

worknotes - XXXX incident added/removed.

Thanks

1 ACCEPTED SOLUTION

Jan Cernocky
Tera Guru

Hi Aditya,

you need to set an AFTER UPDATE BR on [incident] table.

The condition would be

current.caused_by.changes()

and the script may look like this (you need to cover all 3 scenarios - adding INC, removing INC and changing 'caused by' change on INC form) caused. If a field is empty the getValue returns null (false), hence the condition for operation like isAddition, isDeletion can use the boolean check for true/false.

(function executeRule(current, previous /*null when async*/) {	
	
	var oldChange = previous.getValue('caused_by');
	var newChange = current.getValue('caused_by');
	
	var ticket = current.getDisplayValue();
			
	var isDeletion = oldChange &&  !newChange;
	var isAddition = !oldChange && newChange;
	var isChanging = oldChange && newChange;
	
	if(isDeletion) {		
		var gr = new GlideRecord('change_request');
		if (gr.get(oldChange)) {
			gr.work_notes = ticket + ' has been deleted';
			gr.update();
		}		
	}
	
	if(isAddition) {		
		var gr = new GlideRecord('change_request');
		if (gr.get(newChange)) {
			gr.work_notes = ticket + ' has been added';
			gr.update();
		}			
	}
	
	if(isChanging) {		
		var gr1 = new GlideRecord('change_request');
		if (gr1.get(oldChange)) {
			gr1.work_notes = ticket + ' has been deleted';
			gr1.update();
		}	
		var gr2 = new GlideRecord('change_request');
		if (gr2.get(newChange)) {
			gr2.work_notes = ticket + ' has been added';
			gr2.update();
		}							
	}	

})(current, previous);

View solution in original post

3 REPLIES 3

AnirudhKumar
Mega Sage
Mega Sage

Create a Business Rule.

It should look like this:

 

find_real_file.png

 

 

In the script add this:

 

(function executeRule(current, previous /*null when async*/) {

	var chg = new GlideRecord("change_request");
	chg.addQuery("sys_id", current.caused_by);
	chg.query();
	if (chg.next()) {
		if(current.operation() == 'insert')
			chg.work_notes = current.number + ' has been added';
		else
			chg.work_notes = current.number + ' has been deleted';


chg.update();
	}


})(current, previous);

Anirud,

your BR will not work, because

a. it is on INSERT and DELETE, but the incident is not being inserted nor deleted, it is in fact updated (caused_by) field value is being added, removed or changed (+it is better to use AFTER rahter than BEFORE since you are touching another record and you only want to touch it if the previous operation completed successfully)

b. as per the logic in A, your IF/ELSE statement will not work

c. you missed chg.update()

d. current.name in your if statement is for some reason actually making the script not running (I experienced it on my own) ...  current.getValue('name') or current.getDisplayValue() is better in this case

Jan Cernocky
Tera Guru

Hi Aditya,

you need to set an AFTER UPDATE BR on [incident] table.

The condition would be

current.caused_by.changes()

and the script may look like this (you need to cover all 3 scenarios - adding INC, removing INC and changing 'caused by' change on INC form) caused. If a field is empty the getValue returns null (false), hence the condition for operation like isAddition, isDeletion can use the boolean check for true/false.

(function executeRule(current, previous /*null when async*/) {	
	
	var oldChange = previous.getValue('caused_by');
	var newChange = current.getValue('caused_by');
	
	var ticket = current.getDisplayValue();
			
	var isDeletion = oldChange &&  !newChange;
	var isAddition = !oldChange && newChange;
	var isChanging = oldChange && newChange;
	
	if(isDeletion) {		
		var gr = new GlideRecord('change_request');
		if (gr.get(oldChange)) {
			gr.work_notes = ticket + ' has been deleted';
			gr.update();
		}		
	}
	
	if(isAddition) {		
		var gr = new GlideRecord('change_request');
		if (gr.get(newChange)) {
			gr.work_notes = ticket + ' has been added';
			gr.update();
		}			
	}
	
	if(isChanging) {		
		var gr1 = new GlideRecord('change_request');
		if (gr1.get(oldChange)) {
			gr1.work_notes = ticket + ' has been deleted';
			gr1.update();
		}	
		var gr2 = new GlideRecord('change_request');
		if (gr2.get(newChange)) {
			gr2.work_notes = ticket + ' has been added';
			gr2.update();
		}							
	}	

})(current, previous);