Copy Values from another Table into a Form without saving?

shawn_h
Tera Guru

Hi folks,

Hope you can help!

I'm looking for a way to copy field entries I have in a custom table into an incident form, but ideally I'd want to be able to copy the entries and not save them/create a record, more so they appear within the fields and the user has the option to save/edit them before continuing. 

I currently have an extra referenced field linking to my custom table, and a UI action that pulls in those fields. The downside is I've done this wrong as it saves and creates the record also.

Essentially i'm looking for a way of emulating the functionality of templates, but cannot actually use templates for this purpose (unless it's possible to link templates into this other table, and call them that way, but I'd prefer another solution if there is one!)

find_real_file.png

16 REPLIES 16

Abhijeet Upadhy
Tera Expert

UI Action:

onclick: copy_values()

Script:

function copy_values()

{

var table = "table_name"; // name of the custom table comes here

 

var ga = new GlideAjax("copy_values")

ga.addParam("sysparm_name", "copy_values");

ga.addParam("sysparm_table", table);

ga.addParam("sysparm_sys_id", g_form.getVaue("name of reference field comes here"));

ga.getXMLWait();

var answer = ga.getAnswer();

 

answer = answer.split(",");

 

g_form.setValue("field1 on incident form", answer[0]);

g_form.setValue("field2 on incident form", answer[1]);

//and so on

return false;

}

 

Script Include:

Client Callable is true

Name: copy_values

Script:

var copy_values = Class.create();
mandatory.prototype = Object.extendsObject(AbstractAjaxProcessor, {
copy_values: function() {

var table = this.getParameter(sysparm_table);

var sys_id = this.getParameter(sysparm_sys_id);

var answer = "";

 

var gr = new GlideRecord(table);

gr.addQuery("sys_id", sys_id);

gr.query();

if(gr.next())

{

answer = answer + gr.field_name1 + "," + gr.field_name2 + ",";//all the fields value comes here comma seperated

}

 

return answer;
},
type: 'copy_values'
});

 

NOW YOU CAN MARK THE ANSWER AS CORRECT ;)..

Ahmed Hmeid1
Kilo Guru

The easiest way is to do it via the URL on the client.

 

Eg, to create an incident with a short description and description populated you could navigate to:

 

/incident.do?sys_id=-1&sysparm_query=short_description=hello&description=longdescription

 

So you can do this all via a client side UI action. This will loop through the fields on the form, add them to the new URL if they contain a value, and then open the new window up.

 

Set the onclick function to "copyForm()"

 

Then the code will be as follows:

function copyForm() {

var elements = g_form.elements;

var url = "/incident.do?sys_id=-1&sysparm_query=";

var queryParts = [];

for (var i = 0; i < elements.length; i++) {
var field = elements[i].fieldName + "";
if (g_form.getValue(field)) {
queryParts.push(field + "=" + g_form.getValue(field));
}
}


url += queryParts.join("&");
window.open(url, '_blank');
}