Filter the Service Offering field based on Parent (Service) and Auto populate Service field

ss123
Tera Contributor

Hi everyone!

 

I need a help on this requirement. We are customizing the Private Task form [vtb_task] and we have created two new fields "Technical Service" and "Technical Service Offering".

 

  • "Technical Service" is a reference field , referencing "Technical Service" table
  • "Technical Service Offering" is a reference field , referencing "Service Offering" table

 

The first requirement is when a "Technical Service" is first selected, the "Technical Service Offering" list will be filtered based on the "Parent" field which is the selected "Technical Service"

 

The second requirement is when a "Technical Service Offering" is first selected, the "Technical Service" is Auto Populated based on "Parent" ("Technical Service")  field of the selected "Technical Service Offering"

 

I am having a hard time finding the solution for this even though I have created several scripts. I've created a Script Include, Client Scripts and also added reference qualifiers, but NO Luck. 

 

Would appreciate if you can help me on this. Thank you!

SabrinaSalazar_0-1668478597756.png

 

1 ACCEPTED SOLUTION

Hi @ss123,

Try this updated scripts for auto populates - 

 

Client Script: BM - Autopopulate Technical Service

 

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

	var ga = new GlideAjax('x_bmk_technology_s.technicalServiceOffering');
	ga.addParam('sysparm_name', 'getServDetails');
	ga.addParam('sysparm_servId', g_form.getValue('x_bmk_technology_s_u_service_offering'));
	ga.getXMLAnswer(getResponse);

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

	}
}

 

Script Includes

Name: technicalServiceOffering

Client callable - true

var technicalServiceOffering = Class.create();
technicalServiceOffering.prototype = Object.extendsObject(global.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.x_bmk_technology_s_u_technical_service_private_task = gServOffering.getValue('parent');
		}
		//gs.addInfoMessage(obj+JSON.stringify(obj));
		return JSON.stringify(obj);
	},

	type: 'technicalServiceOffering'
});

 

Reference qualifier - 

javascript:"parent="+current. x_bmk_technology_s_u_service_offering;

 

Thanks,
Sagar Pagar

The world works with ServiceNow

View solution in original post

11 REPLIES 11

Hi @ss123,

 

Just apply the query in list view Technical Service Offering table with 1 parent. Copy the query and pass the value "Technical Service" instead of hard-coded values.

 

Example-

 

javascript:"parent=" + current.x_bmk_technology_s_u_technical_service_private_task;

 

 

Thanks,

Sagar Pagar

 

The world works with ServiceNow

Hi @Sagar Pagar , im not sure why is this happening but whenever I change the "Use reference qualifier" to Advanced, then input the script, when I save it, it goes back to Simple

 

 

Hi ! @Sagar Pagar when i add this script as Reference qualifier in Technology Service Offering field 

javascript:"parent=" + current.x_bmk_technology_s_u_technical_service_private_task;

it doesn't show all the records when:

"Technical Service" is empty

 

Instead I used this script include below, and added a reference qualifier in "Technical Service" field javascript:new x_bmk_technology_s.BackfillTechServiceOffering().BackfillTechService(currentx_bmk_technology_s_u_service_offering) , but seems not working??

 

Script Include: BackfillTechServiceOffering

API Name: x_bmk_technology_s.BackfillTechServiceOffering

 

var BackfillTechServiceOffering = Class.create();
BackfillTechServiceOffering.prototype = {
initialize: function() {

},

BackfillTechService: function(a) {

var gp = ''; //var gp = [];

if (!a)
return "sys_class_name=cmdb_ci_service_technical"; // return "sys_class_name=cmdb_ci_service_technical";


//service_offering has the business_service relationship
var grp = new global.GlideRecord('service_offering');
grp.addQuery('sys_id', a);
grp.query();

//var busServ = [];
while (grp.next()) {

gp.push(grp.parent.toString());

}
return "sys_idIN" + gp.join(',');
// return "nameIN" + busServ.join(',');
},

type: 'BackfillTechServiceOffering'
}

 

 

Hi @ss123,

Try this in advanced reference qualifier -


javascript:if(current.x_bmk_technology_s_u_technical_service_private_task != ''){
"parent=" + current.x_bmk_technology_s_u_technical_service_private_task;
}

 

 

Note - It will filter out the Technology Service Offering, if Technology Service selected. Otherwise it will show all Technology Service Offerings

 

Kindly mark helpful and correct!

 

Thanks,
Sagar Pagar

The world works with ServiceNow

Hi @Sagar Pagar ALL requirements are now working as expected! 😁

Really appreciate your help on this. 

 

Thanks,

Sab