Updating Worknotes using a custom widget
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2025 01:06 PM
Server Script:
(function () {
data.errors = [];
data.ok = true;
// Get sys_id from multiple possible sources
var sysId = input && input.sys_id ? input.sys_id : options.sys_id || $sp.getParameter("sys_id");
data.sys_id = sysId;
function loadIncident(sysId) {
var inc = new GlideRecord("incident");
if (!inc.get(sysId)) {
data.ok = false;
data.errors.push("Incident not found.");
return;
}
data.incident = {
sys_id: inc.getUniqueValue(),
number: inc.getValue("number"),
short_description: inc.getValue("short_description"),
caller: inc.getDisplayValue("caller_id"),
state: inc.getDisplayValue("state"),
state_value: inc.getValue("state"),
assigned_to: inc.getDisplayValue("assigned_to")
};
}
function loadWorkNotes(sysId) {
var notes = [];
var j = new GlideRecord("sys_journal_field");
j.addQuery("element_id", sysId);
j.addQuery("name", "incident");
j.addQuery("element", "work_notes");
j.orderByDesc("sys_created_on");
j.query();
while (j.next()) {
notes.push({
note: j.getValue("value"),
added_by: j.getDisplayValue("sys_created_by"),
added_on: j.getDisplayValue("sys_created_on")
});
}
data.worknotes = notes;
}
// Handle adding a note
if (input && input.action === "addWorkNote") {
var gr = new GlideRecord("incident");
if (gr.get(sysId)) {
gr.work_notes = input.note;
gr.update();
// After adding the note, reload the entire list of notes and pass it back
loadWorkNotes(sysId);
// Send back the updated notes list and a success flag
data.ok = true;
return;
}
// If incident record is not found, set error flags
data.ok = false;
data.errors.push("Failed to find incident to add work note.");
return;
}
// Initial load of data when the widget loads
loadIncident(data.sys_id);
loadWorkNotes(data.sys_id);
Client Script:
HTML :
<h4>Add work note</h4>
<textarea class="form-control"
ng-model="c.workNoteText"
placeholder="Enter work note..."
ng-disabled="c.savingNote"></textarea>
<div class="text-danger" ng-if="c.noteError">{{c.noteError}}</div>
<button class="btn btn-primary" style="margin-top:8px;"
ng-click="c.submitWorkNote()" ng-disabled="c.savingNote">
<span ng-if="!c.savingNote">Submit work note</span>
<span ng-if="c.savingNote">Saving…</span>
</button>
<h4 style="margin-top:20px;">Work notes</h4>
<div class="list-group" ng-if="worknotes.length">
<div class="list-group-item" ng-repeat="wn in worknotes track by $index">
<div class="small text-muted">{{wn.added_by}} • {{wn.added_on}}</div>
<div ng-bind="wn.note"></div>
</div>
</div>
<div ng-if="!worknotes.length" class="text-muted">
No work notes yet.
</div>
I want to add the new Worknotes and display it in the Worknotes list, when Submit Work Note button is clicked in the widget.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2025 01:06 AM
What's the process? Work notes are for internal use and aren't normally used within widgets.
Also: you share the script and your question, but what is currently happening? It will save time if we know what does and what doesn't work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2025 08:37 AM
It's for internal use only.
Currently, when I press the Submit Work Note button, it vanishes the existing work notes and does not add new work notes.