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 values 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.

Create the UI Action

  1. Create a new UI Action
  2. Set the Onclick value to a function called copy (or similar, your choice).
  3. Ensure the "Client" checkbox is checked.
copy();

Onclick block from UI Action

  1. Populate the script box with the following function:

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 that is run OnClick
function copy() 
    // Fields that you wish to copy from the current record
    var values = ["field_1", "field_2", "field_3"];

    // Table name for the new record
    var tableName = "incident";

    // Craft the new based url, using the table name 
    var base = "nav_to.do?uri="+tableName+".do?sys_id=-1%26sysparm_query=";

    // Itterate over our fields list, and append the names and values to the URL
	var url = "";
    for (var i = 0; i < values.length; i++) {
        url += "^" + values[i] + "=" + g_form.getValue(values[i]);
    }

    // Send the users window to the new URL
    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!