Copy fields value from one record to another on the same table while copying the requst

Aruna13
Tera Contributor

Hi,

 

I have a requirement for my custom table to have the copy functionality. When user clicks on "Copy AR Request" button in the form, then it should create a new record and some field values like short description, category fields should populate with the values from the request where we are creating the new request. 

 

Currently the code which I have written is directing me to new record while clicking on the button, but the values of some fields are not auto populating as expected. Can anyone please check and let me know what i am missing here?

 

Below is my UI action:

 

function CopyARRequest() {
    var url = '';
    var caller = g_form.getValue('u_caller_id');
    var u_email_from = g_form.getValue('u_email_from');
    var unit_code = g_form.getValue('u_unit_code');
    var u_reference_number = g_form.getValue('u_reference_number');
    var cat = g_form.getValue('u_category');
    var subcat = g_form.getValue('u_subcategory');
    var contact = g_form.getValue('u_contact_type');
    var priority = g_form.getValue('priority');
    var ag = g_form.getValue('assignment_group');
    var sd = g_form.getValue('short_description');

    g_form.setValue('u_unit_code', unit_code);
    g_form.setValue('short_description', sd);
    g_form.setValue('u_category', cat);

    url = encodeURI('u_sr_shared_ar.do?sys_caller=' + caller + '&sys_email_from=' + u_email_from + '&sys_unit_code=' + unit_code + '&sys_ref_no=' + u_reference_number + '&sys_category=' + cat + '&sys_subcategory=' + subcat + '&sys_contactType=' + contact + '&sys_priority=' + priority + '&sys_assignmentGroup=' + ag + '&sys_shortdesc=' + sd);

    g_navigation.openPopup(url);
}
Aruna13_0-1718735065637.png

 

Thanks in Advance!!

 

@Ankur Bawiskar can you please help me this? 

 

1 ACCEPTED SOLUTION

briannice
Kilo Sage

Hi

 

You do not need a client UI action to achieve this. I created a working example for the incident table on my PDI which you can use to create your own. You can find a screenshot and the code below.

 

You need to make the following adaptions to the script:

  • Update the fields you want to copy.
  • Update the name of the table of the GlideRecord.

 

 

// Create new record for the table that you want.
var newIncidentGr = new GlideRecord("incident");

// Initialize the new record.
newIncidentGr.initialize();

// Copy values from the current record to the new record.
newIncidentGr.setValue("short_description", current.getValue("short_description"));
newIncidentGr.setValue("description", current.getValue("description"));

// Save the new record.
newIncidentGr.insert();

// Open the new record.
action.openGlideRecord(newIncidentGr);

 

 

briannice_0-1718737338205.png

 

View solution in original post

4 REPLIES 4

briannice
Kilo Sage

Hi

 

You do not need a client UI action to achieve this. I created a working example for the incident table on my PDI which you can use to create your own. You can find a screenshot and the code below.

 

You need to make the following adaptions to the script:

  • Update the fields you want to copy.
  • Update the name of the table of the GlideRecord.

 

 

// Create new record for the table that you want.
var newIncidentGr = new GlideRecord("incident");

// Initialize the new record.
newIncidentGr.initialize();

// Copy values from the current record to the new record.
newIncidentGr.setValue("short_description", current.getValue("short_description"));
newIncidentGr.setValue("description", current.getValue("description"));

// Save the new record.
newIncidentGr.insert();

// Open the new record.
action.openGlideRecord(newIncidentGr);

 

 

briannice_0-1718737338205.png

 

Thank you and it worked fine! 

 

I also have some logical doubt here. In this case, the record is getting created whenever i click on the UI action on the form. So what if the user mistakenly clicked on this button and it created unnecessary record on the table right? and what if there is a scenario they want to create a duplicate request only when they submit the form after clicking on the ui action?

 

Any ideas/sugesstions?

 

Thanks in advance!

Hi @Aruna13 

 

In that case, you do need a client UI action. I added the code and a screenshot below.

 

I also think that this page in de documentation will help you understand what is going on.

 

Kind regards,

Brian

 

// This function will run on the client.
function copyIncidentsWithConfirm() {
	// Confirm if the user wants to copy the incidents
	var check = confirm("Copy incidents?");
	if (check) {
		gsftSubmit(null, g_form.getFormElement(), 'copy_incident_confirm');
	} else {
		return false;
	}
}

// Check if the script is running on the server.
if (typeof window == "undefined") {
	copyIncidents();
}

// This function will run on the server.
function copyIncidents() {
	// Create new record for the table that you want.
	var newIncidentGr = new GlideRecord("incident");

	// Initialize the new record.
	newIncidentGr.initialize();

	// Copy values from the current record to the new record.
	newIncidentGr.setValue("short_description", current.getValue("short_description"));
	newIncidentGr.setValue("description", current.getValue("description"));

	// Save the new record.
	newIncidentGr.insert();

	// If the action was called on a new record, save that record.
	// Else save the record if changes have been made.
	if (current.isNewRecord()) {
		current.insert();
	} else  {
		current.update();
	}

	// You can choose which record you want to open. Comment out one of the following below.-
	
	// Open original incident
	// action.openGlideRecord(current);
	
	// Open copied incident
	// action.openGlideRecord(newIncidentGr);

	// Open incident list
	// actionHandler.setRedirectURL("incident_list.do");
}

 

briannice_0-1718780649292.png

 

 

Aruna13
Tera Contributor

hi @briannice ,

 

Thank you very much and it works perfectly!