Need to have 3 operation in 1 business rule and need validation on it.

Santoshi5
Giga Guru

Hi Team ,

I have created 2 separate business rule after record get inserted or updated in to my custom table i need some validation and when that record get deleted i need some validation,

I have created 2 separate Business rule 

First BR- When After -insert or update 

Second BR- When After -delete

This 2 BR's are working fine as per my requirement, but i am thinking to collage 2 BR in to Single BR and below is the code , but its seems not working anyone can help to correct me here ,

Combined BR- 1st+2nd

When- After -insert, update ,delete 

table- u_custom

Script-

if((current.insert==1)||(current.update==1))

{

gs.addinfoMessage('Insert or update operation is happened..');

}

else if(current.deleteRecord()==1)

{

gs.addinfoMessage('Delete operation is happened..');

}

 

Can anyone let me know is this correct approach which is am doing ??

I need the validation like when record is inserted in to the table field "u_xyz" check box should get True and when that record get deleted from the table checkbox should get false.

Thanks in advance!!!!!!

1 ACCEPTED SOLUTION

shloke04
Kilo Patron

Hi,

You can combine your both BR into a Single one. Create a After BR on your Custom table and make sure to check Insert, Update and Delete checkbox as True.

Now in your script you can handle like below:

if(current.operation() == 'insert'){
gs.addInfoMessage('Text here');
}else if(current.operation() == 'update'){
gs.addInfoMessage('Text here');
}else if(current.operation() == 'delete'){
gs.addInfoMessage('Text here');
}

 

Say for example, I have used a similar thing to update Reassignment count on Parent Incident record when child Incident task gets insert and deleted accordingly. Sharing a sample script for reference:

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

	// Add your code here
	
	
	if(current.operation() == 'insert'){
		updateReassignCOunt(current);
	}else if(current.operation() == 'delete'){
		decreasecount(current);
	}
	
	
	function updateReassignCOunt(current){
		var count = 0;
		var gr = new GlideRecord('incident');
		gr.addQuery('sys_id',current.incident);
		gr.query();
		if(gr.next()){
			count++;
			gr.reassignment_count = parseInt(gr.reassignment_count) + count;
			gr.update();
		}
	}
	
	function decreasecount(current){
		var count = 0;
		var gr = new GlideRecord('incident');
		gr.addQuery('sys_id',current.incident);
		gr.query();
		if(gr.next()){
			count++;
			gr.reassignment_count = parseInt(gr.reassignment_count) - count;
			gr.update();
		}
	}
	

})(current, previous);

 

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

View solution in original post

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can try to get the operation

current.operation() -> insert/update

But for delete if the record itself is not there then why u_xyz would be set to false?

Regards
Ankur

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

The filed is not available on the same table ,it is on different table 

when the operation is happened in the custom table i need to set the field true false in incident table.

Hi,

then as I mentioned you can use current.operation() to check if it's insert/update/delete

Accordingly have you code

if(current.operation() == 'insert'){

// insert logic

}

else if(current.operation() == 'update'){

// update logic

}

else if(current.operation() == 'delete'){

// delete logic

}

Regards
Ankur

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

shloke04
Kilo Patron

Hi,

You can combine your both BR into a Single one. Create a After BR on your Custom table and make sure to check Insert, Update and Delete checkbox as True.

Now in your script you can handle like below:

if(current.operation() == 'insert'){
gs.addInfoMessage('Text here');
}else if(current.operation() == 'update'){
gs.addInfoMessage('Text here');
}else if(current.operation() == 'delete'){
gs.addInfoMessage('Text here');
}

 

Say for example, I have used a similar thing to update Reassignment count on Parent Incident record when child Incident task gets insert and deleted accordingly. Sharing a sample script for reference:

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

	// Add your code here
	
	
	if(current.operation() == 'insert'){
		updateReassignCOunt(current);
	}else if(current.operation() == 'delete'){
		decreasecount(current);
	}
	
	
	function updateReassignCOunt(current){
		var count = 0;
		var gr = new GlideRecord('incident');
		gr.addQuery('sys_id',current.incident);
		gr.query();
		if(gr.next()){
			count++;
			gr.reassignment_count = parseInt(gr.reassignment_count) + count;
			gr.update();
		}
	}
	
	function decreasecount(current){
		var count = 0;
		var gr = new GlideRecord('incident');
		gr.addQuery('sys_id',current.incident);
		gr.query();
		if(gr.next()){
			count++;
			gr.reassignment_count = parseInt(gr.reassignment_count) - count;
			gr.update();
		}
	}
	

})(current, previous);

 

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke