Need Help with fix script/background script to update RITM 'Requested For"

MR1
Tera Contributor

Hi All,

 

I am trying to update the RITM Requested for field with the "opened by" variable. I need help with the background script to check and update a bunch of RITMs.

var grSIOM = new GlideRecord('sc_item_option_mtom');
grSIOM.addEncodedQuery("request_item.cat_item=c47374801b6b2150f761eb95604bcb5e^sc_item_option.item_option_new.name=v_requested_for^request_item.requested_for!=6816f79cc0a8016401c5a33be04be441^sys_created_onBETWEENjavascript:gs.dateGenerate('2021-09-27','00:00:00')@javascript:gs.dateGenerate('2024-05-28','23:59:59')");
grSIOM.orderBy('null');
grSIOM.setLimit(100);
grSIOM.query();
while (grSIOM.next()) {
    gs.info('request_item: ' + grSIOM.getValue('request_item'));
    gs.info('request_item.request.requested_for: ' + grSIOM.getValue('request_item.request.requested_for'));
    gs.info('sc_item_option.item_option_new: ' + grSIOM.getValue('sc_item_option.item_option_new'));
    gs.info('request_item.request.request_state: ' + grSIOM.getValue('request_item.request.request_state'));
}

 

In the above script, we are not updating any field but checking the values and that's returning as null.

 

Thanks.

 

 

4 REPLIES 4

Tabassum22
Tera Guru

Hi @MR1,

 

In the above script you are using grSIOM.getValue('request_item.request.requested_for'), which is incorrect because it is trying to access a nested field that does not exist directly on the sc_item_option_mtom table.

 

Here is the updated script which you can use 

var grRITM = new GlideRecord('sc_req_item');

grRITM.addEncodedQuery("cat_item=c47374801b6b2150f761eb95604bcb5e^requested_for!=6816f79cc0a8016401c5a33be04be441^sys_created_onBETWEENjavascript:gs.dateGenerate('2021-09-27','00:00:00')@javascript:gs.dateGenerate('2024-05-28','23:59:59')");
grRITM.query();

while (grRITM.next()) {
// Get the 'Opened by' user
var openedBy = grRITM.opened_by;

// Update the 'Requested For' field with the 'Opened by' user
grRITM.requested_for = openedBy;
//Uncomment the below to update the record if needed
//grRITM.update();

gs.info('Updated RITM: ' + grRITM.number + ' - Requested For: ' + grRITM.requested_for.name);
}

 

Please mark it helpful and try to give it a thumbs up if the above helps. Please accept the solution.

 

Thanks & Regards

Tabassum Sultana

 

MR1
Tera Contributor

 

Thank you for the feedback but the "Opened by" is a variable over here, not the OOB field.

mc22
Tera Contributor

Hope this code may help : 

// Initialize the GlideRecord for sc_req_item table
var grRITM = new GlideRecord('sc_req_item');

// Query for a specific RITM number
grRITM.addQuery('number', 'RITM0000013');
grRITM.query();

if (grRITM.next()) {
    // Get the sys_id of the RITM
    var ritmSysId = grRITM.getUniqueValue();

    // Initialize the GlideRecord to fetch variables
    var grVariables = new GlideRecord('sc_item_option_mtom');
    grVariables.addQuery('request_item', ritmSysId);
    grVariables.addQuery('sc_item_option.item_option_new.name', 'var_1'); // sample variable
    grVariables.addQuery('sc_item_option.value', 'val1'); //sample value
    grVariables.query();

    if (grVariables.next()) {
        // Retrieve the 'Opened by' variable value??
        var grOpenedBy = new GlideRecord('sc_item_option_mtom');
        grOpenedBy.addQuery('request_item', ritmSysId);
        grOpenedBy.addQuery('sc_item_option.item_option_new.name', 'opened_by'); // Adjust the variable name as needed
        grOpenedBy.query();

        if (grOpenedBy.next()) {
            var openedByValue = grOpenedBy.getValue('sc_item_option.value');
            // Update the 'Requested For' field on the RITM
           // grRITM.setValue('requested_for', openedByValue);
           // grRITM.update();
            gs.info('Updated RITM ' + ritmSysId + ' with Requested For: ' + openedByValue);
        } else {
            gs.info('Opened by variable not found for RITM ' + ritmSysId);
        }
    } else {
        gs.info('No matching RITM found.');
    }
} else {
    gs.info('No RITM found');
}

Also, have you tried to access the following tables aside from the variable ownership table?

1.  Variable Ownership [sc_item_option_mtom] - This table has below two main fields that we are going to use:

- Parent Item - store the RITM number in reference field from Requested items[sc_req_item] table.

- Dependent item - stores the sys_id in reference field from Options[sc_item_option] table.

2. Options[sc_item_option] - This table stores the Question(catalog variable) and the value entered by user. After mapping this table with Variable Ownership table using Dependent item field, we'll have access to all values entered by user for a particular question.

3. Requested Item[sc_req_item]- We are familiar with this one. We'll use this to map with "parent item" field from Variable ownership table. As result we'll have mapping of Parent item field with RITM number and we can access all the fields from Requested item table.

Link : https://www.servicenow.com/community/platform-analytics-articles/reporting-on-catalog-item-variables...






MR1
Tera Contributor

Hi,

Your script helped me partially but I am unable to fetch the Value from the sc_item_option table

MR1_0-1718903186770.png

 

Thanks