0

1. Create Virtual Agent Topic:

  • Go to Virtual Agent Designer (/VA in the Application Navigator).
  • Create a new Topic for incident creation, for example: "Create an Incident."

2. Define Dialog Actions:

Set up dialog actions to collect details from the user.

  • Question 1: Ask for the Short Description.
  • Question 2: Ask for the Description.
  • Question 3: Ask for the Priority (or let the user choose one).

Example dialog script for the topic:

User: "Create an incident"
Virtual Agent: "What is the short description of the incident?"
User: [User inputs short description]
 Virtual Agent: "Please provide a description for the incident."
User: [User inputs description]
 Virtual Agent: "What is the priority of the incident? You can choose from: Low, Medium, High."
User: [User inputs priority]


3. Script Action for Incident Creation:

Create a Script Action that will execute once the user provides the necessary information. This script will create the incident record using GlideRecord.

Script Action (Server-Side):

(function execute(inputs, outputs) {
    // Extract user inputs
    var shortDescription = inputs.short_description;
    var description = inputs.description;
    var priority = inputs.priority;
     // Create a new incident record
    var incidentGR = new GlideRecord('incident');
    incidentGR.initialize(); // Initialize a new record
     // Set the values for the incident fields based on user input
    incidentGR.short_description = shortDescription;
    incidentGR.description = description;
    incidentGR.priority = getPriorityValue(priority);  // Function to map priority input to numeric value
     // Insert the record into the Incident table
    var incidentSysId = incidentGR.insert();
     // Provide confirmation message back to the user
    outputs.incident_sys_id = incidentSysId;
    outputs.message = 'Your incident has been created successfully. Incident number is ' + incidentGR.number;
})(inputs, outputs);
 // Helper function to map user-friendly priority to ServiceNow priority values
function getPriorityValue(priority) {
    switch(priority.toLowerCase()) {
        case 'low':
            return 3;
        case 'medium':
            return 2;
        case 'high':
            return 1;
        default:
            return 3; // Default to Low if priority is not recognized
    }
}

5. Return the Response to the User:

Once the incident is created, you can use the outputs.message to inform the user of the successful creation of their incident. The Virtual Agent can say something like:

plaintext
"Your incident has been created successfully. The incident number is INC0012345."

Virtual Agent to Create an Incident via User Input ServiceNow
Working Code Asked question February 21, 2025