We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

BUSINESS RULE NOT CREATING NEW RITM AND NOT ATTACHING TO EXISTING REQ

adaptivert
Giga Guru

Hello @everyone.  Good morning.  I need some help reviewing the business rule below and give me advise to correct the issue of the RITM not created and not added to an existing current REQ.  I appreciate the help.

 

------------------

(function executeRule(current, previous /*null when async*/) {
var catItemGr = new GlideRecord('sc_cat_item', 'Sailpoint IdN for Service Catalog');
catItemGr.get('name', 'Sailpoint Access Request');

try {
if (!current.variables.u_access_action || !current.variables.u_access_name) {
gs.info("Input validation failed <SP_SPNT_SN_INT_ManualConfigRITM>");
return;
}

gs.info("Business rule Line 11 <SP_SPNT_SN_INT_ManualConfigRITM> : " + current.variables.u_access_action.toString());

// Read the manual work table if access profile requires manual work
var accessTableGr = new GlideRecord('x_sap_intidn_manual_work_definition');
accessTableGr.addQuery('u_access_obj_name', current.variables.u_access_name.toString());
accessTableGr.query();

if (accessTableGr.next()) {
gs.info("Business rule Line 19 <SP_SPNT_SN_INT_ManualConfigRITM>: record matched");

// Create a new RITM record
var ritmGr = new GlideRecord('sc_cat_item');
ritmGr.name = catItemGr.name;
ritmGr.description = catItemGr.description;
ritmGr.insert();

gs.info("Line 27 <SP_SPNT_SN_INT_ManualConfigRITM> (ritm insert)");

// Add the RITM to the current active REQ
var reqRecord = new GlideRecord('sc_request');
reqRecord.addQuery('number', current.request.number);
reqRecord.query();
if (reqRecord.next())
{
    gs.info("Line 35 <SP_SPNT_SN_INT_ManualConfigRITM>-Parent REQ returned " + reqRecord.getRowCount() + " records");
    reqRecord.insert(ritmGr.sys_id);
    gs.info("Line 37 <SP_SPNT_SN_INT_ManualConfigRITM>-Parent REQ Insert");
    reqRecord.update();
    gs.info("Line 38 <SP_SPNT_SN_INT_ManualConfigRITM>-Parent REQ Update");
}

gs.info("Business rule Line 40 <SP_SPNT_SN_INT_ManualConfigRITM>");
gs.info("RITM created successfully. RITM sys_id <SP_SPNT_SN_INT_ManualConfigRITM: " + ritmGr.sys_id);
} else {
gs.info("No matching manual work definition found <SP_SPNT_SN_INT_ManualConfigRITM>");
}
} catch (err) {
gs.error("Error in business rule <SP_SPNT_SN_INT_ManualConfigRITM> : " + err);
}
})(current, previous);
---------------------
1 ACCEPTED SOLUTION

adaptivert
Giga Guru

Just sharing the code that I finally was able to make to work just in case it can be useful for others

--------------------

(function executeRule(current, previous /*null when async*/) {
var catItemGr = new GlideRecord('sc_cat_item');
catItemGr.get('name', 'Sailpoint Access Request');

try {
    if (!current.variables.u_access_action || !current.variables.u_access_name)
    {
        gs.info("Input validation failed <SP_SPNT_SN_INT_ManualConfigRITM>");
        return;
    }

    gs.info("Business rule Line 12 <SP_SPNT_SN_INT_ManualConfigRITM> : " + current.variables.u_access_action.toString());

    // Read the manual work table if access profile requires manual work
    var accessTableGr = new GlideRecord('x_sap_intidn_manual_work_definition');
    accessTableGr.addQuery('u_access_obj_name', current.variables.u_access_name.toString());
    accessTableGr.query();
    current.stage

    if (accessTableGr.next())
    {
        gs.info("Business rule Line 22 <SP_SPNT_SN_INT_ManualConfigRITM>: record matched");

        // Create a new RITM record
        var ritmGr = new GlideRecord('sc_req_item');
        ritmGr.initialize();
        ritmGr.cat_item = catItemGr.sys_id;
        ritmGr.request = current.request.sys_id;
        ritmGr.assignment_group = accessTableGr.u_assign_group.sys_id;
        gs.info("Business rule Line 30 <SP_SPNT_SN_INT_ManualConfigRITM>: " + ritmGr.assignment_group.name.toString());
        ritmGr.short_description = "Manual Configuration: " + current.variables.u_access_action
                                    + "For " + current.variables.u_access_name;
        ritmGr.description = "Request Item related to  " + current.number  
                                + " For " + current.variables.u_access_name;
        ritmGr.opened_by = current.opened_by.toString();
        ritmGr.requested_for = current.requested_for.toString();
        ritmGr.state = -5;
        ritmGr.insert();

        gs.info("Business rule Line 40 <SP_SPNT_SN_INT_ManualConfigRITM>");
        gs.info("RITM created successfully. RITM sys_id <SP_SPNT_SN_INT_ManualConfigRITM: " + catItemGr.sys_id);
    }
    else
    {
        gs.info("No matching manual work definition found <SP_SPNT_SN_INT_ManualConfigRITM>");
    }
}
catch (err) {
gs.error("Error in business rule <SP_SPNT_SN_INT_ManualConfigRITM> : " + err);
}
})(current, previous);

