Why does my glideRecord update keep creating new records?

David165
Mega Expert

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

1 ACCEPTED SOLUTION

Mark Roethof
Tera Patron
Tera Patron

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

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

View solution in original post

4 REPLIES 4

Mark Roethof
Tera Patron
Tera Patron

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

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Alikutty A
Tera Sage

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 Roethof
Tera Patron
Tera Patron

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

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Thanks Mark, that works perfectly now.

Regards

David