Populate phone type variable based on location table parent field .

suha2
Tera Contributor

Hi All,

In catalog items, I have a work location variable referencing the location table. If the parent field in the location table is set to 'United Kingdom,' then the phone type variable should be visible on the catalog items form. Does anyone have an idea on how to achieve this?

 

suha2_0-1706000172065.png

 

7 REPLIES 7

@Ankur Bawiskar 

I can try this requirement in  script include and catalog client script , but it didn't work for me.  can you  have any idea on how to do this?

var GetLocationParent = Class.create();
GetLocationParent.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
    getParent: function() {
        var locationSysId = this.getParameter('sysparm_location_sys_id');
        var locationGr = new GlideRecord('cmn_location');
        locationGr.addQuery(sys_id,locationSysId );
        if (locationGr.next())
        //if (locationGr.get(locationSysId))
        {
            return locationGr.parent; // Convert to uppercase for case-insensitive comparison
        }
       
        return '';
    },
this script in catalog client script 
function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

   //Type appropriate comment here, and begin script below
   var workLocation = g_form.getValue('Work_location');
 
// Use GlideAjax to get the 'parent' field of the selected location
var ga = new GlideAjax('GetLocationParent');
ga.addParam('sysparm_name', 'name');
//ga.addParam('sysparm_location_sys_id', workLocation);
ga.getXML(getData);

function getData(response)
   {
    var answer = response.responseXML.documentElement.getAttribute('answer');
      if (answer.toUpperCase() =='UNITED KINGDOM') {
        g_form.setDisplay('phone_type', true);
    } else {
        g_form.setDisplay('phone_type', false);
    }

   }
 
    type: 'GetLocationParent'
});

@suha2 

try this

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

	getParent: function() {
		var locationSysId = this.getParameter('sysparm_location_sys_id');
		var locationGr = new GlideRecord('cmn_location');
		locationGr.addQuery('sys_id' ,locationSysId );
		if (locationGr.next())
			//if (locationGr.get(locationSysId))
		{
			return locationGr.parent.getDisplayValue(); // Convert to uppercase for case-insensitive comparison
		}

		return '';
	},
Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Robbie
Kilo Patron
Kilo Patron

Hi @suha2,

 

Whilst a response in this thread provides an example that is easy to code / copy, namely the use of getReference, this is an example of inefficient code. (Even with a callback)

Instead, substitute the use of getReference(), (or GlideRecord lookup) with an asynchronous GlideAjax call.

 

See ServiceNow Technical Best practices and examples here: 

https://developer.servicenow.com/dev.do#!/guides/utah/now-platform/tpb-guide/client_scripting_techni...

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Helpful.

 

Thanks, Robbie