how to configure client script for shopping hub checkout record producer.

Praju_123
Tera Contributor

We have created the following client script on the Shopping Hub checkout record producers. We want to fetch the product short description and autopopulate the part description field using an on-load client script. However, this script is not working on the Shopping Hub portal. When we run this script as a background script, it works properly.

 

function onLoad() {   
   var gr = new GlideRecord('sn_shop_supplier_product');
   gr.addQuery('sys_id','d144b0d287fb1e50ef8e0e5e8bbb350c');
   gr.query();
   if (gr.next()) {
    alert("welcome");
      g_form.setValue('part_description', gr.getValue('short_description'));
   }
   else {
        alert("Product not found");
 }
}
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Praju_123 

is part_description a variable on your record producer?

if yes then why not use default value and set it?

javascript: var val;
var gr = new GlideRecord('sn_shop_supplier_product');
gr.addQuery('sys_id', 'd144b0d287fb1e50ef8e0e5e8bbb350c');
gr.query();
if (gr.next()) {
    val = gr.getValue('short_description');
}
val;

As per best practices store that sysId in system property and use that if required

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

4 REPLIES 4

Swapna Abburi
Mega Sage
Mega Sage

Hi @Praju_123 

GlideRecord should not be used in client scripts. You can use getRefefence with callback function if 'product' is a reference type variable on your record producer. Or use GlideAjax method with a client callable script include.

priyatam_pvp
Tera Guru

Create a client script and script include as mentioned below:

Client script 

function onLoad() {
var ga = new GlideAjax('GetProductDescription');
ga.addParam('sysparm_name', 'getDescription');
ga.addParam('sysparm_product_id', 'd144b0d287fb1e50ef8e0e5e8bbb350c');
ga.getXMLAnswer(function(response) {
if (response) {
g_form.setValue('part_description', response);
} else {
g_form.setValue('part_description', 'Description not found');
}
});
}

Script Include:

getDescription: function(productSysId) {
var gr = new GlideRecord('sn_shop_supplier_product');
if (gr.get(productSysId)) {
return gr.short_description;
}
return "No description found";
},



If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards
Priyatam

Ankur Bawiskar
Tera Patron
Tera Patron

@Praju_123 

is part_description a variable on your record producer?

if yes then why not use default value and set it?

javascript: var val;
var gr = new GlideRecord('sn_shop_supplier_product');
gr.addQuery('sys_id', 'd144b0d287fb1e50ef8e0e5e8bbb350c');
gr.query();
if (gr.next()) {
    val = gr.getValue('short_description');
}
val;

As per best practices store that sysId in system property and use that if required

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@Praju_123 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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