Auto populate description field based on the value selected in a lookup select box type field in catalog item

SK41
Giga Guru

Hi,

I have to auto populate  description field  based  on the value  selected in a lookup select box type field which is referring a table in a catalog item. How can i achieve that through scripting?

Kindly, help me in this regard.

Thanks.

 

1 ACCEPTED SOLUTION

client script:

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

//Type appropriate comment here, and begin script below


var ga = new GlideAjax("HardwareDescription");
ga.addParam("sysparm_name","getDescription");
ga.addParam("sysparm_id",newValue);
ga.getXML(callBackFun);


function callBackFun(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
if(answer){
g_form.setValue("description", answer);
}
}


}

 

 

Script include:

Name: HardwareDescription

Client callable: true

script:

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

getDescription: function(){
var id = this.getParameter("sysparm_id");

var grh = new GlideRecord("alm_hardware");
grh.addQuery("sys_id",id);
grh.query();
if(grh.next()){
return grh.getValue("description") + "";
}

return "";
},

type: 'HardwareDescription'
});

 

 

Please note:

1. Verify all field back end names and table names.

2. In lookup select back, verify if Value field is set as sys_id

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

View solution in original post

9 REPLIES 9

P-Rudenko-SN
ServiceNow Employee
ServiceNow Employee

'onChange' Catalog Client Script, variable name {your lookup select box}

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

var c_field = g_form.getReference('your_field', setF);   //your custom field+callback function }
function setF(your_field) {
	g_form.setValue('description',your_field.reference_field);
}

This  does  not work for  lookup select box field.

How many options you have in lookup select box? 

if number of options are less, then you can try below script.

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

//Repeat below condition for number of options in elect box
if(newValue == "option1 in select box field"){
g_form.setValue("description_field_name","description value you want to set");
}


}

If number of options are more or dynamic, you need to have a place (some property or a table) where you have mapping between option value and description value. 

Then in onchange script, read that mapping and based on current option, you can set description.

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

This lookup  select  box  is   referring  a  table  that  has  mapping  in  it  for  many  records.

This  static script   will  not  work.