Pulling the sys_id of a record that hasn't been inserted yet and writing to another field on a different table

Ken Lombard
Giga Guru

I have a request that I'm wondering if it is functionally possible.  They would like a button on the Idea form that will move the Idea into another state (easy enough), and then direct the user to a new Project form, without actually inserting a record yet.  I've been able to accomplish that part, but what I would also like to do is copy back the sys_id of that record that has not yet been inserted to the task field on the Idea.  That way, if they insert the project record, it will be linked to the Idea.  

Here's the code for the UI Action as it is currently:

var projGr = new GlideRecord('pm_project');
projGr.newRecord();
projGr.setValue('phase', 'Concept');
current.task = projGr.sys_id;
current.state = '6';
current.update();
action.openGlideRecord(projGr);

This will do everything we need, short of writing back the project sys_id to the task field on the Idea record.  It seems like a sys_id is assigned to a record before insertion, so it may just be that the database cannot commit that value to the task field until the record has been inserted.  I do already have a solution that will insert first and then write back the task value to the Idea record, but was just curious if what I mentioned above was possible.

 

1 ACCEPTED SOLUTION

Figured out a workaround.  I ended up storing the sys_id of the Idea onto a custome reference field in the project, then set up an after BR to run after insert of the project and if there is a value in that field, it writes it back to that Idea task field. 

View solution in original post

5 REPLIES 5

Periyasamy P
Tera Guru

You can use setNewGuid() to generate sys_id and copy the new sys_id in local variable and use it in scripts.

 

For ex,

var projGr = new GlideRecord('pm_project');
var newSysID = projGr.setNewGuid();

Tried that, but it still isn't writing back the sys_id to the task field on the Idea record, even if I save the project record that is initiated. 

For me, new sys_id is getting saved in expected place. But it is not opening the new record which i am trying to insert bcz this sys_id is not available in DB.

var inc = new GlideRecord("incident");
inc.initialize();
var newSysID = inc.setNewGuid();
inc.setValue("short_description", "Testig.. setNewGuid");

current.setValue("u_label", newSysID);
current.update();
action.openGlideRecord(inc);

find_real_file.png

Interesting.  So, I can get the sys_id value to save to another field, but not the task field.  I wonder if it is because it is a reference field that can't hold the value.