Automated Fulfillment of ServiceNow Requests – Part 3

Picture of Eric Anderson

Eric Anderson

Recap

In this three-part series, we’re exploring how automation can streamline the fulfillment of ServiceNow requests.  By leveraging the ServiceNow API and automation tools, we’re learning how to transform manual, time-consuming processes into repeatable workflows.

In Part 1, we laid the foundation by exploring how to interact with the ServiceNow API.  We covered the essentials of retrieving a JSON representation of ServiceNow records.

In Part 2, we covered the relationships between requests, items, and tasks, clarifying how these components interact within ServiceNow.  We also dove deeper into retrieving tasks and their associated item variables.

Now, in Part 3, we’re bringing it all together.  This final installment will guide you through the complete end-to-end process of automating the lifecycle of a ServiceNow task.  By the end of this post, you’ll have a clear blueprint for implementing a fully automated workflow in your environment.  Let’s dive in!

Putting it All Together

The goal of the script we’re building is to continuously check for new tasks in ServiceNow, fulfill them, and close them out. It operates as a loop that runs at regular intervals, retrieving tasks and processing them step by step.

The main function is the heart of the script. It performs the following actions in a continuous loop:

  1. Fetches open tasks from ServiceNow.
  2. Retrieves the variables associated with the task.
  3. Updates the task’s status to “Work in Progress” to prevent duplicate processing.
  4. Executes the task’s fulfillment logic.
  5. Closes the task when the fulfillment is complete.
  6. Wait 10 seconds
  7. Repeat steps 1-7

				
					def main():

    while True:

        notify(1, 'Getting all open tasks assigned to automation group')
        tasks = get_open_tasks(assignment_group)

        if tasks:
            task_verbiage = "task" if len(tasks) == 1 else "tasks"
            notify(1, f'{len(tasks)} {task_verbiage} retrieved for fulfillment')

            tasks_data = []

            for task in tasks:

                notify(1, f'Working on task {task["number"]}')
                ritm_sys_id = task['request_item']['value']
                ritm_name = task['request_item.cat_item.name']

                notify(1, f'Getting item variables for task {task["number"]}')
                ritm_vars = get_ritm_vars(ritm_sys_id)

                variables = {}

                for ritm_var in ritm_vars:

                    variables[ritm_var['sc_item_option.item_option_new.name']] = ritm_var['sc_item_option.value']

                task_data = {}
                task_data['task_number'] = task['number']
                task_data['task_sys_id'] = task['sys_id']
                task_data['ritm_name'] = ritm_name
                task_data['task_variables'] = variables
                tasks_data.append(task_data)

                notify(1, f'Setting task {task["number"]} to "Work in Progress"')
                set_task_to_wip(task_data['task_sys_id'])

                notify(1, f'Fulfilling task {task["number"]}')
                task_fulfillment = fulfill_task(task_data)

                if task_fulfillment['result_code'] == 0:
                    notify(1, f'Closing task {task["number"]}')
                    close_task(task_data['task_sys_id'])

            notify(1, f'{task_verbiage.capitalize()} fulfilled successfully')

        else:
            notify(1, 'No tasks to fulfill')

        notify(1, 'Sleeping for 10 seconds')
        time.sleep(10)
				
			

The output of this main function will look like this when no tasks are found to fulfill:

				
					2025-01-05 19:31:14.204010 - Starting ServiceNow task handler
2025-01-05 19:31:14.204050 - Getting all open tasks assigned to automation group
2025-01-05 19:31:14.442560 - No tasks to fulfill
2025-01-05 19:31:14.442596 - Sleeping for 10 seconds
				
			

When tasks are retrieved that need to be fulfilled, the output will look like this

				
					2025-01-05 19:51:01.021640 - Starting ServiceNow task handler
2025-01-05 19:51:01.021676 - Getting all open tasks assigned to automation group
2025-01-05 19:51:01.311920 - 1 task retrieved for fulfillment
2025-01-05 19:51:01.311963 - Working on task SCTASK0010007
2025-01-05 19:51:01.311970 - Getting item variables for task SCTASK0010007
2025-01-05 19:51:01.556648 - Setting task SCTASK0010007 to "Work in Progress"
2025-01-05 19:51:01.877094 - Fulfilling task SCTASK0010007
2025-01-05 19:51:02.206688 - Closing task SCTASK0010007
2025-01-05 19:51:02.534721 - Task fulfilled successfully
				
			

GitHub Repository

Great news!  I have created a project in GitHub called the ServiceNow Task HandlerThe repository includes all the code and resources needed to get started, as well as instructions for customization and deployment.  Please feel free to fork or contribute as you see fit.

Conclusion

Over the course of this three-part series, we’ve explored how to automate ServiceNow task fulfillment, from understanding the basics of the ServiceNow API to building a fully functional Python script that retrieves, processes, and closes tasks.  By leveraging the code and concepts shared in this series, you now have the tools to adapt and expand this solution for your organization’s specific needs.  The provided GitHub repository serves as a great starting point.

If you have feedback, questions, or ideas for improvement, feel free to reach out or contribute to the GitHub repository.

Thank you for following along, and happy automating! 🚀

 

SHARE

More Posts