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

Sharique Azim
Mega Sage

Hi Kevin,



Is u_request a reference field, referencing sc_task?   If not it wont work..



Btw are you using this script in some single place, then i am afraid to say that you have mixed server and client scripts.


BTW, if a sc_task record is created then a stray record should be avoided. place it under a req and ritm record.


The way they implemented "requests" here is that they are using straight Catalog Tasks, and are not using the req or ritm records at all. I did have u_request as a reference field and it was not posting the results, meaning that the field appeared to be blank.


Can you please post the screenshot for better clarity