Editable Journal Entries Popup in Workspace (Last 15 Minutes – Current User)

ItayB
Tera Contributor

Hi everyone,

I’m working on a requirement and would appreciate your input.

We’ve been asked to implement the following functionality as part of an update set:

  • Create a button in Workspace (Agent Workspace) on the Incident form.
    (Note: This will likely be used later on other Task-based tables, so it should be implemented on the Task table.)

  • When the button is clicked, a popup should appear displaying a list of Additional Comments and Work Notes that:

    • Were created in the last 15 minutes

    • Belong to the currently logged-in user

    • Are associated with the current record, based on the sys_journal_field table

  • The agent (user) should be able to edit these comments directly from the popup.

  • Once edited, the comment should be updated in a way that reflects across all views, including both Workspace and the classic Form View.

Please tell me, what is the best way to figure this mission? 
UI Action - MOST
Script Include? / BR? / Just UI Action? 

 

1 REPLY 1

Shruti
Mega Sage
Mega Sage

Hi,

1. Create a UI action on Incident table

Shruti_0-1753191546928.pngShruti_1-1753191565024.png

Update workspace client script 


function onClick(g_form) {
    var sysId = g_form.getUniqueValue();

    // Call GlideAjax to fetch recent comments
    var ga = new GlideAjax('global.CustomUtil');
    ga.addParam('sysparm_name', 'getRecent');
    ga.addParam('sys_id', sysId);
    ga.getXMLAnswer(function(response) {
      
        if (response && response.trim() !== "" && response.trim() !== "[]" && JSON.parse(response).length > 0) {
            var comments = JSON.parse(response);
            // Dynamically build modal fields for all comments
            var fields = comments.map(function(comment, idx) {
                return {
                    type: 'textarea',
                    name: 'comment_' + idx,
                    label: 'Comment ' + idx,
                    mandatory: true,
                    value: comment.value,
                    referringRecordId: comment.sys_id
                };
            });

            g_modal.showFields({
                title: "Enter your comments",
                fields: fields,
                size: 'lg'
            }).then(function(fieldValues) {
                // Iterate over updated fields and send updates via GlideAjax
                fieldValues.updatedFields.forEach(function(field, idx) {
                    var updateGA = new GlideAjax('global.CustomUtil');
                    updateGA.addParam('sysparm_name', 'update');
                    updateGA.addParam('journal_sys_id', field.referringRecordId);
                    updateGA.addParam('new_value', field.value);
                    updateGA.getXMLAnswer(function(response) {
                        // Optionally handle response
                    });
                });
            });
        } else
            g_form.addInfoMessage("No comments found");
    });
}

 

2. Create a client callable script include e.g. CustomUtil

var CustomUtil = Class.create();
CustomUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    update: function() {
        var journalSysId = this.getParameter('journal_sys_id');
        var newValue = this.getParameter('new_value');
        var gr = new GlideRecord('sys_journal_field');
        if (gr.get(journalSysId)) {
            gr.setValue('value', newValue);
            gr.update();
            return 'success';
        }
        return 'failure';
    },

    getRecent: function() {
        var sysId = this.getParameter('sys_id');
        var userId = gs.getUserName();

        var gr = new GlideRecord('sys_journal_field');
        gr.addQuery('element_id', sysId);
        gr.addEncodedQuery('sys_created_on>=javascript:gs.beginningOfLast15Minutes()^sys_created_by=' + userId + '^element=work_notes^ORelement=comments');
        gr.query();

        var results = [];
        while (gr.next()) {

            results.push({
                "sys_id": gr.getUniqueValue(),
                "value": gr.getValue('value')
            });
        }
        return JSON.stringify(results);
    },

    type: 'CustomUtil'
});