UI action to copy/clone a record

Tony Flower
Tera Contributor

We have a table within a scoped app that has the 'Serial number' field set as 'unique' in the dictionary.

 

We need to provide the operator with a simple 'Clone' button that displays a popup prompting for the new serial number, then copies the entire record to a new one with only that one field changed.  Replicating what the 'Insert' or 'Insert and Stay' buttons do to clone the record seems straightforward enough, but how do we get a simple popup to capture this value and pass it to the server part of the UI action script? 

 

This seem to be a simple enough ask, but we can't find any examples in the base platform that don't involve complex UI pages.  Can someone point us in the right direction?

 

 

 

 

gizmo.png

 

 

 

sdfsdf

2 REPLIES 2

Rahul Kumar17
Tera Guru

Hi,

To create a simple popup that prompts for a new serial number and passes it to the server part of the UI action script, you can follow these steps:

  1. Create a new UI action on the table where you want to add the 'Clone' button.
  2. Set the "Action name" field to something like "Clone Record" and the "Client" field to "true".
  3. In the "Script" field, add the following code:

 

function cloneRecord() {
  var newSerialNumber = prompt("Enter the new serial number:");
  if (newSerialNumber) {
    var gr = new GlideRecord('your_table_name');
    gr.addQuery('serial_number', current.serial_number);
    gr.query();
    if (gr.next()) {
      gr.serial_number = newSerialNumber;
      var newGr = gr.insert();
      var url = newGr.getLink(true);
      gs.addInfoMessage('Record cloned successfully: ' + url);
    }
  }
}

 

This code will create a prompt that asks the user to enter a new serial number. If the user enters a value and clicks "OK", the script will create a new GlideRecord object for the current table and add a query for the current record's serial number. If a matching record is found, it will update the serial number with the new value, insert the new record into the table, and display a message with a link to the new record.

4. Save the UI action and add it to the form layout.

5. Test the 'Clone' button by clicking on it and entering a new serial number.

Note: Replace "your_table_name" with the actual name of the table where the 'Serial number' field is set as 'unique' in the dictionary. Also, make sure to test the UI action thoroughly before deploying it to production.

 

Thanks,

Rahul Kumar

If my response helped please mark it correct and close the thread.

Thanks,
Rahul Kumar

Hi Rahul,

 

That is really helpful. A little bit to tweaking to separate the client part of the script from the server part, and replicating the Insert & Stay approach of using current.insert(), and it works nicely.

 

function cloneRecord() {
    var newSerialNumber = prompt("Enter the new Serial number:");
    if (newSerialNumber) {
        g_form.setValue("serial_number", newSerialNumber);
        gsftSubmit(null, g_form.getFormElement(), 'clone_record');
    }
}

if (typeof window == 'undefined')
    insertClone();

function insertClone() {
	var saveMe = current;
    current.insert();
    action.setRedirectURL(saveMe);