Importing data and Transforming variables to the newly created ritms - variables not inserting.

scotte
Tera Contributor

I am trying to create bulk RITMs using data from excel spreadsheet.  Most of the information is put in a form then we submit the form and the RITM is created and the variables shows up on the bottom of the RITM.    since most of these variables do not have a field to transform they show at at the bottom of the RITM as variables. 

 I put in an on before script when transforming data from excel. I get the REQ and RITM to create kind of.   But my main issue is getting the variable created and getting them on the RITM.....I am getting an error  'Failed to insert variable: ........

 

Not sure what I am missing in the script. Here is the script.  Any ideas why the variables are not created or showing on RITM.

(function transformEntry(source, target, map, log, isUpdate) {
    var requestID = source.request;  

    // If no request is linked, create a new one
    if (!requestID) {
        var request = new GlideRecord('sc_request');
        request.initialize();
        request.requested_for = source.requested_for || gs.getUserID();
        request.short_description = "Bulk Request"; 
        requestID = request.insert();
        log.info('Created Request: ' + requestID);
    }

    // Create the RITM
    var ritm = new GlideRecord('sc_req_item');
    ritm.initialize();
    ritm.cat_item = '53895c8bdb58270072a46693ca961910';  
    ritm.request = requestID;  
    ritm.quantity = 1;
    ritm.short_description = source.short_description || 'Bulk RITM Creation'; 

    var ritmID = ritm.insert();
    log.info('Created RITM: ' + ritmID);

    // Attach variables to RITM
    if (ritmID) {
        var variables = {
            'hire_type': source.HireType,
            'proper_name': source.ProperName,
            'alt_name': source.AlternateName,
            'employee_id': source.EmployeeID,
            'empl_dept': source.EmployeeDepartment,
            'manager': source.EmployeeManager,
            'HRBP_name': source.HRBPName,
            'start_date': source.StartDate,
            'cmdb_ci': source.ConfigItem,
            'network_login': source.NetworkLogin
        };

        for (var varName in variables) {
            var varValue = variables[varName];
            var variableSysID = getVariableSysID(varName); 

            if (varValue && variableSysID) {
                var grVarMtom = new GlideRecord('sc_item_option_mtom');  
                grVarMtom.initialize();
                grVarMtom.request_item = ritmID;  // Link to RITM
                grVarMtom.item_option_new = variableSysID;  // Link to Variable
                grVarMtom.value = varValue;  // Set Value
                grVarMtom.insert();
                log.info('Added variable ' + varName + ' with value: ' + varValue);
            } else {
                log.error('Failed to insert variable: ' + varName + ' - Check variable name in Service Catalog');
            }
        }
    }

    // Function to get the sys_id of the variable definition
    function getVariableSysID(varName) {
        var grVarDef = new GlideRecord('item_option_new');
        grVarDef.addQuery('name', varName);  
        grVarDef.query();
        if (grVarDef.next()) {
            log.info('Variable ' + varName + ' sys_id: ' + grVarDef.sys_id);
            return grVarDef.sys_id;
        }
        log.error('Variable ' + varName + ' not found');
        return null;
    }
})(source, target, map, log, action === 'update');

 

0 REPLIES 0