Dynamically fetch records from table

Pratiksha Lang1
Kilo Sage

Hi, How can I dynamically fetch records from table.

for example : I have multiple combinations in 1 table as

combination  - role - admin, site - BDC11, frequency - daily , type - measurable

I have many different combinations in 1 table. How can I fetch it dynamically to perform some calculations further.

Can anyone please help on this

 

5 REPLIES 5

Sai Shravan
Mega Sage

Hi @Pratiksha Lang1 ,

 

Use GlideRecord queries to dynamically fetch records from a table based on specific combinations of values

var gr = new GlideRecord('your_table_name'); // name of your table
gr.addQuery('role', 'admin');
gr.addQuery('site', 'BDC11');
gr.addQuery('frequency', 'daily');
gr.addQuery('type', 'measurable');
gr.query();
while (gr.next()) {
  // Do something with the record
  gs.info(gr.getValue('field_name')); // name of the field you want to retrieve from the record.
}

You can add or remove queries as needed to match the specific combination of values you're looking for.

 

Regards,
Shravan

Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you

@Sai Shravan I don't want to fetch it on the basis of specific condition. The code which you provided is static. I want it to work dynamically as there are multiple unique combinations.

To dynamically fetch records from a table in ServiceNow, you can use the GlideRecord API and add queries to the record dynamically based on user inputs or other criteria. Here is an example script that fetches records dynamically from a table named my_table based on some example conditions:

 

var table = 'my_table';
var gr = new GlideRecord(table);

// Example dynamic query conditions
var conditions = [
  {field: 'role', value: 'admin'},
  {field: 'site', value: 'BDC11'},
  {field: 'frequency', value: 'daily'},
  {field: 'type', value: 'measurable'}
];

// Add query conditions dynamically
for (var i = 0; i < conditions.length; i++) {
  gr.addQuery(conditions[i].field, conditions[i].value);
}

// Execute the query
gr.query();

// Loop through the records returned by the query
while (gr.next()) {
  // Perform some calculations or other actions on each record
  var recordId = gr.getUniqueValue();
  // ...
}

 

this example, the table variable is set to the name of the table you want to fetch records from, and the gr variable is a new GlideRecord object that is initialized to the specified table.

The conditions variable is an array of objects that represent the dynamic query conditions. Each object has two properties: field, which is the name of the field to query on, and value, which is the value to query for.

The for loop iterates over the conditions array and adds a query condition to the gr object for each condition.

The query() method is called to execute the query, and the while loop is used to iterate over the records returned by the query.

Within the loop, you can perform whatever calculations or actions you need to on each record. The getUniqueValue() method can be used to get the sys_id of the current record, which can be useful for performing further actions on that record.

 

Please mark my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hi @Pratiksha Lang1 ,

To make the GlideRecord query dynamic, you can use an array of objects to hold the dynamic conditions and loop over them to build the query.

 

var table = 'your_table_name'; // name of your table
//declaring the conditions in an array
var conditions = [
  {field: 'role', value: 'admin'},
  {field: 'site', value: 'BDC11'},
  {field: 'frequency', value: 'daily'},
  {field: 'type', value: 'measurable'}
];

var gr = new GlideRecord(table);
var firstCondition = true;

for (var i = 0; i < conditions.length; i++) {
  var condition = conditions[i];
  gr.addQuery(condition.field, condition.value);

  if (firstCondition) {
    gr.addActiveQuery();
    firstCondition = false;
  }
}
gr.query();
while (gr.next()) {
  // Do something with the record
  gs.info(gr.getValue('field_name')); // name of the field you want to retrieve from the record.
}
Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you