Adding filter of custom table's filed to target table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2023 03:04 AM
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.
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2023 05:57 AM
are you sure you are querying the correct record with your query?
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-18-2023 03:28 AM
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());
Thanks
Anil Lande
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2023 03:38 AM
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.
Tamil
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2023 04:28 AM
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()?
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-18-2023 04:06 AM
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:
- Retrieve the record from your custom table using GlideRecord API.
- Get the value of the condition field using the getValue() method of GlideRecord.
- Build a filter query using the value obtained from the condition field.
- 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.