Make Work notes or Additional comments required upon closure with client script

Brian24
Tera Expert

All,

I'm attempting to build a client script that requires either work notes or additional comments be populated upon closure of a catalog task. We have added additional comments to the sc_task form as a reference to sc_req_item.comments. The script below is not working.

 

function onChange(control, oldValue, newValue) {
	if (oldValue == newValue)
		return;

 var state = g_form.getValue('state');
 var cmnts = g_form.getValue('sc_req_item.comments');
	
	if  (state == '3' || state == '4' || state == '7' && cmnts == ''){
		g_form.setMandatory('work_notes', true);
		g_form.setMandatory('assigned_to', true);
}
	else {
		g_form.setMandatory('work_notes', false);
		g_form.setMandatory('comments', true);
		g_form.setMandatory('assigned_to', true);
}
}
3 REPLIES 3

Mark Roethof
Tera Patron
Tera Patron

Hi there,

What exactly is not working? Have you checked if the script is executed at all? Maybe the UI Type is already an issue. Or onChange of what field is this running? I guess State?

Looking at your script:
g_form.getValue('sc_req_item.comments'); will not work for sure. Also consider changing state == '3' || state == '4' || state == '7', the state don't need quotes, these are integers, or just check is active is true/false.

Also var state = g_form.getValue('state'); could also just use newValue?

If you would fix this, it should be able to work.

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Brian24
Tera Expert

Thanks for your response Mark. 
I should've added in my initial post:

UI Type: All

Type: OnChange

Field Name:State

The script continues to mark work_notes and assigned_to as a mandatory fields, ignoring the criteria of the comments field. I wouldn't think I would need a getReference back to the ritm as I am not submitting this journal field until I successfully submit the record.

So g_form.getValue('sc_req_item.comments')is your issue indeed.

Its simply because dotwalking this way is not possible client side. You could do a getReference, though far more better would be GlideAjax with a getXMLAswer.

If you would fix this, it should be able to work.

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn