Copying record data to a new record with out saving in ServiceNow

Copying record data to a new record with out saving in ServiceNow

We recently had a requirement to copy data from an existing record to a new record, which traditionally is not super hard, you could use a client script, a business rule, you name it.

However, the issue arose that traditionally when you do this the new record gets inserted. This wasn't going to work for us as we had a flow that kicked off immediately after the record was inserted. This is behaviour we did not want, we wanted the data copied to a new record with out it being saved.

To achieve this...

We used a UI Action to harvest the variables on button click, and pass these using URL navigation to a new record. The fields from the URL automatically get populated into the form. It worked quite well and you can see the simple layout below.

  1. Firstly, create a UI Action, with an Onclick value called copy (or other). Make sure the "Client" checkbox is checked.
copy();
Onclick block from UI Action

2. Secondly, populate the script box with the following. The "Values" variable contains a list of field names that you would like harvested and copied to the new record. Replace the "tableName" with your desired table.

function copy() {
    var values = ["field_1", "field_2", "field_3"];
    var tableName = "incident";
    var base = "nav_to.do?uri="+tableName+".do?sys_id=-1%26sysparm_query=";
	var url = "";
    for (var i = 0; i < values.length; i++) {
        url += "^" + values[i] + "=" + g_form.getValue(values[i]);
    }
    top.window.location = base+encodeURIComponent(url);
}
Script Block from UI Action

3. Add any conditional logic you would like to control when the button should be available. As you can see below, we didn't want it available on new Records, and only available for a certain table.

current.getTableName() == "tableName" && !current.isNewRecord()
Condition block from UI Action

4. That's it! Testing time! Enjoy!