Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Making Service offering field mandatory in Change Request Form

Piyush Dwivedi
Tera Contributor

In Change Request form how to make service offering field mandatory When a user selects a Service in the "Service" field that belong to Business Service or Technical Service CI class

1 ACCEPTED SOLUTION

Hi @Piyush Dwivedi 

 

Make sure you added the table first in UI policy

 

DrAtulGLNG_0-1749136406021.png

 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

View solution in original post

22 REPLIES 22

PiyushDwivedi_0-1749538542246.png

 

so the issue is, when i am selecting technical service in "Service" field then after submitting the Change record the "Service offering" field getting mandatory automatically later on i realized to change Service in "Service" field as Non Technical or Business service and then update the change record then its not allowing me since Service offering is mandatory,

so can we have a solution for this where Service offering gets optional once we select no Technical or Business service?

@Piyush Dwivedi 

Once again, the client script and script include I've posted below works for your use case.

Bert_c1
Kilo Patron

What works for me is a Client Script and Script Include:

 

Screenshot 2025-06-08 090526.png

Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
	if (oldValue == newValue) {
		return;
	}

   // make mandatory
	var srvc =  g_form.getValue('business_service');
	var ga = new GlideAjax('CheckService');
	ga.addParam('sysparm_name', 'checkServiceClass');
	ga.addParam('sysparm_srvc_id', srvc);
	ga.getXMLAnswer(getResponse);

	// callback function for returning the result from the script include
	function getResponse(response) {
		var srvc = response;
		if (srvc == 'true') {
			g_form.setMandatory('service_offering', true);
		}
		// in case the service changed to one where the service_offering is not mandatory
		else {
			g_form.setMandatory('service_offering', false);
		}
	}
}

Script include:

Screenshot 2025-06-08 090840.png

Script:

var CheckService = Class.create();
CheckService.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	checkServiceClass: function() {
		var srvcID = this.getParameter("sysparm_srvc_id");
		var retValue = false;
		var srvc = new GlideRecord('cmdb_ci_service');
		srvc.addQuery('sys_id', srvcID);
		srvc.query();
		if (srvc.next()) {
			if ((srvc.sys_class_name == 'cmdb_ci_service_business') ||
 				(srvc.sys_class_name == 'cmdb_ci_technical_service') {
				retValue = true;
			}
		}
		return retValue;
	},

    type: 'CheckService'
});

 

Or...

 

a business rule can be used, but may not be the best user experience:

Screenshot 2025-06-08 104759.png

script:

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

	// Add your code here
	var srvcID = current.business_service;
	var srvc = new GlideRecord('cmdb_ci_service');
	srvc.addQuery('sys_id', srvcID);
	srvc.query();
	if (srvc.next()) {
		if ((srvc.sys_class_name == 'cmdb_ci_service_business') || (srvc.sys_class_name == 'cmdb_ci_technical_service')) {
			gs.addErrorMessage('Must supply a Service Offering for the selected Service!');
			current.setAbortAction(true);
		}
	}

})(current, previous);