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.

Auto population of business service field (parent) based on service offering field (child)

JordyZ
Mega Sage

Hi,

 

On the incident form I'd like to have business service field (parent) auto populated based on service offering field (child), before saving the form. 

 

I have found a post that had a similar query:

https://www.servicenow.com/community/itsm-forum/filter-the-service-offering-field-based-on-parent-se...

 

I have tried to tweak the client script and script include to fit my specific need, but it doesn't work. What happens now is that the moment I try to manually populate the service offering field, the value immediately disappears. The most probable cause of the scripts failing is because I'm putting the wrong field/column names in the wrong places of the code.

 

I'd appreciate it if someone could look at the code. I have also attached screenshots that might help.

 

Client script

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}
	var sc = g_form.getValue('service_offering');

	var ga = new GlideAjax('dvtbsautopop');
	ga.addParam('sysparm_name', 'getServDetails');
	ga.addParam('sysparm_servId', g_form.getValue('service_offering'));
	ga.getXMLAnswer(getResponse);

	function getResponse(response) {
		var res = JSON.parse(response);
		g_form.setValue('business_service', res.business_service);

	}
}

 

Script include

 

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

getServDetails: function() {
		//gs.addInfoMessage('script include triggered');

		var servId = this.getParameter('sysparm_servId');
		//gs.addInfoMessage('service scr--' + servId);
		obj = {};

		var gServOffering = new GlideRecord('service_offering');
		if (gServOffering.get(servId)) {
			obj.service_offering = gServOffering.getValue('parent');
		}
		//gs.addInfoMessage(obj+JSON.stringify(obj));
		return JSON.stringify(obj);
	},

	type: 'dvtbsautopop'
});

 

 

1 ACCEPTED SOLUTION

Sagar Pagar
Tera Patron

Hi @JordyZ,

Update client scripts as  -

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}
	var sc = g_form.getValue('service_offering');

	var ga = new GlideAjax('dvtbsautopop');
	ga.addParam('sysparm_name', 'getServDetails');
	ga.addParam('sysparm_servId', g_form.getValue('service_offering'));
	ga.getXMLAnswer(getResponse);

	function getResponse(response) {
		var res = JSON.parse(response);
		g_form.setValue('business_service', res.service_offering);

	}
}

 

Thanks,
Sagar Pagar

The world works with ServiceNow

View solution in original post

6 REPLIES 6

Hi @JordyZ,

Is there any other client scripts working on same field/column?

 

Thanks,

Sagar Pagar

The world works with ServiceNow

Hi @Sagar Pagar,

 

No, only reference qualifiers and your script. 

 

edit: This is the dictionary override of business service, I don't know if it helps.

JordyZ_0-1674578973061.png