What is wrong with my ui action code? Triyng glideform on ui action.

luizmarcond
Tera Contributor

Hello everyone! I'm having a problem when trying to add a gliderecord to my UI action.

 

I've never done this before, where am I going wrong?

 

Basically, I need a query to be made on the records in a related table when I click on the approve button. If there are open records related to the same, an error message appears and it doesn't approve (all of this in the workspace).

I've also tried to add the codes to the other field (script), but it didn't work.

 

I tried some answers through gpt, but without success.
---------------------------------
UI ACTION:


SCRIPT field:

// Atualiza o estado para aprovado
current.state = new EventStateUtil().APPROVED_STATE;

// Grava a data e hora da aprovação
current.u_digitally_signed_in = new GlideDateTime();

// Salva o registro
current.update();

// Redireciona e mostra mensagem
action.setRedirectURL(current);
var escapedName = new sn_bcm.BCMUtils().escapeHTML(current.getValue("short_description"));
gs.addInfoMessage(gs.getMessage('<div style="display: inline-block;">{0} aprovado</div>', escapedName));

Workspace Client Script Field: 
function onClick() {
    var evento = g_form.getUniqueValue();

    var ga = new GlideAjax('sn_recovery.CheckOpenTasks');
    ga.addParam('sysparm_name', 'hasOpenTasks');
    ga.addParam('sysparm_event_id', evento);

    ga.getXMLAnswer(function(response) {
        if (response === "true") {
            g_form.addInfoMessage('Existem tarefas relacionadas ainda em aberto. Feche todas as tarefas antes de aprovar o evento.');
        } else {
            g_modal.confirm(
                "Declaro estar ciente de que as evidências incluídas estão de acordo com os testes realizados, comprometendo-me com a política interna de Resiliência Organizacional e disponível para auditorias.",
                function(confirmed) {
                    if (confirmed) {
                        gsftSubmit(null, g_form.getFormElement(), 'approve_event');
                    }
                }
            );
        }
    });
}

SCRIPT INCLUDE:
 
API Name: sn_recovery.CheckOpenTasks

var CheckOpenTasks = Class.create();
CheckOpenTasks.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    hasOpenTasks: function() {
        var eventId = this.getParameter('sysparm_event_id');

        var gr = new GlideRecord('sn_recovery_event_task');
        gr.addEncodedQuery("activated_plan.event=" + eventId + "^stateNOT IN9,3,4,7");
        gr.query();
        if (gr.next()) {
            return "true"; // <- Retorne string, não booleano
        }

        return "false"; // <- Sempre retorne algo
    },

    type: 'CheckOpenTasks'
});




3 REPLIES 3

k_lutz
Tera Expert

Hi Luiz,

The main issue seems to be in your script include...with the formatting of the query. This seems to be the problem:

gr.addEncodedQuery("activated_plan.event=" + eventId + "^stateNOT IN9,3,4,7");

 

May be better like: 

gr.addEncodedQuery("activated_plan.event=" + eventId + "^state NOT IN 9,3,4,7");

 

Or do the GlideRecord like:

var gr = new GlideRecord('sn_recovery_event_task');

gr.addQuery('activated_plan.event', eventId);

gr.addQuery('state', 'NOT IN', '9,3,4,7');

 

Hope that helps.

Ankur Bawiskar
Tera Patron
Tera Patron

@luizmarcond 

what debugging did you do?

Changes to workspace client script

1) use g_form.submit('yourActionNameHere')

Changes to script include

var CheckOpenTasks = Class.create();
CheckOpenTasks.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    hasOpenTasks: function() {
        var eventId = this.getParameter('sysparm_event_id');

        var gr = new GlideRecord('sn_recovery_event_task');
        gr.addEncodedQuery("activated_plan.event=" + eventId + "^stateNOT IN9,3,4,7");
		gr.setLimit(1);
        gr.query();
        if (gr.hasNext()) {
            return "true"; // <- Retorne string, não booleano
        }

        return "false"; // <- Sempre retorne algo
    },

    type: 'CheckOpenTasks'
});

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

Hello, Ankur! Thanks for your reply!

Im trying putting some message logs to validate some lines, but none works as expected.

I believe I must be making a mistake when inserting the code in the script field or workspace client script.

If I make a simple code, it works normally. Is it possible to call a gliderecord through the ui action?