- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2020 03:01 PM
I am trying to loop through a list collector, whilst doing so I am grabbing all the prices of the item and adding it to an 'estimated cost field'
Below is a snippet of the code im working with. the end result is NaN which makes sense. the function only seems to grab the first entry on the list collector and the cost of mysplitresult is undefined.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var cost = 0;
var cost_center = g_form.getValue('om_ba_software');
var mySplitResult = cost_center.split(",");
for(i = 0; i < mySplitResult.length; i++){
var find_price = new GlideRecord('cmdb_ci_business_app');
find_price.addQuery('sys_id', cost_center);
find_price.query(); // Issue the query to the database to get all records
if (find_price.next()) {
if(find_price.cost == 'Unknown = Quote' || mySplitResult[i].cost == '') {
g_form.setValue('est_max_price', 'Unknown = Quote');
}
else
{
cost = +cost + +mySplitResult[i].cost;
g_form.setValue('est_max_price', cost);
}
}
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2020 11:44 PM
Hi,
you are querying with cost_center value in addQuery()
I believe you want to query with all the sys_ids; try this
find_price.addQuery('sys_id', 'IN' , cost_center);
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2020 11:44 PM
Hi,
you are querying with cost_center value in addQuery()
I believe you want to query with all the sys_ids; try this
find_price.addQuery('sys_id', 'IN' , cost_center);
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2020 11:54 PM
Hi,
Few quick things.
for(i = 0; i < mySplitResult.length; i++){
to be
for(var i = 0; i < mySplitResult.length; i++){
find_price.addQuery('sys_id', cost_center);
to be
find_price.addQuery('sys_id', mySplitResult[i]);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2020 12:01 AM
writing glide record in client script is not good approach, kindly use here glide ajax.
use client callable script include , and then use your glide record part inside script include, use glide ajax to execute your script include.
Doc link for further details.