Now Assist Custom Skill is not fetching Input

dhivyal94299399
Tera Contributor

Hi Everyone,

I'm working on a custom Now Assist Skill to get count of bounce back details of Story (The number of times a state has been moved backwards)
When I test in skill Kit I provide the Input of activites as array and it works fine, but when trying the same once published in UI action its not working. 
I have provided 2 inputs one is the record and the other one is JSON Array for activites
when I click the UI action by using the Record it provides the detail as 0 as there are no data for it. 
I tried querying the sys_audit table to get an array and pass but its throwing an error stating Sys Audit table cannot be queried.

How can I pass the activities of the story as input value so that I can receive the expected response.

Kindly assist me with the issue.

Regards.

2 ACCEPTED SOLUTIONS

yashkamde
Mega Sage

Hello @dhivyal94299399 ,

 

test this code on your bg and use the record number that you know has moved backward in its state at least once.

var testStoryNumber = 'STRY0010020'; // Replace with your actual record number
var activityArray = [];

var grStory = new GlideRecord('table');
if (grStory.get('number', testStoryNumber)) {
    
    gs.print('Found Story: ' + grStory.getDisplayValue());
    
    // 2. Force the history set to generate/update
    var hs = new GlideHistorySet(grStory);
    hs.generate();
    
    var storyId = grStory.getUniqueValue();
    
    // 3. Find the generated history set
    var grHistorySet = new GlideRecord('sys_history_set');
    grHistorySet.addQuery('id', storyId);
    grHistorySet.query();
    
    if (grHistorySet.next()) {
        gs.print('Found History Set ID: ' + grHistorySet.getUniqueValue());
        
        // 4. Query the history lines specifically for State changes
        var grHistoryLine = new GlideRecord('sys_history_line');
        grHistoryLine.addQuery('set', grHistorySet.getUniqueValue());
        grHistoryLine.addQuery('field', 'state'); 
        grHistoryLine.orderBy('update'); 
        grHistoryLine.query();
        
        gs.print('Number of state changes found: ' + grHistoryLine.getRowCount());
        
        while (grHistoryLine.next()) {
            activityArray.push({
                "from_state": grHistoryLine.getValue('old'), 
                "to_state": grHistoryLine.getValue('new'),
                "changed_on": grHistoryLine.getValue('update_time')
            });
        }
    } else {
        gs.print('Could not find or generate a History Set for this record.');
    }
    
    // 5. Output the final JSON string exactly as the Skill Kit will receive it
    gs.print('\n--- FINAL JSON PAYLOAD FOR NOW ASSIST ---');
    // Using null and 4 in stringify to pretty-print the JSON in the background script output
    var jsonActivities = JSON.stringify(activityArray, null, 4); 
    gs.print(jsonActivities);
    
} else {
    gs.print('Error: Could not find Story with number ' + testStoryNumber);
}

 

after this you can copy the printed JSON array directly from the BG output screen and paste it into your Now Assist Skill Kit's test panel to verify if your AI prompt correctly identifies the bounce backs from the raw array.

 

If my response helped mark as helpful and accept the solution.

View solution in original post

Tanushree Maiti
Tera Patron

Hi @dhivyal94299399 

 

Instead of sys_audit table , 

  1. Find the sys_history_set record associated with your Story's sys_id. This table acts as a container for audit data related to a specific record. 
  2. Query the sys_history_line table where the set field matches your History Set. This table contains the field changes, including old_value and new_value for the state

Sample script /details - you will get from these post:

Scripts: GlideRecord sys_history_set / sys_history_line / sys_audit

What is the best way to find sys_audit record for sys_journal_field 

KB1226188 How to view the history of a record while logged in as a user with read-only access 

 

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

View solution in original post

4 REPLIES 4

yashkamde
Mega Sage

Hello @dhivyal94299399 ,

 

test this code on your bg and use the record number that you know has moved backward in its state at least once.

var testStoryNumber = 'STRY0010020'; // Replace with your actual record number
var activityArray = [];

var grStory = new GlideRecord('table');
if (grStory.get('number', testStoryNumber)) {
    
    gs.print('Found Story: ' + grStory.getDisplayValue());
    
    // 2. Force the history set to generate/update
    var hs = new GlideHistorySet(grStory);
    hs.generate();
    
    var storyId = grStory.getUniqueValue();
    
    // 3. Find the generated history set
    var grHistorySet = new GlideRecord('sys_history_set');
    grHistorySet.addQuery('id', storyId);
    grHistorySet.query();
    
    if (grHistorySet.next()) {
        gs.print('Found History Set ID: ' + grHistorySet.getUniqueValue());
        
        // 4. Query the history lines specifically for State changes
        var grHistoryLine = new GlideRecord('sys_history_line');
        grHistoryLine.addQuery('set', grHistorySet.getUniqueValue());
        grHistoryLine.addQuery('field', 'state'); 
        grHistoryLine.orderBy('update'); 
        grHistoryLine.query();
        
        gs.print('Number of state changes found: ' + grHistoryLine.getRowCount());
        
        while (grHistoryLine.next()) {
            activityArray.push({
                "from_state": grHistoryLine.getValue('old'), 
                "to_state": grHistoryLine.getValue('new'),
                "changed_on": grHistoryLine.getValue('update_time')
            });
        }
    } else {
        gs.print('Could not find or generate a History Set for this record.');
    }
    
    // 5. Output the final JSON string exactly as the Skill Kit will receive it
    gs.print('\n--- FINAL JSON PAYLOAD FOR NOW ASSIST ---');
    // Using null and 4 in stringify to pretty-print the JSON in the background script output
    var jsonActivities = JSON.stringify(activityArray, null, 4); 
    gs.print(jsonActivities);
    
} else {
    gs.print('Error: Could not find Story with number ' + testStoryNumber);
}

 

after this you can copy the printed JSON array directly from the BG output screen and paste it into your Now Assist Skill Kit's test panel to verify if your AI prompt correctly identifies the bounce backs from the raw array.

 

If my response helped mark as helpful and accept the solution.

Hi @yashkamde 

Th script works works great. But my issue is the output I'm getting here is currently working when I manually give that as an input in the skill kit. How to make it dynamic. I'm using UI action in the table and on click I want the respose which is showing 0 now as no input is passed. 

It would be of great help if you can assist here.

Thank you!

yes sure !

 

could you plz share the ui action code ?

Tanushree Maiti
Tera Patron

Hi @dhivyal94299399 

 

Instead of sys_audit table , 

  1. Find the sys_history_set record associated with your Story's sys_id. This table acts as a container for audit data related to a specific record. 
  2. Query the sys_history_line table where the set field matches your History Set. This table contains the field changes, including old_value and new_value for the state

Sample script /details - you will get from these post:

Scripts: GlideRecord sys_history_set / sys_history_line / sys_audit

What is the best way to find sys_audit record for sys_journal_field 

KB1226188 How to view the history of a record while logged in as a user with read-only access 

 

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti