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.

Service Offering Mandatory for Technical or Business Service

Piyush Dwivedi
Tera Contributor

Issue is, when i am selecting technical service in "Service" field of Change form then after submitting the Change record the "Service offering" field getting mandatory automatically as per my expectations.

further to this when i am changing Service in "Service" field as Non Technical or Business service and then againing clicking update the change record then it's not allowing me since Service offering is mandatory,

I am try Dot walk UI Policy.

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

 

 

1 ACCEPTED SOLUTION

Define a client script for the incident table, as you have for the change_request table. The same Script Include can be used by any number of client scripts. You can copy the client script and just change the table from 'change_request' to 'incident'. 

View solution in original post

21 REPLIES 21

Bert_c1
Kilo Patron

When you're tired of trying a UI Policy, consider a onChange Client script that runs on the change_request table for the "Service" field. the script follows:

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

   // make mandatory or not based on the business_service field
	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);
		}
		else {
			g_form.setMandatory('service_offering', false);
		}
	}
}

The Script Include (named 'CheckService') to check the class being 'Business Service' or 'Technical Service' follows:

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_service_technical')) {
				retValue = true;
			}
		}
		return retValue;
	},

    type: 'CheckService'
});

:

Do i need to just copy past this both the script in Client script?

Bert_c1
Kilo Patron

@Piyush Dwivedi 

what is involved here is you want to make a field mandatory or not based on the value of another field. Here, it is the "Service" field on Change Request table. Which is a reference field to a record in the "Services" (cmdb_ci_service) table. That table has child tables for some common service groups. That is where the 'cmdb_ci_business_service' and 'cmdb_di_technical_service' records exist. Since those are child tables of 'cmdb_ci_service', the 'sys_class_name' field on that 'parent' record will have the specific Class. So a server query is used to find the specific record in 'cmdb_ci_service' to check the Class name for specific values.

 

I don't think you can make a Server query from a UI Action. But maybe you'll get another solution.

so what is the suggested solution, Please suggest?