- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-08-2019 08:28 AM
Hi
I have an approval workflow (that works fine). If the workflow is approved there's a step to either create or update a referenced record. My problem is that, whenever I execute the glideRecord update() function it creates a new record instead updating the existing record. Here's a cut down version of the code:
// This is the record type I want to creat or update
var vp = new GlideRecord('x__cicsv_t1_validation_plan');
var newVPFlag = true;
// Does a VP record already exist for this Project?
gs.info("Looking for VP record for Project ID: " + current.u_project_identifier);
vp.addQuery('u_project_identifier', current.u_project_identifier);
vp.query();
// Do we have an existing record? Set the flag if we do.
while (vp.next()) {
newVPFlag = false;
gs.info("Found VP Record ID: " + vp.sys_id);
}
// Initialize a new record if existing records weren't fouund
if (newVPFlag) {
vp.initialize();
vp.u_project_identifier = current.u_project_identifier;
}
// Here there's a load of logic to set the values in the vp object
// based on the values in current.
if (newVPFlag) {
gs.info("Creating VP Record with Project ID=" + vp.u_project_identifier);
var vp_id = vp.insert();
} else {
gs.info("Updating VP Record: " + vp.sys_id);
vp.update("Validation Plan update");
}
From the system log I can see:
Found VP Record ID: 2bc870f2db5000108f1daf26489619ee
followed by:
Updating VP Record: 2bc870f2db5000108f1daf26489619ee
So I know my validation plan record was found and the vp.update(...) was executed, but when I look in the Validation Plan table a new record has been inserted. This should only happen if the validation plan record doesn't exist.
Can anyone explain why?
Regards
David
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-08-2019 08:42 AM
Your issue is within:
if (newVPFlag) {
gs.info("Creating VP Record with Project ID=" + vp.u_project_identifier);
var vp_id = vp.insert();
} else {
gs.info("Updating VP Record: " + vp.sys_id);
vp.update("Validation Plan update");
}
It's because the vp.update, is outside your:
while (vp.next()) {
I've tested this to confirm. Placing the update within the while will fix the issue.
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
---
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-08-2019 08:30 AM
Hi there,
Technically, if the update is on a record which cannot be found, an insert is done instead. So debug first if the record to update, is actually found/valid.
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
---
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-08-2019 08:39 AM
Hi,
Can you change your logic a bit on the flags? Please try this script
// This is the record type I want to creat or update
var vp = new GlideRecord('x__cicsv_t1_validation_plan');
var newVPFlag = false;
// Does a VP record already exist for this Project?
gs.info("Looking for VP record for Project ID: " + current.u_project_identifier);
vp.addQuery('u_project_identifier', current.u_project_identifier);
vp.query();
// Do we have an existing record? Set the flag if we do.
if(vp.next()) {
newVPFlag = true;
gs.info("Found VP Record ID: " + vp.sys_id);
}
// Initialize a new record if existing records weren't fouund
if (!newVPFlag) {
vp.initialize();
vp.u_project_identifier = current.u_project_identifier;
}
// Here there's a load of logic to set the values in the vp object
// based on the values in current.
if (!newVPFlag) {
gs.info("Creating VP Record with Project ID=" + vp.u_project_identifier);
var vp_id = vp.insert();
} else {
gs.info("Updating VP Record: " + vp.sys_id);
vp.update("Validation Plan update");
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-08-2019 08:42 AM
Your issue is within:
if (newVPFlag) {
gs.info("Creating VP Record with Project ID=" + vp.u_project_identifier);
var vp_id = vp.insert();
} else {
gs.info("Updating VP Record: " + vp.sys_id);
vp.update("Validation Plan update");
}
It's because the vp.update, is outside your:
while (vp.next()) {
I've tested this to confirm. Placing the update within the while will fix the issue.
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
---
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-08-2019 09:30 AM
Thanks Mark, that works perfectly now.
Regards
David