The CreatorCon Call for Content is officially open! Get started here.

How to get the catalog item field in the Item form?

swathigangadhar
Tera Expert

How to get the catalog item name in a field Item form?

test item.PNG

I need to load name of the item in the "Item Name" field, Ex: In the above screen shot, i need to have 'Test Item' value in Item name.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Swathi,



I think you can have onLoad catalog client script, get the window url which should contain the catalog item sys_id, query the maintain item table with this sys_id and populate the item name in that variable.



var windowUrl = window.location.href;



// it should print something like this.



"https://dev17945.service-now.com/ess/com.glideapp.servicecatalog_cat_item_view.do?sysparm_clear_stac..."



var rec_sys = getParmVal('sysparm_id'); // this contains the sys_id


if(rec_sys != null) {



var gr = new GlideRecord('sc_cat_item');


gr.addQuery('sys_id',rec_sys);


gr.query();


if(gr.next()){


g_form.setValue('variable_name', gr.name)


}



}



function getParmVal(name){


  var url = document.URL.parseQuery(); // this also gives the window url


  if(url[name]){


  return decodeURI(url[name]);


  }


  else{


  return;


  }


}



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Nana5
Mega Guru

Hi Swathi,


  For this, i belive you have to use variable editor to get the variable field on request item form.



Thanks


Ankur Bawiskar
Tera Patron
Tera Patron

Hi Swathi,



I think you can have onLoad catalog client script, get the window url which should contain the catalog item sys_id, query the maintain item table with this sys_id and populate the item name in that variable.



var windowUrl = window.location.href;



// it should print something like this.



"https://dev17945.service-now.com/ess/com.glideapp.servicecatalog_cat_item_view.do?sysparm_clear_stac..."



var rec_sys = getParmVal('sysparm_id'); // this contains the sys_id


if(rec_sys != null) {



var gr = new GlideRecord('sc_cat_item');


gr.addQuery('sys_id',rec_sys);


gr.query();


if(gr.next()){


g_form.setValue('variable_name', gr.name)


}



}



function getParmVal(name){


  var url = document.URL.parseQuery(); // this also gives the window url


  if(url[name]){


  return decodeURI(url[name]);


  }


  else{


  return;


  }


}



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Harneet Sital
Mega Sage
Mega Sage

Hi Swati,



We had a similar requirement and here is the code we built. This will populate the name of the item in a specific field, in our case short description.



var sysID = $('sysparm_id').value;


  var catalogName = new GlideRecord('sc_cat_item_producer');


  //catalogName.addEncodedQuery(encQuery);


  catalogName.addQuery('sys_id',sysID);


  catalogName.query();


  while(catalogName.next())


  {


  g_form.setValue('Short_Description',catalogName.name); // BACKEND NAME OF THE FIELD ITEM_NAME


  }


Ankur Bawiskar
Tera Patron
Tera Patron

Hi Swathi,



Since you have marked the answer as correct, please mark it as helpful and hit like. Thanks in advance.



Regards


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader