Email script returns a static value in notification that is different from the value in the request
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2024 02:21 PM - edited 10-14-2024 04:47 AM
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:
Instead I always get "Ubuntu Server 18.04 LTS (Kontorstid)" like shown in the notification below:
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:
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:
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".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2024 05:43 AM - edited 10-14-2024 05:52 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2024 05:57 AM
I now get null in the notification. 😞 I feel like we are so close.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2024 06:01 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2024 06:26 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2024 06:34 AM
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
}
}