How do I update a record that was just inserted in a server script?

gjz
Mega Sage

I have a simple requirement but for some reason I can't figure out how to do it.

I have two tables, TableA and TableB.  I want to update each table with the number of the record created in the other table.

For example:

TableA.short_description = 'some description ' + TableB.number;

TableB.short_description = 'some description ' + TableA.number;

I thought it would be as simple as the example below but TableB's number is not getting updated in TableA's short description. 

var a = new GlideRecord('tableA');
a.initialize();
a.short_description = 'something here';
set a bunch of field values
a.insert();

var b = new GlideRecord('tableB');
b.initialize();
b.short_description = 'something here' + ' - ' + a.number;
b.insert();

//Here's where I'm stuck, I'm trying to update the short description on the record just inserted into tableA with the number from the record just created in tableB.
a.short_description = 'something here' + ' - ' + b.number;
a.update();

 Can anyone help me out?

 

 

1 ACCEPTED SOLUTION

Sam Dey1
Kilo Guru

Hi,

As per my understanding I dont think you can do a GideRecord update() after the GlideRecord has been closed with an insert().

 

So my suggestion is do something like this - 

var gr = new GlideRecord("incident");
gr.initialize();
gr.short_description = "This is the first record";
gr.insert();



var yGr = new GlideRecord("problem");
yGr.initialize();
yGr.short_description = "In Problem - "+gr.getValue("short_description");
yGr.insert();


var xGr = new GlideRecord("incident");
xGr.get(gr.sys_id.toString());

xGr.short_description = yGr.number+" ** "+xGr.short_description;
xGr.update();

This is working for me. 

 

Cheers,

Sam

 

Mark correct/helpful if this helped you 🙂

View solution in original post

3 REPLIES 3

Sam Dey1
Kilo Guru

Hi,

As per my understanding I dont think you can do a GideRecord update() after the GlideRecord has been closed with an insert().

 

So my suggestion is do something like this - 

var gr = new GlideRecord("incident");
gr.initialize();
gr.short_description = "This is the first record";
gr.insert();



var yGr = new GlideRecord("problem");
yGr.initialize();
yGr.short_description = "In Problem - "+gr.getValue("short_description");
yGr.insert();


var xGr = new GlideRecord("incident");
xGr.get(gr.sys_id.toString());

xGr.short_description = yGr.number+" ** "+xGr.short_description;
xGr.update();

This is working for me. 

 

Cheers,

Sam

 

Mark correct/helpful if this helped you 🙂

Thanks Sam, adding the third query to get the record back fixed the issue.

Thanks mate, can you please also mark the answer as Correct. 

 

Cheers,

Sam