logo

NJP

ServiceNow Processors

SN Scout · Dec 15, 2020 · article

ServiceNow has some legacy features and components that are technically deprecated, but are still in use. Officially, ServiceNow supports their own use of processors, but not newly created ones. They instead are directing customers to use Scripted REST APIs. This is all well and good, but sometimes you want to know how something works. Digging into some existing features that maybe you want to replicate.

The use case that brought me into Processors was a feature request to have a cart-like approval experience with line item vetoes for Release Stories similar to a multiple Requested Items (RITMs) on a Request (Req) approval for the Service Catalog.

I found the "approval_summarizer_sc_request" UI Macro that creates the view and experience for Reqs. I made a new copy of it and modified the jelly script to refer to the release and story tables. This all worked well until I got to the function 'lineItemVeto'. The function calls 'service_catalog.do'.

I looked around and began to get pretty steamed. Literally nothing. No script, form or anything else that pointed me in the right direction. I finally found an obscure article that said 'service_catalog.do' was a black box java call which was unavailable for customers to see or modify. I found a script include called 'CatalogTransactionLineItemVeto', which was clearly the set of functions I needed, but I couldn't get to it from the UI Macro as it is a server side call.

So I felt stuck, until another article pointed me toward creating a custom processor. It's essentially a relay station. Processors can execute a script and return values as needed. The call can be wholly contained in the processor, or it can call a script include. I chose a script include, again copying the OOB script and modifying for my needs. The tricky part here was that while the processor calls the script include using abstract AJAX, it needs a response from the execute call. Since the OOB structure was a black box to me I had to figure out how to send the value back to the processor for the UI Macro to use and update the view, but it kept leading to a different page. I tried a few things that didn't work including 'this.response.setRedirectURL(current)' and finally found 'this.response.sendRedirect(url)' to be the correct method.

Now all this work was a few years ago. Knowing what I know now, I probably would look for different methods to use from inside the UI Macro, but I'm glad to know how it works, and hope you found it enlightening as well.

Scripts

UI Macro

(Click here for clean snippet to copy/paste)

| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | <?xml version="1.0" encoding="utf-8" ?> ${gs.getMessage('Summary of Stories within Release being approved')}:

<!-- current_page.getURLSuffix(); /g:evaluate Current URL is this:
${RP.getParameterValue('jvar_base_uri')}sysapproval_approver.do?sys_id=${jvar_ourid} -->
${rm_release_scrum.opened_by.sys_meta.label}: ${rm_release_scrum.opened_by.getDisplayValue()}
${gs.getMessage('Total Story Points')}: ${jvar_total}
function lineItemVeto(rm_story, state, approval) { var form = addForm(); form.action = &#39;release_story.do&#39;; form.name = &#39;release_story.do&#39;; form.id = &#39;release_story.do&#39;; form.method = &quot;POST&quot;; var nameOfStack = $(&#39;sysparm_nameofstack&#39;); if (nameOfStack != null) { nameOfStack = nameOfStack.value; if (nameOfStack != &#39;&#39;) addInput(form, &quot;HIDDEN&quot;, &quot;sysparm_nameofstack&quot;, nameOfStack); } addInput(form, &quot;HIDDEN&quot;, &quot;sysparm_approval&quot;, approval); addInput(form, &quot;HIDDEN&quot;, &quot;sysparm_action&quot;, &quot;veto&quot;); addInput(form, &quot;HIDDEN&quot;, &quot;sysparm_id&quot;, rm_story); addInput(form, &quot;HIDDEN&quot;, &quot;sysparm_state&quot;, state); form.submit(); } /j:if /j:when /j:when /j:when /j:when/j:choose/j:while
${gs.current} ${gs.getMessage('Status')} ${rm_story_labels.number.sys_meta.label} ${rm_story_labels.short_description.sys_meta.label} ${rm_story_labels.description.sys_meta.label} ${rm_story_labels.acceptance_criteria.sys_meta.label} ${rm_story_labels.story_points.sys_meta.label}
${gs.getMessage('Rejected')} ${gs.getMessage('Requested')} ${gs.getMessage('Approved')} ${gs.getMessage('Pending')} ${gs.getMessage('uppercase\_view')} ${rm_story.number} ${smart_description} ${rm_story.description} ${rm_story.acceptance_criteria} ${rm_story.story_points}
/j:jelly |
| ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

Processor

Name = ReleaseStory

Type = Script

Path = release_story

(Click here for clean snippet to copy/paste)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 executeProcessor(); function executeProcessor() { if (!g_processor.canRunAction()) return; var valves = new GlideappValveProcessor(); if (valves.beforeProcessor(g_request, g_response, g_processor)) return; var a = new ReleaseStoryLineItemVeto(g_request, g_response, g_processor); a.execute(); }

Script Include

(Click here for clean snippet to copy/paste)

| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | gs.include('PrototypeServer'); gs.include('AbstractTransaction'); var ReleaseStoryLineItemVeto = Class.create(); ReleaseStoryLineItemVeto.prototype = Object.extendsObject(AbstractTransaction, { execute : function() { var id = this.request.getParameter('sysparm_id'); var state = this.request.getParameter('sysparm_state'); var stackID = this.request.getParameter('sysparm_nameofstack'); var approval = this.request.getParameter('sysparm_approval'); var url = "sysapproval_approver.do?sys_id="+approval; //gs.addErrorMessage("approval from script include =" + approval + " | url = " + url); this.response.sendRedirect(url); var gr = new GlideRecord("rm_story"); gr.addQuery('sys_id', id); gr.query(); if (gr.next()) { if (state == 'reject'){ this._veto(gr,url);} else{ this._accept(gr,url);} return false; } gs.addErrorMessage(gs.getMessage("Could not reject release story as it could not be located")); return "home.do"; }, _veto : function(gr,url) { gr.setValue("approval", 'rejected'); gr.setValue("state", -6); gs.setRedirectURL(url); gr.update(); gs.getSession().addInfoMessage(gs.getMessage("Release story {0} has been reset to draft", gr.number)); }, _accept : function(gr,url) { gr.setValue("approval", "requested"); gr.setValue("state", 1); gs.setRedirectURL(url); gr.update(); gs.getSession().addInfoMessage(gs.getMessage("Release story {0} has been accepted", gr.number)); } }); |
| ------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

View original source

https://snscout.blogspot.com/2020/12/servicenow-processors.html