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
Giga 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!

2 ACCEPTED SOLUTIONS

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'
};

View solution in original post

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

View solution in original post

6 REPLIES 6

What value does this add?

It looks like a bot response, @Kieran Anson your answer really helped! Thank you!