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

Does the Req Item ID log show the sys_id of the correct RITM? These requests have multiple RITMs, but you are effectively only returning one since you are breaking after the first one.  You need to add the u_name variable either as an addQuery, or check each RITM to get the one with the value.

        var reqItemGR = new GlideRecord('sc_req_item');
        reqItemGR.addQuery('request', request);
        reqItemGR.addNotNullQuery('variables.u_name');
        reqItemGR.query();

        while (reqItemGR.next()) {

 

Did the Final OS Version log also show undefined?  This should be the sys_id of the lookup record, you would add .name (or whatever the field name for the Name column is) or try getDisplayValue instead

 

osVersion = reqItemGR.getDisplayValue('variables.u_name'); // Hämta värdet namn

 

 

I now get null in the notification. 😞 I feel like we are so close. 

Is that with the variables.u_name not null query added?  Is the GlideRecord returning the correct RITM - the one that has the u_name variable, and its value?

Yes. I've added the not null query. Maybe it's better to  check each RITM to get the one with the value? As you've stated before? This is how the code looks now: 

(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.addNotNullQuery('variables.u_name');
        reqItemGR.query();

        while (reqItemGR.next()) {
            // Logga sc_req_item ID
            gs.log('Req Item ID: ' + reqItemGR.sys_id);
            osVersion = reqItemGR.getDisplayValue('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);

 

It should be the same either way, it all depends on what the logs are telling you.  Here is the other approach:

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.number);
    if (reqItemGR.variables.u_name) {
        osVersion = reqItemGR.getDisplayValue('variables.u_name'); // Hämta värdet namn
        break; // Avsluta loopen när vi har hittat en matchning
    }
}