Prevent individual DevOps stories from being created/updated in ServiceNow

Brad Warman
Giga Sage

I've been investigating a way to prevent individual Azure DevOps stories from being created/updated, based on a field in the DevOps story. Unfortunately, the OOB configuration in ServiceNow only allows us to enable or disable the syncing of work items/stories. It doesn't allow for this to occur on a story-by-story basis. After searching the documentation and forums I couldn't find any information on how the inbound payload is processed, but was able to work it out by looking into the many scripts/flows etc that come with the Azure DevOps Agile Integration module. I'm writing this post in case others are looking to achieve something similar.

 

The business requirement was quite simple - the DevOps integration is critical to the business, but there are a small percentage of DevOps stories that do not need to be synced to ServiceNow. The DevOps users needed a way to stop the creation/update of a ServiceNow Agile story using a field on the DevOps story.

 

The DevOps administrator created a true/false field called 'Synced to ServiceNow' which was to be used to decide if the DevOps story was to be created/updated in ServiceNow. This field was mapped using the OOB table/field mapping for the DevOps project. The ServiceNow field that was configured was a simple choice list called 'Synced to DevOps' with yes/no options. A choice field was used due to ServiceNow's limitations on the types of custom fields that can be mapped (see 'Customize your field map configuration' documentation).

 

Now that we had the field mapped and confirmed that it was syncing correctly, the next step was to use it to prevent the sync process from occurring if the DevOps 'Synced to ServiceNow' field was set to false. This was achieved by making a minor change to the 'ADOWebhookEventProcessorUtil' script include which controls the processing of the inbound payload from DevOps.

 

The following code was added within the processWorkItem function from line 65 in the script include (note, the line number may be different in your instance).

 

// Added to stop the creation/update of stories that are created in DevOps from being created in SN. Custom.StopServiceNowsync is a custom field on the DevOps project that allows for the team to stop certain stories being synced. This is used to stop the creation/update of stories.
if (!gs.nil(eventType) && (eventType == 'workitem.created' || eventType == 'workitem.updated')) {
  var checkSync = JSON.parse(workitemPayload);
  var fields = checkSync.resource.revision.fields;

  // Check if the custom DevOps field exists.
  if (fields.hasOwnProperty(['Custom.StopServiceNowsync'])) {
      var fieldValue = JSON.stringify(fields['Custom.StopServiceNowsync']);

      // If field value is false, this means that the DevOps story has been set to not sync to SN.
      if (fieldValue == 'false') {
          var devopsId = JSON.stringify(checkSync.resource.revision.id);
          var devopsTitle = JSON.stringify(fields['System.Title']);

          // Add a log to indicate why the sync did not occur
          gs.info("Workitem " + devopsId + " - " + devopsTitle + " has not synced to SN as DevOps story is set to not sync.");

          // Add a return to stop the rest of the script from procesing the creation/update.
          return;
      }
  }
}

 

With these changes to the script include, the DevOps story will not be created/updated in ServiceNow whenever the 'Synced to ServiceNow' field in DevOps is set to false. 

 

Hopefully this helps others who are looking to achieve a similar outcome.

 

Cheers,

Brad

0 REPLIES 0