Exporting Graphical Workflows in One Step

/, System Definition/Exporting Graphical Workflows in One Step

Exporting Graphical Workflows in One Step

While this article is still extremely useful as an example and a reference for exporting records and use of processors, the specific functionality for exporting workflow versions is now included by default in ServiceNow. The functionality was built-in starting with the Aspen release.

I‘ve written before about how you can quckly export and import data between ServiceNow instances using the XML export/import context menus. While this works great for a lot of situations, it doesn’t work so great for data that resides in multiple tables, but really makes sense to be exported as a single unit. One example of this is Graphical Workflows. The components that make up a Graphical Workflow version are actually stored in 7 separate tables. It is possible to export a graphical workflow version but you have to do 7 separate exports to do it! In this post, I’ll show you how you can set up UI actions for some of these more complex export tasks.


The first step is to set up a ‘Processor’ record to handle the export. ServiceNow actually has some built-in code to handle exports like this. You just have to know how to leverage it. Here’s how you would set up a processor for the workflow export.

‘ExportWorkflow’ Processor
Name: ExportWorkflow
Type: Script
Path: export_workflow
Script:

//Get the sys_id of the workflow version
var sysID = g_request.getParameter('sysparm_sys_id');
//Get the sys_id of the workflow
var wfID = g_request.getParameter('sysparm_wf_id');
var actID = '';//Query for workflow activities
var act = new GlideRecord('wf_activity');
act.addQuery('workflow_version', sysID);
act.query();
while(act.next()){
actID = actID + ',' + act.sys_id.toString();
}

//Export workflow version info to XML
var exporter = new ExportWithRelatedLists('wf_workflow_version', sysID);
exporter.addRelatedList('wf_stage', 'workflow_version');
exporter.addRelatedList('wf_activity', 'workflow_version');
exporter.addRelatedList('wf_condition', 'activity.workflow_version');
exporter.addRelatedList('wf_transition', 'to.workflow_version');
exporter.addRelatedList('wf_transition', 'from.workflow_version');
exporter.addRelatedList('wf_workflow_instance', 'workflow_version');
if(wfID != ''){
exporter.addQuerySet('wf_workflow', 'sys_id=' + wfID);
}
if(actID != ''){
exporter.addQuerySet('sys_variable_value', 'document_keyIN' + actID);
}
exporter.exportRecords(g_response);

Once you have your processor set up, you just need to call it. The processor above is called by its path name ‘export_workflow’ followed by ‘.do’. It also needs to know what to export so you need to pass it the sys_id of the top-level record that you want to export…in this case, the sys_id of the ‘Workflow context’ record.

‘Export to XML’ UI Action
Name: Export to XML
Table: Workflow Version (wf_workflow_version)
Order: 200
Form context menu: True
Hint: Export workflow for importing in another instance
Condition: gs.hasRole(“admin”)
Script:

action.setRedirectURL('export_workflow.do?sysparm_sys_id=' + current.sys_id + '&sysparm_wf_id=' + current.workflow);

Once you’re done with this, you should have an ‘Export to XML’ UI action context menu item on your ‘Workflow version’ form that you can use to transport workflows in the event that they don’t get captured in an update set.

By | 2018-07-09T15:00:03-06:00 December 14th, 2010|Categories: Graphical workflow, System Definition|Tags: , , , |34 Comments

About the Author:

