What is the globals object good for?

ChristianW
Valued Contributor

Hi all

What is the globals object good for we are seeing in all the business rules?

ChristianW_0-1634664282447.png

 

 

1 ACCEPTED SOLUTION

ChristianW
Valued Contributor

With the globals variable, you can store data or objects globally and transfer it from one process steps to another.

  • For the runtime of a consolidation
    With the globals you can populate a variable or create an object in the first entity you calculate and store it for all other entities of a consolidation so that you don't need to calculate it again.
  • For a cubeview/report
    With the globals you can populate a variable or create an object in a dynamic calc when calculating the first cell and store it for all other cells of the cubeview so that you don't need to calculate it again.

This is a sample how to count process steps

Dim aSimpleCounter As Integer
'lock the process to avoid overrides 
SyncLock Globals.LockObjectForInitialization
	'Initialize aSimpleCounter with the stored variable, or initialize it with 0
	simpleCounter = globals.GetInt32Value("SimpleCounter", 0)
	'Count it up by 1
	simpleCounter +=1
	'Save it to globals
	globals.SetInt32Value("SimpleCounter", simpleCounter)
End SyncLock

You can also store objects like list or dictionaries in globals

Dim nameToStore As String = "MyObjectName"
'Definition of a list
Dim anyObject As list(of string)
'lock the process to avoid multiple initialisations 
SyncLock Globals.LockObjectForInitialization
	'Check if the object is already stored in globals
	anyObject = globals.GetObject(nameToStore)
	'If not initialize it
	If anyObject Is Nothing Then
		'create and populate the list
		anyObject = New list(of string)
		anyObject.add("One")
		anyObject.add("Stream")
		anyObject.add("Software")
		globals.SetObject(nameToStore , anyObject)
	End If
End SyncLock	
'Continue working

Be careful, many object like list and dictionaries are not supporting mutithreaded (support working in parallel), so you can not use them to add members to the list during a report or a consolidation.

But there are multithreaded versions you can use for this purpose

'Additional Imports for concurent list and dictionary
Imports System.Collections.Concurrent
'create a concurrent dictionary
Dim concDict As New ConcurrentDictionary()

View solution in original post

3 REPLIES 3

ChristianW
Valued Contributor

With the globals variable, you can store data or objects globally and transfer it from one process steps to another.

  • For the runtime of a consolidation
    With the globals you can populate a variable or create an object in the first entity you calculate and store it for all other entities of a consolidation so that you don't need to calculate it again.
  • For a cubeview/report
    With the globals you can populate a variable or create an object in a dynamic calc when calculating the first cell and store it for all other cells of the cubeview so that you don't need to calculate it again.

This is a sample how to count process steps

Dim aSimpleCounter As Integer
'lock the process to avoid overrides 
SyncLock Globals.LockObjectForInitialization
	'Initialize aSimpleCounter with the stored variable, or initialize it with 0
	simpleCounter = globals.GetInt32Value("SimpleCounter", 0)
	'Count it up by 1
	simpleCounter +=1
	'Save it to globals
	globals.SetInt32Value("SimpleCounter", simpleCounter)
End SyncLock

You can also store objects like list or dictionaries in globals

Dim nameToStore As String = "MyObjectName"
'Definition of a list
Dim anyObject As list(of string)
'lock the process to avoid multiple initialisations 
SyncLock Globals.LockObjectForInitialization
	'Check if the object is already stored in globals
	anyObject = globals.GetObject(nameToStore)
	'If not initialize it
	If anyObject Is Nothing Then
		'create and populate the list
		anyObject = New list(of string)
		anyObject.add("One")
		anyObject.add("Stream")
		anyObject.add("Software")
		globals.SetObject(nameToStore , anyObject)
	End If
End SyncLock	
'Continue working

Be careful, many object like list and dictionaries are not supporting mutithreaded (support working in parallel), so you can not use them to add members to the list during a report or a consolidation.

But there are multithreaded versions you can use for this purpose

'Additional Imports for concurent list and dictionary
Imports System.Collections.Concurrent
'create a concurrent dictionary
Dim concDict As New ConcurrentDictionary()

Cosimo
Contributor II

When a user runs a business rule which generates a global object, is this same object accessible to other users/sessions or only applicable to user who generated the global? 

ChristianW
Valued Contributor

Only for this user and only for this activity. The next calculation or the next report creates its own global object. But it bridges different dataunits of a consolidation. Or different cells of a report. Very powerful.