Virtual agent topic fetch

Hemachithra
Tera Contributor

Hi experts!

 

Please help me in fetching user last selected topic/promoted topic in virtual agent servicenow.

so that the returned value is populated to the short description of incident.

1 ACCEPTED SOLUTION

Riya Verma
Kilo Sage
Kilo Sage

Hi @Hemachithra ,

 

Hope you are doing great.

 

To fetch the user's last selected topic/promoted topic in ServiceNow Virtual Agent and populate it to the short description of an incident, we can follow these steps:

  1. Create a Business Rule in ServiceNow that triggers when a new incident is created.
  2. In the Business Rule, use a GlideRecord query to retrieve the last selected topic/promoted topic from the Virtual Agent session history for the user.
  3. Update the short description field of the newly created incident with the fetched topic.

Reference code in BR :

 

// Business Rule: Fetch and Populate Last Selected Topic
// Table: Incident

(function executeRule(current, previous /*, displayed*/ ) {
    // Check if the current incident has a virtual agent session ID associated with it
    if (current.virtual_agent_session && current.virtual_agent_session.length > 0) {

        // Query the Virtual Agent session history table to retrieve the last selected topic/promoted topic
        var vaSessionHistory = new GlideRecord('sn_va_session_history');
        vaSessionHistory.addQuery('session_id', current.virtual_agent_session);
        vaSessionHistory.addQuery('topic', 'ISNOTEMPTY'); // Ensure we get only records with a topic (not system messages)
        vaSessionHistory.orderByDesc('sys_created_on'); // Order by creation time in descending order
        vaSessionHistory.setLimit(1); // Limit the result to the latest record

        if (vaSessionHistory.next()) {
            // If a session history record is found, update the incident's short description with the topic
            current.short_description = vaSessionHistory.topic;
            current.update();
        }
    }
})(current, previous);

 

 
Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma

View solution in original post

1 REPLY 1

Riya Verma
Kilo Sage
Kilo Sage

Hi @Hemachithra ,

 

Hope you are doing great.

 

To fetch the user's last selected topic/promoted topic in ServiceNow Virtual Agent and populate it to the short description of an incident, we can follow these steps:

  1. Create a Business Rule in ServiceNow that triggers when a new incident is created.
  2. In the Business Rule, use a GlideRecord query to retrieve the last selected topic/promoted topic from the Virtual Agent session history for the user.
  3. Update the short description field of the newly created incident with the fetched topic.

Reference code in BR :

 

// Business Rule: Fetch and Populate Last Selected Topic
// Table: Incident

(function executeRule(current, previous /*, displayed*/ ) {
    // Check if the current incident has a virtual agent session ID associated with it
    if (current.virtual_agent_session && current.virtual_agent_session.length > 0) {

        // Query the Virtual Agent session history table to retrieve the last selected topic/promoted topic
        var vaSessionHistory = new GlideRecord('sn_va_session_history');
        vaSessionHistory.addQuery('session_id', current.virtual_agent_session);
        vaSessionHistory.addQuery('topic', 'ISNOTEMPTY'); // Ensure we get only records with a topic (not system messages)
        vaSessionHistory.orderByDesc('sys_created_on'); // Order by creation time in descending order
        vaSessionHistory.setLimit(1); // Limit the result to the latest record

        if (vaSessionHistory.next()) {
            // If a session history record is found, update the incident's short description with the topic
            current.short_description = vaSessionHistory.topic;
            current.update();
        }
    }
})(current, previous);

 

 
Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma