Workspace UI Action with g_modal

Anderson_R
Tera Contributor

Hello,

I need assistance with a UI Action that should work in both the standard UI and Asset Workspace. The UI Action functions correctly in the standard UI, but I'm encountering issues with its behavior in the Workspace. Specifically, when using the button in Workspace, the work_notes and state fields are not updating as expected.


I've attempted to follow the guidance from the "How to use UI Actions in Workspaces - ServiceNow Community" post and a few others, but there remains to be some user error. Any guidance is greatly appreciated! Thank you!

Script that is working for the Standard UI Form Button:

 

 

function runClientCode() {
    // Prompt the user for a cancellation reason
    var reason = prompt('Are you sure you want to cancel this part requirement? If yes, please provide a reason.');

    // If the user provides a reason, proceed with the cancellation
    if (reason) {
        var cancelMsg = 'Part Requirement cancelled by the Warehouse for the following reason: ' + reason;
        
        // Set the state to '4' (closed incomplete) and update the work_notes
        g_form.setValue('state', '4');
        g_form.setValue('work_notes', cancelMsg);
        
        // Save the record
        g_form.save();
    } else {
        // If no reason is provided, prevent further action
        return false;
    }
}

 

 


Workspace Client Script that's not updating the form fields as expected:

 

 

function runClientCode(g_form) {
    g_modal.showFields({
        title: "Are you sure you want to cancel this part requirement?",
        fields: [{
            type: 'textarea',
            name: 'cancellation_reason',
            label: getMessage('If yes, please provide a reason.'),
            mandatory: true
        }],
        size: 'lg'
    }).then(function(fieldValues) {
        var reason = fieldValues.cancellation_reason; // Accessing the value using the 'name' attribute
        if (reason) {
            var cancelMsg = 'Part Requirement cancelled by the Warehouse for the following reason: ' + reason;
            
            // Set the state to '4' (closed incomplete) and update the work notes
            g_form.setValue('state', '4');
            g_form.setValue('work_notes', cancelMsg);
            
            // Save the record
            g_form.save()
        } else {
            // If no reason is provided, prevent further action
            return false;
        }
    });
}

 

 


Workspace Modal:

Anderson_R_0-1721770840858.png

Screenshots of UI Action:

Anderson_R_1-1721770919266.pngAnderson_R_2-1721770950401.png

 

1 ACCEPTED SOLUTION

Shruti
Mega Sage
Mega Sage

Hi, Try below code and replace ui action name and script include name

Create a script include and a function to update the state and worknotes



function onClick(g_form) {
    askConfirmation(g_form);
}

function askConfirmation(g_form) {
    var fields = [{
        type: 'textarea',
        name: 'cancellation_reason',
        label: getMessage('If yes, please provide a reason.'),
        mandatory: true
    }];

    var instructions = getMessage("Are you sure you want to cancel this part requirement?");

    g_modal.showFields({
        title: getMessage('Confirmation'),
        fields: fields,
        instruction: instructions,
        cancelTitle: getMessage('Cancel'),
        confirmTitle: getMessage('Move to cancelled'),
        size: 'md',
        cancelType: "default",
        confirmType: "confirm"
    }).then(function(fieldValues) {
        if (g_form.submit('<ui_action_name>')) {
            var updatedFields = fieldValues["updatedFields"];
            var worknotes = updatedFields[0]["value"];
            var sysId = g_form.getUniqueValue();
            var ga = new GlideAjax('<scriptIncludeName>');
            ga.addParam('sysparm_name', 'updateAdditionalWorknotes');
            ga.addParam('sysparm_sysId', sysId);
            ga.addParam('sysparm_worknotes', worknotes);
            ga.addParam('sysparm_tableName', g_form.getTableName());
            ga.getXMLAnswer(function() {});
        }
    });
}

 

View solution in original post

4 REPLIES 4

Shruti
Mega Sage
Mega Sage

Hi, Try below code and replace ui action name and script include name

Create a script include and a function to update the state and worknotes



function onClick(g_form) {
    askConfirmation(g_form);
}

function askConfirmation(g_form) {
    var fields = [{
        type: 'textarea',
        name: 'cancellation_reason',
        label: getMessage('If yes, please provide a reason.'),
        mandatory: true
    }];

    var instructions = getMessage("Are you sure you want to cancel this part requirement?");

    g_modal.showFields({
        title: getMessage('Confirmation'),
        fields: fields,
        instruction: instructions,
        cancelTitle: getMessage('Cancel'),
        confirmTitle: getMessage('Move to cancelled'),
        size: 'md',
        cancelType: "default",
        confirmType: "confirm"
    }).then(function(fieldValues) {
        if (g_form.submit('<ui_action_name>')) {
            var updatedFields = fieldValues["updatedFields"];
            var worknotes = updatedFields[0]["value"];
            var sysId = g_form.getUniqueValue();
            var ga = new GlideAjax('<scriptIncludeName>');
            ga.addParam('sysparm_name', 'updateAdditionalWorknotes');
            ga.addParam('sysparm_sysId', sysId);
            ga.addParam('sysparm_worknotes', worknotes);
            ga.addParam('sysparm_tableName', g_form.getTableName());
            ga.getXMLAnswer(function() {});
        }
    });
}

 

Calling the script include to update the state and work notes worked out great! Thank you! 

For anyone wanting to do something similar. The setValue method that worked for me in the standard UI form button, didn't seem to work for the script include. 

// Set the state to '4' (closed incomplete) and update the work_notes
        g_form.setValue('state', '4');
        g_form.setValue('work_notes', cancelMsg);

 For the script include I ended up using: 

// Update the record
        var gr = new GlideRecord(tableName);
        if (gr.get(sysId)) {
            gr.work_notes = prefixNotes; // Use direct assignment for the work_notes field
            gr.state = '4'; // State '4' typically represents 'Closed Incomplete'
            gr.update();

if there's a choice field in the modal, how to call a script include to fetch the choices from sys_choice table for the table and the field?

Thanks

Asking your own questions on someone else's question isn't helping you to get to a solution faster. It is better to ask your own question on the Community for a couple of reasons:

  • This question is from over 6 months ago and was also answered very soon (accepted solution). People seeing it in the 'updated' questions will see it is already resolved and won't look further.
  • The only people notified on your question, are the contributors to this question and you run the risk that they moved on and are no longer checking on the ServiceNow Community
  • Your question is to something this question was about, but it will not be exactly the same (proven by the fact that you are asking something while the original question already is resolved)
  • Questions on the Community are found through google and searching the community. Answers aren't. When asking your own question separately, you are effectively helping others with the same question. Doing that in someone else's question isn't.

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