The CreatorCon Call for Content is officially open! Get started here.

Email script returns a static value in notification that is different from the value in the request

ronro2
Tera Contributor

Hello!

I have a notification that I want to trigger when a request from a specific order guide is set to Closed Complete. 

In this notification I want to return some values about the request, such as Server OS if it's a server. The problem is that it returns a static value all the time that doesn't reflect what was chosen in the request. I just don't understand why. 

Here is what was actually chosen from a table called "u_technical_services_lookup" in my request:

ronro2_0-1728814643545.png

 

Instead I always get "Ubuntu Server 18.04 LTS (Kontorstid)" like shown in the notification below:

ronro2_1-1728814731570.png

This very value,  "Ubuntu Server 18.04 LTS (Kontorstid)", also comes up in the system logs. "Ubuntu Server 18.04 LTS (Kontorstid)" comes from here in the same "u_technical_services_lookup" table: 

ronro2_2-1728814852959.png



Here is how the script looks: 

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {

    (function() {
        var request = current.request; // Hämta sc_request
        var osVersion = '';

        // Hämta alla sc_req_item under sc_request
        var reqItemGR = new GlideRecord('sc_req_item');
        reqItemGR.addQuery('request', request.sys_id);
        reqItemGR.query();

        while (reqItemGR.next()) {
            // Logga sc_req_item ID
            gs.log('Req Item ID: ' + reqItemGR.sys_id);

            // Hämta värde från u_technical_services_lookup baserat på u_category
            var techServiceGR = new GlideRecord('u_technical_services_lookup');
            techServiceGR.addQuery('u_category', 'STARTSWITH', 'server_os');
            techServiceGR.addQuery('sc_req_item', reqItemGR.sys_id); // Kontrollera att sc_req_item är kopplad
            techServiceGR.query();

            if (techServiceGR.next()) {
                // Logga teknisk tjänst ID och OS-version
                gs.log('Tech Service ID: ' + techServiceGR.sys_id);
                gs.log('OS Version: ' + techServiceGR.u_name);

                osVersion = techServiceGR.u_name; // Hämta värdet namn
                break; // Avsluta loopen när vi har hittat en matchning
            }
        }

        // Logga den slutliga OS-versionen
        gs.log('Final OS Version: ' + osVersion);

        // Använd template.print för att inkludera OS-versionen i email scriptet
        template.print('OS Version: ' + osVersion);
    })();

})(current, template, email, email_action, event);


In this example, you can see that the Technical Services Lookup table becomes a variable under a Request Item: 

ronro2_0-1728906464804.png




KEEP IN MIND that this Request (sc_request) creates several Request Items (sc_req_item) under and it comes not from a form, but a series of Rule bases. 

Also, there are plentiful of records regarding OS versions in the "u_technical_services_lookup" table with Category starting with "server_os".

17 REPLIES 17

The Lookup table is used as a variable somehow, as you can se under one of the request items under the request (parent):

ronro2_0-1728906527894.png

 

That's good.  Just replace 'variable_name' with the name of the Server OS variable, and you should see the correct lookup record Name.

i replaced it with u_name, but now the notification says undefined. Do you think I need some kind of glideRecord for the Lookup table? 

ronro2_0-1728908193136.png

 



What does your script look like now?

It looks like this: 

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {

    (function() {
        var request = current.request; // Hämta sc_request
        var osVersion = '';

        // Hämta alla sc_req_item under sc_request
        var reqItemGR = new GlideRecord('sc_req_item');
        reqItemGR.addQuery('request', request);
        reqItemGR.query();

        while (reqItemGR.next()) {
            // Logga sc_req_item ID
            gs.log('Req Item ID: ' + reqItemGR.sys_id);
            osVersion = reqItemGR.variables.u_name; // Hämta värdet namn
            break; // Avsluta loopen när vi har hittat en matchning
        }

        // Logga den slutliga OS-versionen
        gs.log('Final OS Version: ' + osVersion);

        // Använd template.print för att inkludera OS-versionen i email scriptet
        template.print('OS Version: ' + osVersion);
    })();

})(current, template, email, email_action, event);