--------------------

View solution in original post

11 REPLIES 11

adaptivert
Giga Guru

Hello @Bert_c1 .  I was working on the code and modified it further as indicated below. on the update to the REQ, I am working to add another RITM record to it.

 

(function executeRule(current, previous /*null when async*/) {
var catItemGr = new GlideRecord('sc_cat_item');
catItemGr.get('name', 'Sailpoint Access Request');

try {
if (!current.variables.u_access_action || !current.variables.u_access_name) {
gs.info("Input validation failed <SP_SPNT_SN_INT_ManualConfigRITM>");
return;
}

gs.info("Business rule Line 11 <SP_SPNT_SN_INT_ManualConfigRITM> : " + current.variables.u_access_action.toString());

// Read the manual work table if access profile requires manual work
var accessTableGr = new GlideRecord('x_sap_intidn_manual_work_definition');
accessTableGr.addQuery('u_access_obj_name', current.variables.u_access_name.toString());
accessTableGr.query();

if (accessTableGr.next()) {
gs.info("Business rule Line 19 <SP_SPNT_SN_INT_ManualConfigRITM>: record matched");

    // Create a new RITM record
    //var ritmGr = new GlideRecord('sc_cat_item');
    catItemGr.name = "Manual Configuration: " + current.variables.u_access_action
                     + "For " + current.variables.u_access_name;
    catItemGr.description = "Request Item related to  " + current.number  
                     + "For " + current.variables.u_access_name;
    catItemGr.group = accessTableGr.u_assign_group.toString();
    catItemGr.insert();

    if (catItemGr.insert())
    {
        // Add the RITM to the current active REQ
        var reqRecord = new GlideRecord('sc_request');
        reqRecord.addQuery('sys_id', current.request.sys_id);
        reqRecord.query();
        if (reqRecord.next())
        {
            gs.info("Line 35 <SP_SPNT_SN_INT_ManualConfigRITM>-Parent REQ returned " + reqRecord.getRowCount() + " records");
            reqRecord.update(catItemGr.sys_id);
            gs.info("Line 37 <SP_SPNT_SN_INT_ManualConfigRITM>-Parent REQ Insert");
        }
    }
    else
    {
        gs.info("Line 42 <SP_SPNT_SN_INT_ManualConfigRITM>-Failed to Create RITM")
    };

    gs.info("Business rule Line 45 <SP_SPNT_SN_INT_ManualConfigRITM>");
    gs.info("RITM created successfully. RITM sys_id <SP_SPNT_SN_INT_ManualConfigRITM: " + catItemGr.sys_id);
}
else
{
    gs.info("No matching manual work definition found <SP_SPNT_SN_INT_ManualConfigRITM>");
}
}

catch (err) {
gs.error("Error in business rule <SP_SPNT_SN_INT_ManualConfigRITM> : " + err);
}
})(current, previous);

adaptivert
Giga Guru

@Bert_c1 .  I modified further modification with your inputs.

adaptivert
Giga Guru

@Bert_c1 .  It still did not create the new RITM and the REQ updated even though the logs indicated it pass through key steps such as 

Line 35 <SP_SPNT_SN_INT_ManualConfigRITM>-Parent REQ returned 1 records

Line 37 <SP_SPNT_SN_INT_ManualConfigRITM>-Parent REQ Insert

Business rule Line 59 <SP_SPNT_SN_INT_ManualConfigRITM>

RITM created successfully. RITM sys_id <SP_SPNT_SN_INT_ManualConfigRITM: 8053818edbffb300e90690b3db9619c4

 

Updated code below.

-------------

