Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Incrementing System Properties integer value by multiple users causing duplicate values

WatsonK
Mega Contributor

Hello everyone!

 

I have a flow in flow designer that increments a value in system properties. This value should be readily available to users and every user should get a unique incremented value in a sequence. However, I am running into a problem where multiple users are accessing the same variable from system properties and are receiving duplicate values

 

This variable should increment every time a unique user accesses it, however when multiple users access it at the exact same time through a flow in flow designer, they receive the same value.

Does anyone have any ideas or experience regarding this issue, it would be greatly appreciated!

 

Thank you!

6 REPLIES 6

Kieran Anson
Kilo Patron

Hi,

You'll need to create a custom flow action for this as you'll need to create a mutex. A mutex will ensure exclusive access to the resource and ensure each time it's accessed, a unique value is provided.

 

For example:

var generateCode = Class.create();
generateCode.prototype = {
    initialize: function() {
    },

	getCodeAndIncrement: function(){
		return new global.Mutex.enterCriticalSection('UNIQUE_NAME_FOR_MY_MUTEX' , this, getCode);
	},

	getCode: function(){
		//We use GlideRecord in order to leverage the mutex lock on the record
		var lastValue = new GlideRecord('sys_properties');
		lastValue.addQuery('name' , 'my.property.name');
		lastValue.setLimit(1);
		lastValue.setNoCount(true);
		lastValue.query();
		if(lastValue.next()){
			var codeToUse = lastValue.getValue('value');
			var newVaue = codeToUse ++;
			lastValue.setValue('value' ,  );
			lastValue.update();
			return codeToUse;
		}
	},

    type: 'generateCode'
};

Good morning @Kieran Anson,

I am facing the same problem and your suggestion would work perfectly. However, I have to implement this in a custom application scope where global.Mutex is not reachable. 

Do you have any suggestions for implementing something similar in a scoped application (I am also limited in terms of custom table creation).

Thank you!
- Joa

Hi Kieran

 

Thank you for your response. I am currently facing a challenge with the integration of the global.Mutex.enterCriticalSection script include, as it resides within the global scope, while my script needs to be executed in a scoped application.
To address this, I'm considering creating a new script include in the global scope , i.e customScriptInclude, that will be accessible across all application scopes. This script include would call the necessary methods from the global.Mutex script include. I plan to invoke this customScriptInclude from my scoped application to effectively access the functionality of global.Mutex.
I would appreciate your thoughts on this approach.

 

Regards

Watson

Ankur Bawiskar
Tera Patron
Tera Patron

@WatsonK 

script is shared by @Kieran Anson 

try that and share feedback

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader