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

How can i loop through a List Collector?

subhyde
Giga Contributor

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); 
	          }
         }
    }
}
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

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

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

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

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

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

Jaspal Singh
Mega Patron
Mega Patron

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]);

 

Harsh Vardhan
Giga Patron

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. 

 

https://docs.servicenow.com/bundle/geneva-servicenow-platform/page/script/server_scripting/reference...