Change the Value of State Field in two different tables using Business rule

Community Alums
Not applicable

Hi all,

I have two tables one is Sc_req_item
Second table is Sc_task
In this two tables i have a state field.

In the sc_task table the State field of  is Awaiting and watch list field is User Feedback .

After that if the comment is posted in Additional comment field in sc_req_item table .

The state field in Both the tables need to be changed to In Progress state.

solution script will be more helpful thanks in Advance.

@Sohail Khilji  

1 ACCEPTED SOLUTION

Bert_c1
Kilo Patron

@Community Alums 

 

why not review OOB business rules that perform similar actions. One is named "Update Idea State on Close" defined on the 'dmn_demand' table.

 

Screenshot 2024-04-22 095825.pngScreenshot 2024-04-22 095841.png

However, it is unclear on which table to define the BR on based on your requirements:

 

"In the sc_task table the State field of  is Awaiting and watch list field is User Feedback .

After that if the comment is posted in Additional comment field in sc_req_item table ."

 

Seems you need to define the BR on the 'sc_req_item' table, when the 'comments' field changes, the script would then check the 'sc_task' records that reference the same 'sc_request' to see what is in the state and watch list fields. And then perform the desired updates to each record. Too complicated for quickly proposing script logic for a BR approach.

 

I took a stab at a BR defined on the sc_req_item table, for when "Additional Comments" changes, see below:

 

Screenshot 2024-04-22 132334.png

Screenshot 2024-04-22 132934.png

 

script follows:

 

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

	// Add your code here
	// check sc_task for records for the sc_request here
	var scTask = new GlideRecord('sc_task');
	scTask.addQuery('request', current.request);
	scTask.addQuery('state', 'awaiting');  // there no choice value for state=awaiting in my instance. so that needs to change to the value for 'awaiting'
	scTask.addQuery('watch_list', 'CONTAINS', gs.getUserID());  // watch_list is a list of users, looking for current user
	var updTaskFlag = false;
	scTask.query();
	while (scTask.next()) {
		updTaskFlag = true;
		scTask.state = 'progress';  // there is no choice value for state=progress on either sc_task or sc_req_time, use the value for those in your instance.
		gs.addInfoMessage('Updating state on sc_task: ' + scTask.number);
		scTask.update();
	}
	if (updTaskFlag){
		current.state  = 'progress';  // there is no choice value for state=progress on either sc_task or sc_req_time, use the value for those in your instance.
		gs.addInfoMessage('Updating state on sc_req_itme: ' + current.number);
	}

})(current, previous);

 

You need to use the Choice values defined in your instance to state = "Awaiting" and "pending' on your two tables.

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Community Alums 

It would be nice if you could share what script did you start with along with screenshots.

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

Community Alums
Not applicable

Hi @Ankur Bawiskar 

I Didn't start with script.

I am asking for Script. so , i could make the changes.

if you want further information i can share.

Thanks & Regards,

Mohammed Sazid.

 

Bert_c1
Kilo Patron

@Community Alums 

 

why not review OOB business rules that perform similar actions. One is named "Update Idea State on Close" defined on the 'dmn_demand' table.

 

Screenshot 2024-04-22 095825.pngScreenshot 2024-04-22 095841.png

However, it is unclear on which table to define the BR on based on your requirements:

 

"In the sc_task table the State field of  is Awaiting and watch list field is User Feedback .

After that if the comment is posted in Additional comment field in sc_req_item table ."

 

Seems you need to define the BR on the 'sc_req_item' table, when the 'comments' field changes, the script would then check the 'sc_task' records that reference the same 'sc_request' to see what is in the state and watch list fields. And then perform the desired updates to each record. Too complicated for quickly proposing script logic for a BR approach.

 

I took a stab at a BR defined on the sc_req_item table, for when "Additional Comments" changes, see below:

 

Screenshot 2024-04-22 132334.png

Screenshot 2024-04-22 132934.png

 

script follows:

 

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

	// Add your code here
	// check sc_task for records for the sc_request here
	var scTask = new GlideRecord('sc_task');
	scTask.addQuery('request', current.request);
	scTask.addQuery('state', 'awaiting');  // there no choice value for state=awaiting in my instance. so that needs to change to the value for 'awaiting'
	scTask.addQuery('watch_list', 'CONTAINS', gs.getUserID());  // watch_list is a list of users, looking for current user
	var updTaskFlag = false;
	scTask.query();
	while (scTask.next()) {
		updTaskFlag = true;
		scTask.state = 'progress';  // there is no choice value for state=progress on either sc_task or sc_req_time, use the value for those in your instance.
		gs.addInfoMessage('Updating state on sc_task: ' + scTask.number);
		scTask.update();
	}
	if (updTaskFlag){
		current.state  = 'progress';  // there is no choice value for state=progress on either sc_task or sc_req_time, use the value for those in your instance.
		gs.addInfoMessage('Updating state on sc_req_itme: ' + current.number);
	}

})(current, previous);

 

You need to use the Choice values defined in your instance to state = "Awaiting" and "pending' on your two tables.

Community Alums
Not applicable

thankx a lot @Bert_c1