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.
Name: ExportWorkflow
Type: Script
Path: export_workflow
Script:
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.
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:
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.
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.
Sure. Take a look here.
https://servicenowguru.wpengine.com/system-definition/i…
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?
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.
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.
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.
Hey, you’re right. I didn’t pull the whole thing over. Once I did the nit cleared up. 🙂 Thanks!
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.
It is. I’ve modified the processor and UI action scripts above to include this. Give it a try and see how it works.
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?
It’s possible, but would probably be extremely complex. Each application would probably be slightly different and it could get pretty messy.
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.
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)
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.
Thanks Mark,
I was just looking for the wrong thing in the right place
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.
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.
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?
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’.
Could you please advise how this compares to using an update set to record your workflow? (Looks like this has potential to be smaller?)
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.
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?
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.
Subflows appear to break when being moved to new instance. Has anybody else experienced this?
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
You’re welcome Ravi!
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.
Thanks Andrew! I’ve added this to the script above as well.
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?
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.
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) …
I haven’t seen a way to do that yet. Will let you know if I find out.
Is there a way to copy just a single workflow activity from one workflow to another?
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.