Adding filter of custom table's filed to target table

Tamil4
Tera Contributor

Hi Everyone!

 

Am having a custom table, in that I have a field called condition which is Conditions type field.

I have to get the value of this condition filed and use that as a filter in my target table through script.

 

For Example: The custom table's condition field having company=ACME Africa, then I should use the same in incident table filter.

Tamil4_0-1681811793933.png

 

Below is the script I wrote. Not sure how far it is correct.

 

1. var Script = new GlideRecord('u_integration_configuration'); -> this is the custom table name

2. Script.addQuery('u_table_name', current.sys_class_name);
3. Script.addQuery('u_active', true);
4. Script.query();
5. while (Script.next()) {
6. //var fliterString = Script.getValue('u_condition');
7. var Rec = new GlideRecord(Script.u_table_name);
8. Rec.addEncodedQuery(Script.u_condition);
9. Rec.query();
10. if (Rec.next()) {

 

In the 8th line I have to use the condition field's value of custom table.

 

Please help me to achieve this.

 

Thanks in advance.

 

Thanks,

Tamil

19 REPLIES 19

@Tamil4 

are you sure you are querying the correct record with your query?

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

Anil Lande
Kilo Patron

Hi,

Your Script looks good to me.

Please add some log statements and see if you are getting correct encoded query.

Also try using toString on line no 8.

Rec.addEncodedQuery(Script.u_condition.toString());

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Tamil4
Tera Contributor

Thanks for your inputs Anil.

 

As suggested added toString() and added log also but in log it is not giving anything it is just empty.

 

Tamil4_0-1681814180691.png

Tamil4_1-1681814202607.png

 

Tamil

@Tamil4 

so you are getting the condition field value and it's not working with dynamic value.

Did you try storing the Script.u_condition.toString() in some variable and then use it in addEncodedQuery()?

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

auspayday
Giga Contributor

To get the value of the condition field and use it as a filter in your target table through script, you can use the following steps:

  1. Retrieve the record from your custom table using GlideRecord API.
  2. Get the value of the condition field using the getValue() method of GlideRecord.
  3. Build a filter query using the value obtained from the condition field.
  4. Query the target table (in your case, the incident table) using GlideRecord with the filter query.

Here's an example code snippet that shows how to achieve this:

 

// Assuming your custom table name is 'my_custom_table' and the condition field name is 'condition'
var gr = new GlideRecord('my_custom_table');
gr.addQuery('sys_id', '<custom table record sys_id>'); // Replace with the sys_id of the custom table record you want to retrieve
gr.query();

if (gr.next()) {
  var condition = gr.getValue('condition');
  var filterQuery = 'company=' + condition; // Assuming 'company' is a field in the incident table
  var incidentGr = new GlideRecord('incident');
  incidentGr.addEncodedQuery(filterQuery);
  incidentGr.query();

  while (incidentGr.next()) {
    // Do something with the incident record(s) that match the filter query
  }
}

 

 

Replace <custom table record sys_id> with the sys_id of the custom table record you want to retrieve. Also, modify the filter query based on the field name and condition value in your custom table.