I have an run script in workflow which creates an ritm, i need to convert it to flow, Can someone?

Harithapola
Tera Contributor
(function execute(inputs, outputs) {
gs.log('#### Create Database Security - Distributed RITM activity in SystemOne workflow; current.number is ' + current.number);


try {
    gs.log('#### Create Database Security - Distributed RITM activity in SystemOne workflow; current.number is ' + current.number + ' and we are in the Try block and current.u_request_type is ' + current.u_request_type);

 
    var catItem = " sysid"; //sysID of "Database Security - Distributed" cat item


    var secFormType = current.u_request_type;
    //var secFormType = current.secFormType;
    secFormType = secFormType.toString();

    var requestedFor;
    //requestedFor = requestedFor.toString();

    if (secFormType == 'New Hire') {
        requestedFor = current.u_multiple_users;
        requestedFor = requestedFor.toString();
    } else {
        requestedFor = current.variables.numusersMultipleUsers;
        requestedFor = requestedFor.toString();
    }

    workflow.scratchpad.requested_for = requestedFor;

    gs.log('#### Create Database Security - Distributed RITM activity in SystemOne workflow; current.number is ' + current.number + ' and we are in the Try block and current.u_request_type is ' + current.u_request_type + ' and requestedFor is ' + requestedFor);

 
    var cartid = gs.generateGUID();
    var cart = new sn_sc.CartJS(cartid);  ///added parameter to fix the problem stated INC2485728
    var request = {
        'sysparm_id': catItem,  
        'sysparm_quantity': '1',
        "sysparm_cart_name": cartid, //added parameter to fix the problem stated INC2485728
        'variables': {
            'secFormType': secFormType, // added by Patrick Latella per INC2250900
            'newhireHiringManager': current.variables.newhireHiringManager.toString(),
            'transferNewManager': current.variables.transferNewManager.toString(),
            'chkModal': current.variables.chkModal.toString(),
            'numusersUsersType': current.variables.numusersUsersType.toString(),
            'chkBotAccount': current.variables.chkBotAccount.toString(),
            'botAccount': current.variables.botAccount.toString(),
            'numusersMultipleUsers': current.variables.numusersMultipleUsers.toString(),
            'userLandIDs': current.variables.userLandIDs.toString(),
            'effDate': current.variables.effDate.toString(),
            'chkBotEnabled': current.variables.chkBotEnabled.toString(),

            //Set other variables
            'local_db_service_account': current.variables.local_db_service_account.toString(),
            'open_txt_username': current.variables.open_txt_username.toString(),
            'serviceAcctOwner': current.variables.serviceAcctOwner.toString(),
            'dbextDatabase': '899b9f1e1bffac90fd762f876e4bcbfc', //System One record on the [u_database_forms] table
            'dbextChkNoDatabase': current.variables.dbextChkNoDatabase.toString(),
            'dbsecAppName': 'SystemOne',
            'dbsecPlatform': 'Sybase',
            'dbsecEnvironment': 'PROD',
            'dbsecInstance': 'SYB_CADBURY2',
            'dbsecDBName': 'gmpriv2dp',
            'chcAccessType': current.variables.chcAccessType.toString(),
            'dbsecJobFunction': current.variables.dbsecJobFunction.toString(),
            'dbsecReasonForAccess': current.variables.dbsecReasonForAccess.toString(),
            //'dbsecDescAccess':dbsecDescAccess.toString(),
            'dbADGroup': current.variables.dbADGroup.toString(),
            'dbsecSameGroupAsDBOwner': current.variables.dbsecSameGroupAsDBOwner.toString(),
            'dbsecDBOwner': current.variables.dbsecDBOwner.toString(),
            'chkMultipleDatabases': current.variables.chkMultipleDatabases.toString(),
            'dbsecMultipleDatabaseInfo': current.variables.dbsecMultipleDatabaseInfo.toString(),
            'policy_data_mart': false

        }
       
       
    };

    var cartDetails = cart.orderNow(request);
    var cartID = cartDetails.request_id;

    gs.log('#### Create Database Security - Distributed RITM activity in SystemOne workflow; current.number is ' + current.number + ' and we are in the Try block and current.u_request_type is ' + current.u_request_type + ' and requestedFor is ' + requestedFor + ' and cartID is ' + cartID);

    workflow.scratchpad.req_id = cartID;

} catch (exception) {

    gs.log('#### Create Database Security - Distributed RITM activity in SystemOne workflow; current.number is ' + current.number + ' and we are in the Catch block and current.u_request_type is ' + current.u_request_type + ' and requestedFor is ' + requestedFor + ' and cartID is ' + cartID + ' and the message is ' + ':' + exception.message);

} finally {
    gs.log('#### Create Database Security - Distributed RITM activity in SystemOne workflow; current.number is ' + current.number + ' and we are in the Finally block and current.u_request_type is ' + current.u_request_type + ' and requestedFor is ' + requestedFor + ' and cartID is ' + cartID);

   }
})(inputs, outputs);
9 REPLIES 9

