trying to Populate Catalog tasks by cost center

sarahjane19
Tera Contributor

Good Morning 

 

I have written a script in a  create catalog task in the legacy workflow to it should be getting a variable from the RITM is it is being create on and using that variable to populate the assignment group for the task. However, I can see from the logging it is able to retrieve the variable but then says it can't find the RITM and the assignment group on the task is left blank. 

as it gets the Variable from the RITM I don't understand why it says it then can see it to create the Assignment group.

 

here is the script 

 

(function executeRule(current) {
    if (!current.request_item) {
        gs.info(" Catalog Task is missing 'request_item' reference.");
        return;
    }

    var ritm = current.request_item.getRefRecord();

    if (!ritm || !ritm.isValidRecord()) {
        gs.info(" Could not find valid RITM record from Catalog Task.");
        return;
    }

    var lineManagerSysId = ritm.variables.employee_line_manager_string;

    if (!lineManagerSysId) {
        gs.info("⚠️ No line manager specified in variable 'employee_line_manager_string'");
        return;
    }

    var user = new GlideRecord('sys_user');
    if (!user.get(lineManagerSysId)) {
        gs.info(" Could not find sys_user record for Line Manager sys_id: " + lineManagerSysId);
        return;
    }

    var costCentre = user.getDisplayValue('cost_center');
    gs.info(" Line Manager Cost Centre: " + costCentre);

    switch (costCentre) {
        case 'W015': current.assignment_group = 'f32fd89fdb5fb3008a66304d3b961950'; break; // Midlands
        case 'W507': current.assignment_group = '2fa52bec1b87c210ec5865f2b24bcba7'; break; // H IT
        case 'W038': current.assignment_group = 'c26e985fdb5fb3008a66304d3b9619da'; break; // North
        case 'W044': current.assignment_group = 'a5502c53db9fb3008a66304d3b961967'; break; // Construction South
        case 'W060': current.assignment_group = '41ce509fdb5fb3008a66304d3b9619c9'; break; // W&W
        case 'W008': current.assignment_group = 'f5c0a093db9fb3008a66304d3b9619d7'; break; // Interiors
        case 'W002': current.assignment_group = 'd26fdc9fdb5fb3008a66304d3b961982'; break; // London and East
        case 'W500':
        case 'W501':
        case 'W502':
        case 'W503':
        case 'W504':
        case 'W505':
        case 'W508':
            current.assignment_group = '78ead85bdb5fb3008a66304d3b9619b9'; break; // Holdings
        default:
            gs.info("⚠️ No assignment group mapped for cost centre: " + costCentre);
    }
})(current);
2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@sarahjane19 

did you add logs and see what came in log?

I will suggest to debug the switch statement.

you can store the sysId in variable and then at the end set the group

You should be use before insert/update business rule on sc_task

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi there this is the error that is triggered   ("

 Could not find valid RITM record from Catalog Task.");