Updating Worknotes using a custom widget

Aqil
Tera Contributor

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:

function($scope, spUtil) {
    var c = this;
 
    c.noteError = "";
    c.savingNote = false;
    c.workNoteText = "";
    
    // Initialize the work notes array on the scope from the server data
    $scope.worknotes = c.data.worknotes || [];
 
    c.submitWorkNote = function () {
        c.noteError = "";
        var text = (c.workNoteText || "").trim();
 
        if (!text) {
            c.noteError = "Please enter a work note.";
            return;
        }
 
        c.savingNote = true;
 
        // Save note
        c.server.update({
            action: "addWorkNote",
            sys_id: c.data.sys_id,
            note: text
        }).then(function (response) {
            var d = response && response.data ? response.data : {};
 
            if (d.ok === false) {
                c.savingNote = false;
                c.noteError = (d.errors || ["Failed to save work note."]).join(" ");
                return;
            }
 
            // Update the work notes array with the new data from the server
            $scope.worknotes = d.worknotes || [];
        c.savingNote = false;
        c.workNoteText = "";
        });
    };


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.

2 REPLIES 2

Mark Manders
Mega Patron

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.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

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.