@Harithapola 

what do you mean by log the activity?

how are you planning to trigger the flow?

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,

You can call a subflow that first performs the logging as needed, and then submits the Request as per suggestion by Ankur.

Ravi Gaurav
Giga Sage
Giga Sage

Hi @Harithapola 
I found a holistic PDF that will help.
PFA 

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


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

Hi @Ravi Gaurav ,

Thanks for it and could you please help me understand after 5 sec of wait time, how do we update the newly created ritm(through flow action)

I have the below script in update DB run script:

 

var reqID = workflow.scratchpad.req_id;
var requestedFor = workflow.scratchpad.requested_for;

var newRec = new GlideRecord("sc_req_item");
newRec.addQuery("request", reqID);
newRec.query();

if(newRec.next()) {          
    newRec.request = current.request;
    newRec.opened_by = current.opened_by;
    newRec.u_multiple_users = requestedFor;
    newRec.update();
}

Hi @Ravi Gaurav 

I tried your solution from the pdf but its creating new ritm with new request number instead of creating with same request. Please find code below.

 

(function execute(inputs, outputs) {
    gs.log('#### Flow Action: Create Database Security - Distributed RITM; inputs.number = ' + inputs.number);

    try {
        var catItem = "6a279c080fbd2200471ce498b1050e02"; // database security distributed Catalog Item sys_id
        var secFormType = inputs.u_request_type.toString();
        var requestedFor;

        if (secFormType === 'New Hire') {
            requestedFor = inputs.u_multiple_users.toString();
        } else {
            requestedFor = inputs.numusersMultipleUsers.toString();
        }

        var cartid = gs.generateGUID();
        var cart = new sn_sc.CartJS(cartid);

        var request = {
            'sysparm_id': catItem,
            'sysparm_quantity': '1',
            'sysparm_cart_name': cartid,
            'sc_request':inputs.existingRequestSysId,
            'variables': {
                'secFormType': secFormType,
                'newhireHiringManager': inputs.newhireHiringManager.toString(),
                'transferNewManager': inputs.transferNewManager.toString(),
                'chkModal': inputs.chkModal.toString(),
                'numusersUsersType': inputs.numusersUsersType.toString(),
                'chkBotAccount': inputs.chkBotAccount.toString(),
                'botAccount': inputs.botAccount.toString(),
               // 'numusersMultipleUsers': inputs.numusersMultipleUsers.toString(),
                'userLandIDs': inputs.userLandIDs.toString(),
                'effDate': inputs.effDate.toString(),
                'chkBotEnabled': inputs.chkBotEnabled.toString(),
                'local_db_service_account': inputs.local_db_service_account.toString(),
                'open_txt_username': inputs.open_txt_username.toString(),
                'serviceAcctOwner': inputs.serviceAcctOwner.toString(),
                'dbextDatabase': '899b9f1e1bffac90fd762f876e4bcbfc',
                'dbextChkNoDatabase': inputs.dbextChkNoDatabase.toString(),
                'dbsecAppName': 'SystemOne',
                'dbsecPlatform': 'Sybase',
                'dbsecEnvironment': 'PROD',
                'dbsecInstance': 'SYB_CADBURY2',
                'dbsecDBName': 'gmpriv2dp',
                'chcAccessType': inputs.chcAccessType.toString(),
                'dbsecJobFunction': inputs.dbsecJobFunction.toString(),
                'dbsecReasonForAccess': inputs.dbsecReasonForAccess.toString(),
                'dbADGroup': inputs.dbADGroup.toString(),
                'dbsecSameGroupAsDBOwner': inputs.dbsecSameGroupAsDBOwner.toString(),
                'dbsecDBOwner': inputs.dbsecDBOwner.toString(),
                'chkMultipleDatabases': inputs.chkMultipleDatabases.toString(),
                'dbsecMultipleDatabaseInfo': inputs.dbsecMultipleDatabaseInfo.toString(),
                'policy_data_mart': false
            }
        };


cart.addToCart(request);
var cartDetails = cart.submitOrder(request);
var newRequestId = cartDetails.request_id;


var ritmGR = new GlideRecord('sc_req_item');
ritmGR.addQuery('request', newRequestId);
ritmGR.orderByDesc('sys_created_on');
ritmGR.query();

if (ritmGR.next()) {
  ritmGR.request = inputs.existingRequestSysId; 
ritmGR.update();
}
//var cartDetails = cart.orderNow(request);
    //var cartID = cartDetails.request_id;
   

  outputs.req_id = cartID; // Output the new RITM sys_id
    outputs.requested_for = requestedFor;
}  
         

    catch (e) {
        gs.error('#### Flow Action: Error creating RITM: ' + e.message);
        outputs.error = e.message;
    }

})(inputs, outputs);