Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

UI Action (using client and server side) to avoid update on record, just change value on field

Walmag Castro S
Tera Guru

Hello.

I´m facing a challenger requirement.

We have the bellow UI Action that changes value on field "assignment_group" according to the queries bellow.

The goal is: avoid to update the record. We need to only show the result of the query, according to the conditions, onto the field "assignment_group". So, after user´s decision to really change the field to assigned group shown, he can save and update the record.

It possible to avoid the update, any kind of "setValue"... ? I´m out of ideas.

I already used "current.setAbortAction(true);", but it does not match our requirements.
Thanks in advance.

 

 

function OnAutoAssignGroup() {
    var confirmation = confirm(getMessage("Are you sure you want to make the change? If you are not a member of the assignment group, you will not be able to edit the form."));
    if (confirmation == false) {
        return false; // Abort change if answer is  "no"
    }
    //Call the UI Action and skip the 'onclick' function
    gsftSubmit(null, g_form.getFormElement(), "u_auto_assign_group"); //MUST call the 'Action name' set in this UI Action
}

//Code that runs without 'onclick'
//Ensure call to server-side function with no browser errors
if (typeof window == 'undefined') {
    runServer();
}


//Server-side function
function runServer() {
    //CONFIGURATION ITEM
 
    var ciGR = new GlideRecord('cmdb_ci_business_app');
    ciGR.addQuery('name', current.cmdb_ci.name);
    ciGR.query();

    //CATEGORY AND SUBCATEGORY
    var tableGR = new GlideRecord('dl_u_assignment');
    tableGR.addQuery('category', current.category);
    tableGR.addQuery('subcategory', current.subcategory);
    tableGR.query();

    current.setWorkflow(false);

    if (current.contact_type == 'redphone') {
        current.assignment_group = gs.getProperty('xpto.GROUPS.xxx');
        current.assigned_to = '';
        action.setRedirectURL(current);
        current.update();

    } else {

        if (ciGR.next()) {
            if (ciGR.support_group != '' && current.assignment_group != ciGR.support_group) {
                current.assignment_group = ciGR.support_group;
            } else if (current.assignment_group == ciGR.support_group) {
                gs.addInfoMessage('Assignment Group already correctly assigned.');
            } else {
                if (tableGR.next()) {
                    if (current.assignment_group != tableGR.assignment_group) {
                        current.assignment_group = tableGR.assignment_group;
                    } else {
                        gs.addInfoMessage('Assignment Group already correctly assigned.');
                    }

                } else {
                    current.assignment_group = '';
                    gs.addInfoMessage('No Assignment Group.');
                }
            }
            current.assigned_to = '';
            action.setRedirectURL(current);
            current.update();
        } else {
            if (tableGR.next()) {
                if (current.assignment_group != tableGR.assignment_group) {
                    current.assignment_group = '';
                    current.assigned_to = '';
                    current.assignment_group = tableGR.assignment_group;
                } else {
                    gs.addInfoMessage('Assignment Group already correctly assigned.');
                }
                action.setRedirectURL(current);
                current.update();
            } else {
                current.assignment_group = '';
                gs.addInfoMessage('No Assignment Group.');
                action.setRedirectURL(current);
            }
        }
    }
}

 

1 ACCEPTED SOLUTION

You can't use initialize function

        autoAssignedGroupGA.addParam('sysparm_name''initialize');

 

Leave the initialize as it is. And create a new function in the script include and call that function.


Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

8 REPLIES 8

You can't use initialize function

        autoAssignedGroupGA.addParam('sysparm_name''initialize');

 

Leave the initialize as it is. And create a new function in the script include and call that function.


Please mark this response as correct or helpful if it assisted you with your question.

Thank you so much, Sanjiv.

I renamed the function.

Amit Gujarathi
Giga Sage
Giga Sage

HI @Walmag Castro S ,
I trust you are doing great.
Please find the modified script as given below :

// Code to run without 'onclick'
// Ensure call to server-side function with no browser errors
if (typeof window == 'undefined') {
    runServer();
}

// Server-side function
function runServer() {
    // CONFIGURATION ITEM
    var ciGR = new GlideRecord('cmdb_ci_business_app');
    ciGR.addQuery('name', current.cmdb_ci.name);
    ciGR.query();

    // CATEGORY AND SUBCATEGORY
    var tableGR = new GlideRecord('dl_u_assignment');
    tableGR.addQuery('category', current.category);
    tableGR.addQuery('subcategory', current.subcategory);
    tableGR.query();

    // Set the workflow to false to avoid unnecessary actions
    current.setWorkflow(false);

    if (current.contact_type == 'redphone') {
        current.assignment_group = gs.getProperty('xpto.GROUPS.xxx');
        current.assigned_to = '';
        gs.addInfoMessage('Assignment Group will be set to: ' + current.assignment_group);
    } else {
        if (ciGR.next()) {
            if (ciGR.support_group != '' && current.assignment_group != ciGR.support_group) {
                current.assignment_group = ciGR.support_group;
                gs.addInfoMessage('Assignment Group will be set to: ' + current.assignment_group);
            } else if (current.assignment_group == ciGR.support_group) {
                gs.addInfoMessage('Assignment Group already correctly assigned.');
            } else {
                if (tableGR.next()) {
                    if (current.assignment_group != tableGR.assignment_group) {
                        current.assignment_group = tableGR.assignment_group;
                        gs.addInfoMessage('Assignment Group will be set to: ' + current.assignment_group);
                    } else {
                        gs.addInfoMessage('Assignment Group already correctly assigned.');
                    }
                } else {
                    gs.addInfoMessage('No Assignment Group.');
                }
            }
            current.assigned_to = '';
        } else {
            if (tableGR.next()) {
                if (current.assignment_group != tableGR.assignment_group) {
                    current.assignment_group = tableGR.assignment_group;
                    gs.addInfoMessage('Assignment Group will be set to: ' + current.assignment_group);
                } else {
                    gs.addInfoMessage('Assignment Group already correctly assigned.');
                }
            } else {
                gs.addInfoMessage('No Assignment Group.');
            }
        }
    }
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Thanks, Amit. But it didn´t work. The info message shows the sys_id, but it does not populate the field assignment_group.