Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Need help implementing “Copy Item” functionality in RITM using UI Action + Flow Designer (Platform a

sriram 1
Tera Contributor

Hi everyone,

I need to implement a “Copy Item” feature for Request Items (RITMs), irrespective of the state(open, closed, etc.)

When click on "Copy item" , the flow should trigger and open up new record with all the copied values of the old record. Can someone help me on this. 

 

I just need help on

1.Steps of the Flow Designer

2.Script for the UI Action to trigger Flow

 

Thanks in advance,

3 REPLIES 3

Vishal_Jaiswal
Mega Guru

Hi @sriram 1 ,

 

Please follow below steps:

Step 1: Create the Subflow
- Navigate to Flow Designer and click New > Subflow.
- Name your subflow (e.g., Copy RITM).
- Click the Inputs button in the sidebar. Create one input:
- Name: Old RITM Sys_ID
- Type: String
- Click the Outputs button. Create one output:
- Name: New RITM Sys_ID
- Type: String

Step 2: Look Up the Old RITM
- Add an Action: Look Up Record.
- Table: Requested Item [sc_req_item]
- Conditions: [Sys ID] [is] [Input -> Old RITM Sys_ID]


Step 3: Copy the RITM and its Variables
- Add an Action: Script. (Search for the Script action under "Core").
- Input Variables: Click the + to add an input variable to this script step.
- Name: oldRITM
- Value: Drag and drop the Requested Item Record pill from your (1) Look Up Record step.
- Output Variables: Click the + to add an output variable for this script step.
- Name: newRITMID
- Type: String

Script: Paste the following script into the script box.

(function(inputs, outputs) {
    var oldRITMRecord = inputs.oldRITM;
    var variablesJSON = {};

    var grVarOwner = new GlideRecord('sc_item_option_mtom');
    grVarOwner.addQuery('request_item', oldRITMRecord.getUniqueValue());
    grVarOwner.query();

    while (grVarOwner.next()) {
        var grVar = new GlideRecord('sc_item_option');
        if (grVar.get(grVarOwner.sc_item_option)) {
            var varName = grVar.item_option_new.name.toString();
            // Get the *value* of the variable (e.g., 'US')
            var varValue = grVar.value.toString();

            if (varName) {
                variablesJSON[varName] = varValue;
            }
        }
    }

    var cart = new sn_sc.CartJS();
    var item = {
        'sysparm_id': oldRITMRecord.cat_item.toString(),
        'sysparm_quantity': '1',
        'variables': variablesJSON,
        'sysparm_requested_for': oldRITMRecord.requested_for.toString()
    };

    var cartResponse = cart.order(item);

    if (cartResponse && cartResponse.request_id) {
        var newRITM = new GlideRecord('sc_req_item');
        newRITM.addQuery('request', cartResponse.request_id);
        newRITM.query();
        if (newRITM.next()) {
            outputs.newRITMID = newRITM.getUniqueValue();
        }
    }
})(inputs, outputs);

Step 4: Assign the Subflow Output
- Below your script action, click the Assign value button for your Subflow Output (New RITM Sys_ID).
- Drag and drop the newRITMID data pill from your (2) Script step into the value box.
- Click Done and Publish your Subflow.

Step 5: Script for the UI Action
- Now, create the "Copy Item" button that will run this Subflow.
- Navigate to System Definition > UI Actions and click New.
- Name: Copy Item
- Table: Requested Item [sc_req_item]
- Action name: copy_ritm
- Show update: Checked
- Client: Unchecked
- Form button: Checked
- Condition: Leave this blank
- Script: Paste the following script.

(function() {
    try {
        var inputs = {};
        inputs['old_ritm_sys_id'] = current.getUniqueValue();
        var subflowName = 'global.copy_ritm'; // <-- Change this to your subflow's name
        var outputs = sn_fd.Flow.executeSubflow(subflowName, inputs);
        var newRITMsysid = outputs['new_ritm_id'];
        if (newRITMsysid) {
            gs.addInfoMessage("Item " + current.number + " has been copied to a new request.");
            action.setRedirectURL(newRITMsysid); // This opens the new RITM form
        } else {
            gs.addErrorMessage("Could not copy the item. The flow did not return a new RITM.");
        }
    } catch (e) {
        gs.addErrorMessage("An error occurred while copying the item: " + e.message);
    }
})();

 

Regards,

Vishal

Ankur Bawiskar
Tera Patron
Tera Patron

@sriram 1 

when UI action is clicked you want to copy the RITM and create a new RITM for same Item which is present in current RITM?

If yes then you can create a subflow and make it trigger from server side UI action

Then use "Submit Catalog Item Request" flow action and let new RITM trigger

Once new RITM is triggered the associated flow for this catalog item will trigger automatically

Submit Catalog Item Request action 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

@sriram 1 

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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