Client Script is not working in HR agent workspace - dialog box

Mohamed Ayub
Tera Contributor
I’m using a client script to display a dialog box whenever a user manually changes the state to Closed Incomplete. The dialog box appears correctly, and I can capture the reason, but the entered reason is not being saved to the u_closure_reason field after saving the record.

if there is any solution or you have also crossed same issue, help me to understand the problem,
Thank in Advance




6 REPLIES 6

Narayana RC
Tera Contributor

You're not using an array to store the field values, so please try using fieldValues.updatedFields.value — remove the [0] and check.

 

Let me know if that helped of you have some struggles and we can discuss the next steps

 

Regards,

Siva

Hi @Narayana RC ,
It didn't work, the state not moving to closed incomplete

MohamedAyub_0-1761127147753.png

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Mohamed Ayub 

please share scripts here rather than image as it becomes difficult to go through those

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Hi @Ankur Bawiskar 
here is the Script that we are using 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    // NOTE: the Close Incomplete UI action on HR Case depends on
    // this client script's behavior
   
    // If state propagation is stopped, don't proceed, just return
    if (g_scratchpad.stopIncompleteStatePropagation) {
        g_scratchpad.stopIncompleteStatePropagation = false;
        return;
    }

    //Live Update check not available in Printer friendly version
    var isLiveUpdating = typeof g_form.isLiveUpdating === 'function' && g_form.isLiveUpdating();

    if (isLoading || newValue === '' || isLiveUpdating) {
        g_scratchpad.previous_state = newValue;
        return;
    }

    // If calling from workspace
    if (typeof(g_modal) !== 'undefined') {
       
        // form field set to close incomplete? can be set from form edit or UI action
        if (newValue == 4) {

            // Go get the Suspend reasons from backend  
            var ga = new GlideAjax('sn_hr_core.irm_hr_CaseAjax');
            ga.addParam('sysparm_name', 'getCloseIncompleteReasons');
            ga.addParam('sysparm_table_name', g_form.getTableName());
            ga.addParam('case_id', g_form.getSysId());
            ga.getXML(wsParseCloseIncompletedReasons);
        }
        // Got the reasons we care about, now plug those values into choice field, and pop the dialog box
    } else {

        // If the state is changed to Close Incomplete (4)
        if (newValue == 4) {
            // Revert back to the previous state and show the close incomplete dialog
            setIncompleteState(g_scratchpad.previous_state);

            var sysId = typeof rowSysId == 'undefined' || rowSysId == null ?
                g_form.getUniqueValue() : rowSysId;
            var dialogClass = GlideModal ? GlideModal : GlideDialogWindow;
            var dialog = new dialogClass('sn_hr_core_irm_closeIncompleteDialog');
            dialog.setTitle(getMessage('Close Incomplete'));
            dialog.setPreference('sysparm_sys_id', sysId);
            dialog.setPreference('sysparm_ok_button_name', getMessage('Close Incomplete'));
            dialog.setPreference('focusTrap', true);
            dialog.setPreference('sysparm_table_name', g_form.getTableName());
            dialog.on('closeIncompleteSuccess', function() {
                dialog.destroy();

                gsftSubmit(null, g_form.getFormElement(), 'sysverb_update_and_stay');
            });
            dialog.render();
            return;
        }
    }

    function wsParseCloseIncompletedReasons(response) {
        var messages = ['reason_code', 'Close Incomplete'];

        getMessages(messages, function() {
            var answer = response.responseXML.documentElement.getAttribute("answer");
            var reasons = JSON.parse(answer);

            if (!reasons)
                reasons = [];

            var fields = [{
                    type: 'choice',
                    name: 'reason',
                    label: getMessage('reason_code'),
                    value: (reasons && reasons.length > 0) ? reasons[0].value : '',
                    choices: reasons,
                    mandatory: true
                }
            ];

            var sysID = g_form.getUniqueValue();
            var tableName = g_form.getTableName();

            g_modal.showFields({
                title: getMessage('Close Incomplete'),
                fields: fields,
                size: 'md',
                confirmType: 'confirm'
            }).then(function(fieldValues) {

                //get the work note entered
                var newReason = fieldValues.updatedFields[0].value;

                g_form.setValue("u_closure_reason", newReason);
                g_form.addInfoMessage('Closure reason entered: ' + newReason);
                setTimeout(function () { g_form.save(); }, 1000);
               
               
            }, function() {
                // Reset state dropdown if the modal is canceled
                setIncompleteState(g_scratchpad.previous_state);
            });
        });
    }

    function setIncompleteState(value) {
        // Stop the state propagation so that we don't go into an infinite loop
        // within this client script
        g_scratchpad.stopIncompleteStatePropagation = true;
        g_form.setValue('state', value);
    }

    g_scratchpad.previous_state = oldValue;
}