As my blog title indicates everything I try to share is about try, learn, grow, repeat. Most of the content I shared so far was about stuff I did and played around with. The recent post was about the update set governance and naming convention that I applied in my instances. And it works like a charm with all my test cases, but like always you will not test everything.
This week someone else said that the business rule is not really working and I was wondering what it was. So I looked up his use case and he used a couple of spaces within the first 11 characters of the substring that I used to identify the story number. If it was plain text like in all my use cases all worked well, but spaces were a problem for the REST call. I received all the time an invalid URL except when I copied the URL directly in the browser. The browser changed the blanks by itself to ‘%20’ but the REST call from ServiceNow had still blanks.
So the fix to the business rule is to replace all spaces with nothing to get a concatenated string, which you will also get when it is a correct story number at the beginning.
The issue was with line 5. I have added a replaceAll() into it to remove the spaces. The fixed business rule will look like this:
(function executeRule(current, previous /*null when async*/) { // Extract story number from the update set var number = current.getValue('name'); number = number.replaceAll(' ','').substring(0, 11); // Set variables for the REST call var instance = gs.getProperty('tlgr.productionServiceNowEnvironment'); var product = gs.getProperty('tlgr.productSysID'); /* Get a result from the rm_story table in the production environment * Conditions: * Product = ServiceNow (or whatever you mentioned as sysID in the property) * Active = True * State IS ONE OF Ready, Work In Progress, Ready for Testing, Testing * Number starts with Number Substring * Return the number of the user story */ var endpoint = 'https://' + instance + '/api/now/table/rm_story?sysparm_query=sys_class_name%3Drm_story%5Eproduct%3D' + product + '%5Eactive%3Dtrue%5EstateIN1%2C2%2C-7%2C-8%5EsprintISNOTEMPTY%5EnumberSTARTSWITH' + number + '&sysparm_fields=number'; // Create the REST call to production ServiceNow environment var request = new sn_ws.RESTMessageV2(); request.setEndpoint(endpoint); request.setHttpMethod('GET'); // Get the credentials from the credentials store var provider = new sn_cc.StandardCredentialsProvider(); var credential = provider.getCredentialByID('c1b813702f630110f908f64ef699b6f3'); // Sys ID of your credentials var user = credential.getAttribute("user_name"); var password = credential.getAttribute("password"); request.setBasicAuth(user, password); request.setRequestHeader("Accept", "application/json"); var response = request.execute(); var responseBody = response.getBody(); responseBody = JSON.parse(responseBody); // Check if the number of the update set has a valid story in production, if not abort the action and throw an error message. if (number != responseBody.result[0].number) { gs.addErrorMessage("Update Set can't be closed. Please check if naming convention is correct and user story is part of current sprint and in a state like Ready, WIP, Ready for Testing or Testing!"); current.setAbortAction(true); } })(current, previous);
You can see, that you never stop learning what small topics you have to test and check. I’m sure I will never again face that issue with spaces in my rest call or at least I will test on it.
1 thought on “Update Set governance and naming convention – FIX”