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

SumanKumarM
Tera Contributor

Hello ronro2,

I understand from above code I see 2 OS version records whose category startswith "server_os".

If your custom table "u_technical_services_lookup" has only above 2 records.

Please add below extra code

techServiceGR.orderByDesc("name") and retrieve the first record.

Apply the logic by Iterating through the list if there exists more than 2 records.

 

Please mark helpful, if it solves your issue.

 

Best Regards,

Suman.

 

Brad Bowman
Kilo Patron
Kilo Patron

I don't know if this matters, but since your 'request' script variable is current.request, the value is the sys_id of the request, so when you use it in the addQuery it can/should just be:

reqItemGR.addQuery('request', request);

Beyond this, what are you seeing in the logs - is the correct record in the lookup table retrieved by the GlideRecord? Do all of the logs seem to contain the correct information?

No, the logs show this very  "Ubuntu Server 18.04 LTS (Kontorstid)" instead of the chosen one. 

That makes sense.  I'm trying to understand how your lookup table is used.  How/where are you selecting a server OS record?  The most common approach is to use a Catalog Item variable, which should be the type of reference since you are picking a record from a table.  I'm guessing you don't have a field named sc_req_item on your lookup table so this line is ignored and it's always retrieving the same lookup record.  If your first log shows the expected Req Item ID, then you should just be able to dot-walk to the lookup table record Name, using your 'variable_name' 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.variable_name.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);

If you are not selecting the actual lookup table record in any variable or field, rather selecting a category and/or other related fields then you would still need the GlideRecord on the lookup table to get the Name, but use the correct criteria in the addQuery lines.