34 Comments

  1. Eric Gustavson April 29, 2011 at 8:06 am - Reply

    Good post. Thanks. Do you have another post on how to import the workflow that you exported. I followed your instructions and was able to export successfully.

  2. Adam Scheblein May 2, 2011 at 7:28 am - Reply

    The graphical workflow (activities etc.) exported perfectly. However, the scripts that I had written as part of the “Run Script” and “Catalog Task” activities didn’t export. Any idea on how to include these as part of the one step process?

    • Mark Stanger May 2, 2011 at 12:18 pm - Reply

      I can’t believe nobody has seen this issue before! Thanks for bringing it to my attention. The problem is that those values are actually stored in yet another table that has to be specially queried in order to add the values to the export. I’ve updated the processor script above to grab those values. Give it a try with the new code and let me know how it goes.

  3. Adam Scheblein May 5, 2011 at 6:43 am - Reply

    Ah, much better! The scripts are now included. Thank you for your effort! I found one more nit, but it’s not a showstopper. The selected items in the “Variables on Task Form” slush bucket (part of the catalog task activity) weren’t retained.

    • Mark Stanger May 5, 2011 at 6:53 am - Reply

      Thanks for the feedback. In my testing they do come over as long as there are matching variables available for items in the destination instance. Sounds like your variables and items might not match in your destination instance.

      • Adam Scheblein May 5, 2011 at 7:17 am - Reply

        Hey, you’re right. I didn’t pull the whole thing over. Once I did the nit cleared up. 🙂 Thanks!

  4. Adam Scheblein June 3, 2011 at 8:41 am - Reply

    Is it possible to add the workflow record from the wf_workflow table into the Export Workflow processor? That way i don’t have to export it separately every time i need to move it to a different environment. This of course is for our virgin workflows that haven’t been created in our prod instance yet.

    • Mark Stanger June 3, 2011 at 9:07 am - Reply

      It is. I’ve modified the processor and UI action scripts above to include this. Give it a try and see how it works.

  5. Amado Sierra February 7, 2012 at 2:44 pm - Reply

    How about using this same idea to export the definition for a table, including all its related business rules, UI actions, etc.? Could this be possible?

    • Mark Stanger February 7, 2012 at 4:26 pm - Reply

      It’s possible, but would probably be extremely complex. Each application would probably be slightly different and it could get pretty messy.

  6. mark sandner July 5, 2012 at 6:32 pm - Reply

    This worked fine for me too, only like a fool thought I could do it without reading the whole article. I used the OOB processor and UI Action, but forgot to change Condition: gs.hasRole(“admin”) to admin as OOB it is “maint”. Thought admin would override it but there you are. Thanks for the post.

  7. David Field October 3, 2012 at 4:35 am - Reply

    The functionality doesn’t appear to be present in Berlin, when I select ‘Export to XML’ on a Workflow Version the generated XML only appears to contain that record (as per the usual ‘Export to XML’ functionality)

    • Mark Stanger October 3, 2012 at 7:08 am - Reply

      ServiceNow started including this functionality in the Aspen release. There is a UI action included in the Aspen and Berlin releases that allows you to do this. It’s restricted to the ‘maint’ role by default. Look in your UI actions table for one named ‘Export Workflow’. If that’s broken, then you’ll need to contact ServiceNow support.

      • David Field October 3, 2012 at 8:10 am - Reply

        Thanks Mark,

        I was just looking for the wrong thing in the right place

      • Shane C December 12, 2013 at 11:18 am - Reply

        Where do I exactly need to be to view this export link? I can’t find it anywhere.

        It is part of my UI Actions, I just can’t locate where it’s ran from. I am on Calgary releas, with Maint role.

        Thanks.

        • Mark Stanger December 12, 2013 at 12:58 pm - Reply

          Check out the screenshot above. If you followed the directions, it should be available as a right-click context menu option from the header of the Workflow version form.

          • Shane C December 12, 2013 at 1:07 pm - Reply

            I was referring to the now built in UI Action for ServiceNow, to perform this function. I can’t find it. Which page do I need to be on?

          • Mark Stanger December 12, 2013 at 1:37 pm - Reply

            Looks like it’s a Form link on the Workflow version. It has a condition that restricts it to the ‘maint’ role though. You can copy it or change ‘maint’ to ‘admin’.

  8. Morgan Glines April 7, 2013 at 10:44 am - Reply

    Could you please advise how this compares to using an update set to record your workflow? (Looks like this has potential to be smaller?)

    • Mark Stanger April 8, 2013 at 8:21 am - Reply

      Could potentially be smaller I suppose, but the primary difference is that this method targets each table individually. The regular update set method isn’t extractable in a single click and would require you to re-publish the workflow for the workflow to be captured.

  9. Waqas Mahmud January 10, 2014 at 8:57 am - Reply

    Any way to export items from these tables into an Excel format, one of our users is trying to get all the approvals/tasks for each Change workflow in some sort of text format so they can put together a text workflow – any help or thoughts?

    • Mark Stanger January 10, 2014 at 9:07 am - Reply

      You wouldn’t be able to do that this way, and most of the information exported via this method wouldn’t be relevant to that anyway. You could bring up the ‘wf_activity’ table and find the activities associated with that particular workflow version and export those to XLS or CSV using the standard export context menu though.

  10. Christopher Joyal August 4, 2014 at 1:03 pm - Reply

    Subflows appear to break when being moved to new instance. Has anybody else experienced this?

  11. Ravi August 15, 2015 at 4:22 am - Reply

    Mark,
    This was awesome. You saved me a lot of time in finding the data flow and fixing an issue I am working on.
    Apparantly, we had someone who didn’t bother to capture the updates in a proper update set and we ended up with a complex workflow developed successfully, but not exportable to the production.
    Many thanks.
    regards,
    Ravi

  12. Andrew Westervelt June 1, 2016 at 9:44 am - Reply

    For anyone who has had issues with subflow activities not being exported correctly, insert the following code as line 22:

    exporter.addRelatedList(‘wf_workflow_instance’, ‘workflow_version’);

    You’ll still need to export the additional workflow when moving between instances, but at least the relationship will stay intact.

    • Mark Stanger June 1, 2016 at 10:55 am - Reply

      Thanks Andrew! I’ve added this to the script above as well.

  13. Mark June 23, 2016 at 11:55 am - Reply

    I tried using the code you published for Exporting Service Catalog Items in One Step, and it works great!

    But when I tried this script, and clicked on the Export to XML link in my workflow, it didn’t bring up the window to ask me where to save the file.
    Do you have any advice for me?

  14. Mark June 23, 2016 at 1:57 pm - Reply

    By the way, I saw no evidence that pop-ups were blocked. I tried it in Chrome, Internet Explorer, and Firefox, all with the same result.

  15. Avi Goli April 18, 2017 at 3:40 pm - Reply

    Hi Mark,

    Any idea how to copy a workflow in a script (server, not client).. the same as we do in the UI, but I would like to be able to copy workflow (by name or sys_id) to a new (by a new name) … The SN version is Helsinki or/and Istanbul .. Is there any SN API/Processor I can use? something like this maybe: com.glideapp.workflow.ui.ng.NGWorkflowProcessor (java) …

    • Mark Stanger April 19, 2017 at 7:43 am - Reply

      I haven’t seen a way to do that yet. Will let you know if I find out.

  16. Chandran May 18, 2017 at 12:11 pm - Reply

    Is there a way to copy just a single workflow activity from one workflow to another?

    • Jim Pisello July 14, 2017 at 10:32 am - Reply

      Hi Chandran,

      There is no easy way to do that, due to the number of related tables that would also need to have records copied. The way I’ve done this in the past is simply to open the two workflows in the workflow editor canvas. Add a new activity of the correct type to the workflow, then manually configure the new activity so that it mirrors the existing one.

Leave A Comment