- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2024 11:33 AM
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:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2024 12:04 PM - edited 06-18-2024 12:05 PM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2024 12:04 PM - edited 06-18-2024 12:05 PM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2024 05:37 PM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2024 12:06 AM
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");
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2024 04:18 PM