Using newly created record number after insert.

kowens
Tera Contributor

I am having issues with recording a ticket number in the current record after an insert. I have a "Convert to Task" button on my incident form so that if an incident comes in that should have been a request, this can be used to convert the incident into a task and cancel the incident. Everything works great with the exception of "relating" the two records. I am able to pass the current incident number into the task, but unable to obtain the new ticket number to add to my closing of the incident. The code I have thus far is below, and is a UI Action. Any help would be GREATLY appreciated!

function convertToTask() {

// Generate Task

var task = new GlideRecord("sc_task");

task.initialize();

task.short_description = g_form.getValue("short_description");

task.cmdb_ci = g_form.getValue("cmdb_ci");

task.assignment_group = g_form.getValue("assignment_group");

task.assigned_to = g_form.getValue("assigned_to");

task.comments = g_form.getValue("comments");

task.priority = g_form.getValue("priority");

task.description = g_form.getValue("description");

task.state = 1;

task.u_contact = g_form.getValue("caller_id");

task.u_related_incident = g_form.getDisplayValue("sys_id"); //This is the part I am having issues with. No matter what I use, this field is blank in the new task. u_related_incident is a reference field.

var taskSysID = task.insert();

// Close Incident

g_form.checkMandatory = false;

g_form.setValue("close_code", "Cancelled");

g_form.setValue("u_related_request", taskSysID);

g_form.setValue("close_notes", "Incident has been automatically updated to a request.");

g_form.setValue("state", 7);

gsftSubmit(gel('sysverb_update_and_stay'));

// Jump over to new Task

top.location.href =

"https://tractorsupplydev.service-now.com/nav_to.do?uri=/sc_task.do%3Fsys_id%3D" + taskSysID +"%26sysparm_record_target%3Dsc_task%26sysparm_record_row%3D1%26sysparm_record_rows%3D662%26sysparm_record_list%3Dactive%253Dtrue%255EORDERBYDESCnumber";

1 ACCEPTED SOLUTION

Hi Kevin,



Try this,



task.u_related_incident = g_form.getUniqueValue();



Regards,


Sagar


View solution in original post

13 REPLIES 13

kowens
Tera Contributor

Code used...


find_real_file.png


Hi Kevin,



Try this,



task.u_related_incident = g_form.getUniqueValue();



Regards,


Sagar


Glad to know you got your answer:


but i would like to advice you   on your formatting,which is quite ugly as of now. The script should be formatted, for future developers ease of readability.



this code maybe prone to errors in the future as you have mixed server and client scripts in the same function. I encourage you to read   more about UI actions:



http://wiki.servicenow.com/index.php?title=Using_a_UI_Action_to_Create_a_Record#gsc.tab=0


UI actions




You should check the client side as you wish to include api such as g_form


all these syntax should be   taken inside the onclick function. once the onclick funciton ends, make your server side codes run, eg gliderecord.



And such that you may use task.field_name = current.field.getDisplayValue(), etc   and finally use task.u_related_incident = current.sys_id;



kowens
Tera Contributor

Thank you Sagar!!! That worked!!!