Why is GlideRecord update causing duplicate primary key error?

MBarrott
Mega Sage

The issue is tied to the last couple lines of the script, the setValue and update. 

 

If I create an entirely new GlideRecord for the table then it works fine, but I thought initialize() should clear out the GlideRecord for re-use?

 

Any help would be appreciated. 

 

// ********** find and add new stakeholders to register **********
// declare gliderecord and execute query against stakeholder group
var grStakeGrpMem = new GlideRecord('sys_user_grmember');
grStakeGrpMem.addEncodedQuery('group=25dcde621b58a850f30dfc03cc4bcb53'); // EDI Applications Managers group (test sample)
grStakeGrpMem.query();

// variable declarations
var addStakeArr = []; // add stakeholders 

// loop through found records
while(grStakeGrpMem.next())  
{
	if(grStakeGrpMem.getDisplayValue('user') != '') // // push record(s) to array if display value is not null
	{
		addStakeArr.push(grStakeGrpMem.getValue('user'));
	}
}

// retrive array values and populate registry with new records
for(var i = 0; i < addStakeArr.length; i++) 
{
	var grStakeHolReg = new GlideRecord('dmn_stakeholder_register'); // declare gliderecord object and insert new records
	grStakeHolReg.initialize();
	grStakeHolReg.setValue('user', addStakeArr[i]);
	grStakeHolReg.insert();
}

// find and deactivate stakeholders with inactive user records
grStakeHolReg.initialize();
grStakeHolReg.addEncodedQuery('sys_class_name=dmn_stakeholder_register^user.active=false');
grStakeHolReg.query();
while(grStakeHolReg.next())
{
	grStakeHolReg.setValue('active', false); // ISSUE IS HERE
	grStakeHolReg.update(); // ISSUE IS HERE
}

 

 

1 ACCEPTED SOLUTION

Ivan Betev
Mega Sage
Mega Sage

Hi @MBarrott ,

 

You are initializing variable grStakeHolReg that is being declared inside the loop. Declare it outside and see what changes.

 

var grStakeHolReg = new GlideRecord('dmn_stakeholder_register'); // declare gliderecord object and insert new records
// retrive array values and populate registry with new records
for(var i = 0; i < addStakeArr.length; i++) 
{	
	grStakeHolReg.initialize();
	grStakeHolReg.setValue('user', addStakeArr[i]);
	grStakeHolReg.insert();
}

Regards, Ivan

View solution in original post

6 REPLIES 6

Hi @Ivan Betev

 

The update multiple is a good suggestion, I'll definitely try that. With regards to the second attempt, I basically meant running the script a second time. If I setup my data accordingly it would work fine when ran (inserts new records, adjusts active fields), but if I ran it again and therefore no changes were necessary it would cause a failure due to a duplicate primary key. 

 

Looks to be an issue with the updates to the GlideRecord. If I used a separate GlideRecord for the insert and then another for the update it worked fine every time. 

 

I did read that you couldn't re-use a GlideRecord after an insert as it would close the GlideRecord but I'm honestly not 100% sure if that's true. Just seems weird that I can execute the script fine initially, run it again and see a failure. 

Hi @Ivan Betev

 

I will definitely incorporate updateMultiple that is a good idea. 

 

With regards to the second attempt, this is purely whenever I test run the script a second time. If I prep the table and records accordingly at first it will work (carries out inserts, active field changes etc) but running the script a second time when there's no data to update causes the unique key error. 

 

Running it with separate GlideRecords for insert and updates works which is confusing.