(function executeRule(current, previous /*null when async*/) {
var catItemGr = new GlideRecord('sc_cat_item');
catItemGr.get('name', 'Sailpoint Access Request');

try {
    if (!current.variables.u_access_action || !current.variables.u_access_name) {
    gs.info("Input validation failed <SP_SPNT_SN_INT_ManualConfigRITM>");
    return;
}

gs.info("Business rule Line 11 <SP_SPNT_SN_INT_ManualConfigRITM> : " + current.variables.u_access_action.toString());

// Read the manual work table if access profile requires manual work
var accessTableGr = new GlideRecord('x_sap_intidn_manual_work_definition');
accessTableGr.addQuery('u_access_obj_name', current.variables.u_access_name.toString());
accessTableGr.query();

if (accessTableGr.next()) {
gs.info("Business rule Line 19 <SP_SPNT_SN_INT_ManualConfigRITM>: record matched");

    // Create a new RITM record
    //var ritmGr = new GlideRecord('sc_cat_item');
    catItemGr.name = "Manual Configuration: " + current.variables.u_access_action
                + "For " + current.variables.u_access_name;
    catItemGr.description = "Request Item related to  " + current.number  
                + "For " + current.variables.u_access_name;
    catItemGr.group = accessTableGr.u_assign_group.toString();

    // Add the RITM to the current active REQ
    var reqRecord = new GlideRecord('sc_request');
    reqRecord.addQuery('sys_id', current.request.sys_id);
    reqRecord.query();
    if (reqRecord.next())
    {
        gs.info("Line 35 <SP_SPNT_SN_INT_ManualConfigRITM>-Parent REQ returned " + reqRecord.getRowCount() + " records");
        reqRecord.update(catItemGr.sys_id);
        gs.info("Line 37 <SP_SPNT_SN_INT_ManualConfigRITM>-Parent REQ Insert");
    }
    else
    {
        gs.info("Line 41 <SP_SPNT_SN_INT_ManualConfigRITM>-Failed to Create RITM")
    };

    catItemGr.insert();
/*
    if (catItemGr.insert())
    {
        // Add the RITM to the current active REQ
        var reqRecord = new GlideRecord('sc_request');
        reqRecord.addQuery('sys_id', current.request.sys_id);
        reqRecord.query();
        if (reqRecord.next())
        {
            gs.info("Line 35 <SP_SPNT_SN_INT_ManualConfigRITM>-Parent REQ returned " + reqRecord.getRowCount() + " records");
            reqRecord.update(catItemGr.sys_id);
            gs.info("Line 37 <SP_SPNT_SN_INT_ManualConfigRITM>-Parent REQ Insert");
        }
    }
    else
    {
        gs.info("Line 42 <SP_SPNT_SN_INT_ManualConfigRITM>-Failed to Create RITM")
    };
*/
    gs.info("Business rule Line 59 <SP_SPNT_SN_INT_ManualConfigRITM>");
    gs.info("RITM created successfully. RITM sys_id <SP_SPNT_SN_INT_ManualConfigRITM: " + catItemGr.sys_id);
}
else
{
    gs.info("No matching manual work definition found <SP_SPNT_SN_INT_ManualConfigRITM>");
}
}

catch (err) {
gs.error("Error in business rule <SP_SPNT_SN_INT_ManualConfigRITM> : " + err);
}
})(current, previous);
-----------------------

I now see you changed code to reference sc_cat_item table, when you want to create a record in the sc_req_item table.  Proper code for that follows.

 

 

    // Create a new RITM record
    var ritmGr = new GlideRecord('sc_cat_item');
    ritmGr.name = "Manual Configuration: " + current.variables.u_access_action
                + "For " + current.variables.u_access_name;
    ritmGr.description = "Request Item related to  " + current.number  
                + "For " + current.variables.u_access_name;
    ritmGr.group = accessTableGr.u_assign_group.toString();

    // Add the RITM to the current active REQ
    var reqRecord = new GlideRecord('sc_request');
    reqRecord.addQuery('sys_id', current.request.sys_id);
    reqRecord.query();
    if (reqRecord.next())
    {
        gs.info("Line 35 <SP_SPNT_SN_INT_ManualConfigRITM>-Parent REQ returned " + reqRecord.getRowCount() + " records");
        ritmGr.request = reqRecord.sys_id;
		// no need to update the sc_request table, but need to set sc_req_item.request to request's sys_id
//		recRecord.update(catItemGr.sys_id);
        gs.info("Line 37 <SP_SPNT_SN_INT_ManualConfigRITM>-Parent REQ Insert");
    }
	// now create the sc_req_item record
	var recInsertResult = ritmGr.insert();
    gs.info("After Line 37 <SP_SPNT_SN_INT_ManualConfigRITM>-REQ Item Insert, result = " + recInsertResult);

 

 

use of 'current.request.sys_id' is suspect without knowing what table the BR is defined on. And how the 'request' field is defined. If a reference to the sc_request table, then that line should be:

 

 

    reqRecord.addQuery('sys_id', current.request);

 

 

You first querried sys_request based on the 'number' field. good luck, very confusing on what your goal is.

adaptivert
Giga Guru

@Bert_c1 .  Just  curious question.  Do I have to use cartjs to create a new RITM?