Searching for data based on incident number

Francivaldo
Tera Contributor

Hello, in this study that I did over two days, I created a catalog item with 3 string fields, free text type, so that the person can type the incident number and thus bring up the short description and the incident description. I am aware that there are ways to do this without having to create a catalog client script or an include script, but I did it this way for a challenge that was proposed, so that I could go deeper and better understand the relationship between a GlideAjaz and catalog client script.

Francivaldo_0-1733516189158.png

 

Script Include:

 

var DetalhesIncident = Class.create();
DetalhesIncident.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getDescription: function() {
        var getIncident = this.getParameter('sysparm_incident');

        var obj = {};

        var inci = new GlideRecord('incident');
        inci.addQuery('number', getIncident);
        inci.query();

        if (inci.next()) {

            obj.campo2 = inci.description.getValue();
            obj.campo3 = inci.short_description.getValue();

        }
        var log = JSON.stringify(obj);
        //gs.addInfoMessage('Retorno objeto: ' + log + ' | Retorno parâmetro: ' + getIncident);
        return JSON.stringify(obj);
    },

    type: 'DetalhesIncident'
});
 

 

Francivaldo_1-1733516189158.png

 

Catalog Client Script:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var ga = new GlideAjax('DetalhesIncident'); // Nome do Script Include
    ga.addParam('sysparm_name''getDescription'); // Método
    ga.addParam('sysparm_incident', newValue); // Parâmetro correto
    ga.getXML(callback);

    function callback(response) {
        // Obtendo a mensagem (comments) do XML de resposta
        var comments = response.responseXML.documentElement.getAttribute("answer");

        // Convertendo a string JSON em um objeto
        var obj = JSON.parse(comments);

        // Atribuindo as mensagens às variáveis
        var campo_2 = obj.campo2; // "Unable to access the shared folder. Please provide access."
        var campo_3 = obj.campo3; // "Unable to access"

        g_form.setValue('campo_2', campo_2);
        g_form.setValue('campo_3', campo_3);

    }
}
Francivaldo_2-1733516189295.png

 

Francivaldo_3-1733516189156.png

 

The challenge and reaching the result this way was fun. I believe there are ways to create better and more dynamic code, but for now I think this works very well.

 

If the post is helpful, please give it a thumbs up and comment so we can improve together in this world of ServiceNow.

0 REPLIES 0