Only once additional comments should be added

Community Alums
Not applicable

HI,

 

Once the ritm is approved a new variable should be displayed and the value from variable should be added to additional comments on ritm. For this i have written a on load client script which should be applied to variable set because this logic should be reusable to other catalog items and script include. But every time form is loaded each time additional comments are displaying i want it to be displayed only once when the variable is populated the value should be displayed in additional comments. 

 

script include:

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

    updateComments: function() {
        var ritmId = this.getParameter('sysparm_ritm_id');
        var commentText = this.getParameter('sysparm_comment_text');

        var checkText = 'The fulfillment of your request is in progress.'
        if (ritmId && commentText) {
            var ritmGR = new GlideRecord('sc_req_item');
            if (ritmGR.get(ritmId)) {

                /* var checkText = 'The fulfillment of your request is in progress.';
                var existingComments = ritmGR.comments || '';
                if (existingComments.indexOf(checkText) === -1) {
                    gs.info('alekhyascriptinclude');
                    ritmGR.comments = existingComments + '/n' +commentText;
                    //ritmGR.comments = (ritmGR.comments || '') + '\n' + commentText;
                    ritmGR.update();
                } */

                var journalGR =new GlideRecord('sys_journal_field');
                journalGR.addQuery('element_id',ritmId);
                journalEntry.addQuery('element', 'additional_comments');
                journalGR.addQuery('value', 'LIKE', checkText);
                journalGR.query();

                if(!journalGR.hasNext()){
                    var journalEntry = new GlideRecord(sys_journal_field);
                    journalEntry.initialize();
                    journalEntry.element_id = ritmId;
                    journalEntry.element = 'additional_comments';
                    journalEntry.value = commentText;
                    journalEntry.insert();
                }

            }
        }
    },

    type: 'UserPostFulfillmentComments'
});
 
client script:
 
function onLoad() {
    //Type appropriate comment here, and begin script below

    if (g_scratchpad.isApproved == 'true') {

        alert('insideif');

       // var ritmSysId = g_form.getValue('sys_id');
       
        var variableValue = g_form.getValue('variables.user_steps_post_fulfillment');
        var defaultValue = 'The fulfillment of your request is in progress.Once you receive notification of completion, please ensure you review the instructions. \n' +variableValue;

        var ga = new GlideAjax('UserPostFulfillmentComments');
        ga.addParam('sysparm_name', 'updateComments');
        ga.addParam('sysparm_comment_text', defaultValue);
        ga.addParam('sysparm_ritm_id',g_scratchpad.ritmSysId);
        alert(g_scratchpad.ritmSysId);
        ga.addParam('sysparm_variable_value', defaultValue);
        ga.getXMLAnswer(function(response) {
        });
}

}
2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@Community Alums 

why to use onLoad client script and set the comments?

Why not use after update BR on sysapproval_approver and handle this?

OR

You can also use flow designer for the same

BR: After Update on sysapproval_approver

Condition: State [Changes To] Approved

Script:

// query RITM and get the variable value and append and add Comments to RITM

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Community Alums
Not applicable

Hi, Thanks for your response. I want to reuse this logic to all catalog items wherever this variable set is added. so am writing client script which should be applied to variable set.