Unlock ServiceNow Superpowers: A Beginnerâs Guide to Building Your First Workflow
ServiceNow is a powerhouse platform for streamlining IT and business processes. At its heart lies the workflow engine, a visual tool that allows you to automate tasks and create efficient, repeatable processes. This guide will walk you through the fundamentals of creating your first ServiceNow workflow, even if youâve never touched it before. Weâll cover the basics, illustrate with practical examples, and provide step-by-step instructions to empower you to build your own automated solutions.
What is a ServiceNow Workflow?
Think of a workflow as a digital flowchart. Itâs a sequence of activities that are triggered by a specific event and then automatically execute to achieve a desired outcome. Workflows can automate virtually anything, from approving requests to onboarding new employees to resolving incidents.
Key components of a workflow:
- Trigger: The event that initiates the workflow (e.g., a new incident record being created).
- Activities: The individual tasks or actions performed within the workflow (e.g., sending an email, updating a record, approving a request).
- Transitions: The paths that connect activities, determining the flow based on conditions or outcomes (e.g., âif the request is over $1000, route it to the managerâ).
- Canvas: The visual workspace where you design and build your workflow.
Getting Started: Accessing the Workflow Editor
- Log in to your ServiceNow instance. Youâll need appropriate permissions to access the workflow editor (typically
workflow_admin
orworkflow_creator
roles). - Navigate to Workflow > Workflow Editor. You can find this using the application navigator in the left sidebar.
Creating Your First Workflow: A Simple Incident Assignment
Letâs build a basic workflow that automatically assigns incidents to a specific assignment group based on the category selected.
1. Create a New Workflow:
- In the Workflow Editor, click âNew Workflowâ.
- Fill out the following fields:
- Name: Assign Incident by Category
- Table: Incident [incident]
- If condition matches: Choose ârecord is inserted or updatedâ. We want the workflow to run when a new incident is created or updated.
- Access: Change it to âAll scopesâ.
- Description: A brief explanation of what the workflow does (e.g., âAssigns incidents to specific assignment groups based on categoryâ).
- Click âSubmitâ. This will open the workflow canvas.
2. Add the âBeginâ and âEndâ Activities:
These are automatically created for you. They mark the start and end points of the workflow.
3. Add a âRun Scriptâ Activity:
- In the Workflow Editorâs left pane, search for âRun Scriptâ under âCore Activitiesâ.
- Drag and drop the âRun Scriptâ activity onto the workflow canvas between the âBeginâ and âEndâ activities.
- Connect the âBeginâ activity to the âRun Scriptâ activity by dragging the connector (the circle on the edge of the activity) from âBeginâ to the âRun Scriptâ activity.
- Connect the âRun Scriptâ activity to the âEndâ activity.
4. Configure the âRun Scriptâ Activity:
- Double-click the âRun Scriptâ activity to open its properties.
- Name: Assign Group based on Category (this is just a descriptive name for the activity).
- Script: This is where youâll write the JavaScript code to assign the incident. Replace the default script with the following:
(function executeProcess( /*GlideRecord*/ current, /*Workflow*/ workflow, /*WorkflowExecutionContext*/ workflowContext) {
// Get the incident category.
var category = current.category;
// Define a mapping between categories and assignment groups.
var assignmentGroupMap = {
'inquiry': 'Service Desk', // Replace 'Service Desk' with the sys_id of the Service Desk group
'software': 'Software Support', // Replace 'Software Support' with the sys_id of the Software Support group
'hardware': 'Hardware Support' // Replace 'Hardware Support' with the sys_id of the Hardware Support group
};
// Look up the assignment group in the mapping.
var assignmentGroupName = assignmentGroupMap[category];
// If an assignment group is found, assign the incident to it.
if (assignmentGroupName) {
// Query for the assignment group sys_id based on the group name. It's better to use sys_id directly, but this example uses name for simplicity.
var gr = new GlideRecord('sys_user_group');
gr.addQuery('name', assignmentGroupName);
gr.query();
if (gr.next()) {
current.assignment_group = gr.sys_id; // Set the assignment group
current.update(); // Important: Save the changes to the incident record!
} else {
gs.log("Assignment Group '" + assignmentGroupName + "' not found.");
}
} else {
gs.log("No assignment group defined for category: " + category);
}
})(current, workflow, workflowContext);
-
Explanation of the Script:
- The script retrieves the incidentâs category using
current.category
. - It uses a JavaScript object (
assignmentGroupMap
) to store the mapping between incident categories and assignment group names. Important: You should replace the string values (âService Deskâ, âSoftware Supportâ, âHardware Supportâ) with the actualsys_id
values of your assignment groups. You can find thesesys_id
values by going toUser Administration > Groups
in ServiceNow, opening the desired group, and looking at the URL. - It uses a GlideRecord query to get the group sys_id from the group name. It is best practice to use the sys_id directly to avoid issues with naming changes.
- If a matching assignment group is found, it sets the
assignment_group
field of the incident record to the assignment groupâs sys_id. current.update()
saves the changes to the incident record. This is crucial!- Error messages are logged using
gs.log
if an assignment group is not found. These logs can be viewed in the System Logs.
- The script retrieves the incidentâs category using
-
Click âSubmitâ.
5. Validate the Workflow:
- Click the âValidateâ button at the top of the Workflow Editor. This checks for any syntax errors or configuration issues.
6. Publish the Workflow:
- Click the âPublishâ button at the top of the Workflow Editor. This activates the workflow and makes it ready to run.
Testing Your Workflow
- Create a new incident record.
- Select a category that you defined in the
assignmentGroupMap
in your âRun Scriptâ activity (e.g., âInquiryâ). - Save the incident.
- Verify that the incident is automatically assigned to the correct assignment group. You should see the assignment group field updated automatically.
- Check the system logs (System Logs > System Log > All) for any error messages that might have been logged by your script.
Expanding Your Workflow
This is just a basic example. You can expand your workflow in many ways:
- Add approvals: Use the âApproval - Userâ or âApproval - Groupâ activities to route requests for approval.
- Send notifications: Use the âSend Notificationâ activity to send email or SMS notifications.
- Create tasks: Use the âCreate Taskâ activity to create new tasks for specific users or groups.
- Use conditions: Use the âIfâ activity to create branching logic based on specific conditions.
- Integrate with other systems: Use Scripted REST APIs or other integration methods to connect your workflow to external systems.
- Timer activities: Use timer to add a delay in the process
Best Practices for Workflow Development
- Keep it simple: Break down complex processes into smaller, more manageable workflows.
- Use descriptive names: Give your workflows and activities meaningful names that clearly indicate their purpose.
- Comment your code: Add comments to your script activities to explain what the code does.
- Test thoroughly: Test your workflows in a development environment before deploying them to production.
- Use version control: Track changes to your workflows using version control.
- Leverage existing activities: Take advantage of the built-in activities provided by ServiceNow to avoid reinventing the wheel.
- Use GlideRecord judiciously: Avoid querying inside loops wherever possible. Optimize your GlideRecord queries.
Conclusion
Youâve now built your first ServiceNow workflow! This is just the beginning of your journey. By understanding the fundamentals of workflow creation and exploring the available activities, you can unlock the true power of ServiceNow and automate countless processes, saving time, improving efficiency, and transforming the way your organization works. Keep experimenting, keep learning, and keep building!