AbstractAjaxProcessor - The newItem() method
New article articles in ServiceNow Community
·
Jun 29, 2025
·
article
Take a look at this OOTB code:
var AgileAjaxProcessor = Class.create(); AgileAjaxProcessor.prototype = Object.extendsObject(AbstractAjaxProcessor, { updateStoryDefect: function() { var result = this.newItem("result"); var storySysId = this.getParameter('sysparm_story_sys_id'); var defectSysId = this.getParameter('sysparm_defect_sys_id'); var gr; try { if (!storySysId) throw new Error('sysparm_story_sys_id is missing'); if (!defectSysId) throw new Error('sysparm_defect_sys_id is missing'); gr = new GlideRecordSecure('rm_story'); if (gr.get(storySysId)) { gr.defect = defectSysId; if (gr.update()) { result.setAttribute('status', 'success'); result.setAttribute('message', 'story.defect has been updated successfully'); } else { throw new Error('unable to update story ' + storySysId); } } else throw new Error('story record ' + storySysId + ' does not exist'); } catch (err) { result.setAttribute('status', 'failed'); result.setAttribute('message', err.message); } }, type: 'AgileAjaxProcessor' });
A simple script include that accepts a valid Story sys_id, fetches the record and assigns a valid defect record in the Defect reference field.
But where is the return statement?
The script include doesn't return a value!
THE KEY lies in this line of code:
var result = this.newItem("result");
When the client does:
``var ga = new GlideAjax('AgileAjaxProcessor'); ga.addParam('sysparm_name', 'updateStoryDefect'); ... ga.getXML(function(response) { var result = response.responseXML.documentElement; var status = result.getAttribute("status"); var msg = result.getAttribute("message"); });
The responseXML is automatically populated from the items you created on the server using:
` this.newItem("result").setAttribute("...");``
`
This is the designed behavior of AbstractAjaxProcessor, and you don’t need to manually return anything unless you’re using getAnswer()/getXMLAnswer().
When You Do Need a Return Statement
- If you're using getXMLAnswer(), you must explicitly return inside the server method, which becomes the "answer". // return return "Success!";
To summarize:
``
If you're building a full XML tree using this.NewItem(), No 'return' is needed — the framework handles it. Think of this.NewItem(), as writing into a reply envelope that ServiceNow automatically seals and sends to the client for you.
Regards,
Anish Reghu
Hit Like if this helps you!
https://www.servicenow.com/community/developer-articles/abstractajaxprocessor-the-newitem-method/ta-p/3303603