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

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

Thanks a lot @Sagar Pagar ! The auto population works now.

 

A slight issue however, is that after the first auto population, the service offering now is filtered based on the business service that's auto populated (correct behavior). The problem is that if I want to select a different service offering, the value immediately disappears. Is this because of the reference qualifier or the script? 

 

 

 

javascript:(!gs.nil(current.business_service)? 'parent='+current.business_service: '');

 

 

 

update: also, after the first auto population, if I want to manually select a different business service, any service offering I choose will immediately disappear as well.

 

Hi @JordyZ,

Try by removing the reference qualifier.

 

Thanks,
Sagar Pagar

The world works with ServiceNow

Hi @Sagar Pagar ,

 

I've tried removing reference qualifier but still no success. There's also an issue when I manually populate the business service (parent) first and then select the service offering (child), the value of service offering disappears immediately.