How to search response_body of Subflow execution

mballinger
Mega Guru

Hello,

 

We have a subflow setup that gets called from a script. I need to search for a specific transaction based on the response_body. There are over 1000 records and it is making this task very difficult because I have to click through every execution. Is there a way that I can search based off the response_body?

 

Thanks!

5 REPLIES 5

Shaqeel
Mega Sage

Hi @mballinger 

 

Yes, you can search based on the response_body in ServiceNow.

Here are the steps:

  1.  Navigate to Flow Execution table(sys_flow_execution). 
  2.  Click on "Advanced view".
  3.  In the "Go to filter" field, enter the following query: response_bodyLIKE. Replace with the specific transaction you are looking for.
  4. Press Enter to execute the search.

By the way, this method will only work if the response_body is stored as a string in the database. If it's stored as a JSON object or any other non-string data type, you'll need to convert it to a string before you can search. 

Also, keep in mind that this method will only search within the current set of records loaded in the list. If there are more records than what's currently loaded, you'll need to navigate to the next set of records and repeat the search.

 

If you want to search across all records in the table, you'll need to create a script that iterates over all records and performs the search. This can be done using a GlideRecord query. Here's a sample script:

var gr = new GlideRecord('sys_flow_execution');
gr.query();

while (gr.next()) {
if (gr.response_body.indexOf('') !== -1) {
// Found a match, do something with it
gs.info('Found match in sys_id: ' + gr.sys_id);
}
}

 

Replace with the specific transaction you are looking for. This script will log the sys_id of all records where a match is found.

 

Mark Helpful/Solution 🙂

 

Regards

Shaqeel

 

 


***********************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action benefits both the community and me.

***********************************************************************************************************************





Regards

Shaqeel

Thank you for your response Shaqeel! I am not seeing the sys_flow_execution table

Hey @mballinger , 

There are two ways in which you can address this issue 

You might not see the sys_flow_execution table due to permission restrictions. Here are some possibilities:

  • Role Permissions: The user's role might not have read access to the sys_flow_execution table.
  • Table Security: The table itself might have security settings that prevent users from seeing it.

the most straightforward approach is to request access from their system administrator. They can explain why they need access and the administrator can grant them the appropriate role or permission.

(Or)

If requesting access isn't feasible, consider alternative scripting approaches that work with data accessible to the user. Here are some possibilities:

  • Pass Information from Subflow: Modify the subflow to pass relevant information from the response_body (or other fields) back to the calling script using outputs or parameters. This information can then be used for further processing or logging without directly accessing the sys_flow_execution table.
  • Script with Current Execution Context: If the script is running within the context of the subflow execution, you might be able to access information about the current execution using the current object. This could include properties like flow_execution_script or custom fields set on the subflow execution record.

1. Pass Information from Subflow:

This approach modifies the subflow to pass the relevant information from the response_body (or other fields) back to the calling script using outputs or parameters.

 

// Assuming 'responseBody' is the variable holding the response data

// Extract the information you want to search for (replace 'searchTerm' with the actual field)
var searchTerm = responseBody.someField;  // Modify 'someField' to extract the desired data

// Set the output parameter with the search term
flow.outputs.searchTerm = searchTerm;

 

Calling Script:

 

// Call the subflow
var flow = new Flow('your_subflow_name');
flow.run();

// Get the search term passed back from the subflow
var searchTerm = flow.outputs.searchTerm;

// Now you can use 'searchTerm' to search for relevant data (e.g., in another table)
gs.info("Search term from subflow: " + searchTerm);

// You can further process or log the search term here

 

2. Script with Current Execution Context:

This approach assumes the script is running within the context of the subflow execution. You can leverage the current object to access information about the current execution.

 

// Assuming 'responseBody' is a field on the subflow execution table

// Get the response body from the current record
var responseBody = current.responseBody;

// Extract the information you want to search for (replace 'searchTerm' with the actual field)
var searchTerm = responseBody.someField;  // Modify 'someField' to extract the desired data

// Now you can use 'searchTerm' to search for relevant data (e.g., in another table)
gs.info("Search term from current execution: " + searchTerm);

// You can further process or log the search term here

 

  • These scripts provide basic examples. You might need to modify them based on your specific subflow logic and data structure.
  • The "Pass Information from Subflow" approach requires modifying the subflow script, which might require approval depending on your workflow.
  • The "Script with Current Execution Context" approach assumes the script runs within the subflow execution context. If the script runs elsewhere, this approach won't work.

By using one of these alternative scripting techniques, you can achieve the goal of searching for transactions based on the response_body, even if the user doesn't have direct access to the sys_flow_execution table. Remember to adapt the scripts to your specific use case and data model.

 

Thanks

Aravind Panchanathan

 

 

 

I have full admin/security admin access