How does Insert and Stay work?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-29-2012 04:39 PM
I'm trying to update our Clone button where I don't have to
manually copy over each property from one object to another (ie
newrecord.description = current.description). So I'm looking at the insert and stay
ui action and I don't fully understand how it works.
Code:
doInsertAndStay();
function doInsertAndStay() {
var
saveMe = current;
if
(typeof current.number != 'undefined' && current.number)
current.number = ""; //
generate a new number
current.insert();
action.setRedirectURL(saveMe);
}
So the function starts off with:
var saveMe = current;
this leads me to believe that current =
original record and saveMe =
duplicated record. At the end of the function you're redirected to saveMe. But if that's the case, why do you use current.insert() to create the new record instead of saveMe.insert() to create the new
record?If you add a gs.addInfoMessage at the end of the script with both saveMe.sys_id and current.sys_id
you get the exact same value for both.
The reason this is an issue is I'm trying to use the
service-now code for copying over attachments: Packages.com.glide.ui.SysAttachment.copy('sourcetable', 'sys_id',
'destinationtable', 'sys_id');
If I create a new GlideRecord and manually copy each
property over individually and insert the GlideRecord the SysAttafchment.copy
works like a charm. It's only when I use the above function that I can't get it
to work, so I'm thinking my understanding of how sys_ids are created is
incorrect, and that the way I'm using that attachment function is trying to
copy from the wrong sys_id.
Thanks for your time!
Here's an example of the code I'm writing:
doInsertAndStay();
function doInsertAndStay() {
var saveMe = current;
current.number = ""; // generate a new number
current.insert();
gs.addInfoMessage("Request " +
saveMe.sys_id + " has been duplicated..");
gs.addInfoMessage("Request " +
current.sys_id + " created");
Packages.com.glide.ui.SysAttachment.copy("change_task",
saveMe.sys_id, "change_task",
current.sys_id);
//current.update(); tried
with and without this, also tried saveMe.update();, and flipping saveMe and
current
action.setRedirectURL(saveMe);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-30-2012 06:40 AM
Hi zschneider,
I have tried your code on demo instance and it is working properly. Can you explain the exact issue.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-30-2012 06:57 AM
If you have an attachment it will not copy over from the old ticket to the new ticket using the code above.
something about: Packages.com.glide.ui.SysAttachment.copy("change_task", saveMe.sys_id, "change_task", current.sys_id);
Is not working properly.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-02-2012 09:05 AM
I can see no good reason for the
var saveMe = current;
...
action.setRedirectURL(saveMe);
dance. As they're both just references to the same GlideRecord object.
But I expect your real problem is that you're not storing the sys_id of the original 'current' before it gets updated.
current.sys_id is also a pointer to an object, and after the insert() it will have a new value, even if you think you've saved it into an old one:
// this does not do what you expect
var orig_sys_id = current.sys_id;
...
current.insert();
// this prints the new sys_id, not the original one
gs.print(orig_sys_id);
gs.print(current.sys_id);
Try this instead:
var orig_sys_id = current.getValue('sys_id');
...
current.insert();
// this will print the original sys_id
gs.print(orig_sys_id);
gs.print(current.sys_id);
(This is a common gotcha, though it's usually encountered in loops, when trying to save sys_ids into an array.)
Some of my colleagues prefer this other style:
var orig_sys_id = current.sys_id + '';
which also works because it forces the conversion to a string.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-29-2014 06:23 AM
Just to add to this you can also do
var orig_sys_id = String(current.sys_id);
Which also converts to a string and in my opinion makes it clearer as to what you are doing for any one else reading your code.