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

Bert_c1
Kilo Patron

Your first line in the function is wrong

 

var catItemGr = new GlideRecord('sc_cat_item''Sailpoint IdN for Service Catalog');

should be

var catItemGr = new GlideRecord('sc_cat_item');

 

adaptivert
Giga Guru

Thank you, @Bert_c1 .  I think I did the suggestion earlier and it was not executing good.  Let me try again with you advise.

Hi @Bert_c1 .  Change the definition as you suggested and the RITM is not created and REQ not updated.

This section of code has problems:

 

 

 

		// 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");
		}

 

 

where you have reqRcord.insert() (note: there are no arguments to insert()) and an "insert()" makes no sense on a record that exists. So remove that and leave the 'update()'. But then what field are you updating on the sc_request table. The record in sc_req_item uses a reference field to link the record to one in sc_request. just above the ritmGr.insert() add the code section

 

		// 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");
			ritmGr.request = reqRecord.sys_id;
		}

before the 'ritmGr.insert();'