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.

View solution in original post

8 REPLIES 8

SanjivMeher
Mega Patron

This line should take care of not allowing the update. Am I correct in saying that, when users says no to the confirmation, you dont want to update the assignment?

 

    if (confirmation == false) {
        return false; // Abort change if answer is  "no"
    }

You are correct. However, the user needs to check the newly assigned group by the server-side script.
If the user says "Yes", the script runs populating the assignment_group and updating it. But the requirement is: the user needs to see the new value, and then save manually if agrees to the new value.

I would suggest making a GlideAjax call from the UI action after the confirmation alert and call a script include which is client callable. Return the sysid of the group to the UI action and the use g_form.setValue() to set the value on the form, which will not save the form, but will just set the value.

Hello, Sanjiv. Thanks for your response.
I´m facing a problem: after calling the script include, sysparm values are undefined. I can´t figure it out. Can you help me?

I´ll show you the scripts:

 

CLIENT SCRIPT:

 

function OnAutoAssignGroup() {
    var ci = g_form.getValue('cmdb_ci');
    var category = g_form.getValue('category');
    var subcategory = g_form.getValue('subcategory');
    var contact_type = g_form.getValue('contact_type');
    var assignment_group = g_form.getValue('assignment_group');

    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"
    } else { // CONFIRMATION YES
        if (contact_type == "redphone") {
            alert("caiu no if pois é redphone");
            var confirmRedphone = confirm("Canal é Redphone. Alterar para o grupo XPTO?");
            if (confirmRedphone == false) {
                return false;
            } else {
                g_form.setValue('assigned_to', '');
                g_form.setValue('assignment_group', "8c1f64521bf74d10b7a7fc49cd4bcb28");
                g_form.save();
                return;
            }
        }

        // SOLUTION TO HAVE THE GROUP IN THE MESSAGE FOR THE USER TO CONFIRM ------------       START
        var autoAssignedGroupGA = new GlideAjax('AutoAssignGroupButton');
        autoAssignedGroupGA.addParam('sysparm_name', 'initialize');
        autoAssignedGroupGA.addParam('sysparm_ci', ci);
        autoAssignedGroupGA.addParam('sysparm_category', category);
        autoAssignedGroupGA.addParam('sysparm_subcategory', subcategory);
        autoAssignedGroupGA.addParam('sysparm_assignment_group', assignment_group);
        autoAssignedGroupGA.getXML(responseAssignedGroup);
    }

    function responseAssignedGroup(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        var confirmAutoAssignedGroup = confirm("Quer mesmo mudar para o grupo " + answer + "?");

        if (confirmAutoAssignedGroup == false) {
            return false; // Abort autoAssignment if answer is  "no"
        } else {
            // SOLUTION TO HAVE THE GROUP IN THE MESSAGE FOR THE USER TO CONFIRM ------------       END

            //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');
        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);
            }
        }
    }
    gs.eventQueue('incident.assigned.to.group', current, 'Incident assigned to my group', '');
}
 ___________________________________________________________________________
SCRIPT INCLUDE
 
var AutoAssignGroupButton = Class.create();
AutoAssignGroupButton.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    initialize: function() {
        var ci = this.getParameter('sysparm_ci');
        var category = this.getParameter('sysparm_category');
        var subcategory = this.getParameter('sysparm_subcategory');
        var assignment_group = this.getParameter('sysparm_assignment_group');

        var ciGR = new GlideRecord('cmdb_ci_business_app');
        ciGR.addQuery('name', ci);
        ciGR.query();

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

        current.setWorkflow(false);

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

                } else {
                    assignment_group = '';
                    gs.addInfoMessage('No Assignment Group.');
                }
            }
            current.assigned_to = '';
            action.setRedirectURL(current);
            current.update();
        } else {
            if (tableGR.next()) {
                //current.setWorkflow(false);
                if (assignment_group != tableGR.assignment_group) {
                    current.assignment_group = '';
                    current.assigned_to = '';
                    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);
            }
        }
    },

    type: 'AutoAssignGroupButton'
});