Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to fetch price of a catalog item which can vary based on variable values

Subhajit Mukhe1
Kilo Expert

In my catalog form, there is a list collector where we can select multiple values and each value has a price that will add up to the price of the catalog item. This functionality is working fine. The catalog item price is increasing as per the value selection. But I need to make a variable mandatory and visible if the price exceeds 2500$ before form submission... 

Any idea how can I achieve this.

14 REPLIES 14

Hi,

You can get the price value by following code:

var gr = new GlideRecord('sc_cat_item');
gr.addQuery('name','Apple iPhone 6s'); // name of the catalog item 
gr.query();
if(gr.next())
{
alert('Price value using GlideRecord is '+gr.price);
}

OR you can use the script include also.

 

Thanks,

Mohit Kaushik

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)

Yeah, this price is okay. But I need the value onchange of price or before form submission. This price through glide record will provide the value updated in the database.

Hey,

You can use the below code to get the value associated with price in a catalog item:

var price =g_form.getElement('price_span').innerHTML;

var price2 =price.trim().replace('$',''); // this will remove the white spaces and remove the $ sign as well so that you can compare

Now before comparing the values you need to parse this to decimal value and for that you can do it like this.

if(parseFloat(price2)>2500) // this will check your price statement to make your variable available and mandatory.

here price_span is the id of the value of price element in your catalog form. So once you open the catalog form in native UI, you need to select the value of price and do right click to go to inspect. Then from there you can get the Id associated to your catalog's price value. As shown in below screenshot.

find_real_file.png

find_real_file.png

 

Like wise you need to see it in your catalog item form and then it should do the remaining work. Apart from this i don't see there is any other way to get the value.

 

Please mark this answer correct and helpful if your query is resolved.

 

Thanks,

Mohit Kaushik

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)

I think this approach might work although its throwing javascript error in service portal.

Yes this will not work in Service portal as portal does not support g_form.getElement() method. 

I feel there is no other to achieve this functionality apart from modifying the OOB widget. But i would say that is again not a best practice because this widget works for all the catalog items, so your change can affect the functionality for other items as well. You need to do some complex coding i feel to achieve this.

 

Regards,

Mohit Kaushik

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)