Service Board > Max for Developers > Max Groovy APIs > Transaction and Operations Execution
Transaction and Operations Execution
For transaction and operations execution, you can use helper methods implemented by the com.servicemax.core.Max API. To use this class, you must import the com.servicemax.core package.
Methods
Max.executeAsAdmin(Callable<T> closure)
Executes the code block (or closure) passed as parameter in the context of a new transaction, executed under system user rights.
int initialRecordNumber = Max.executeAsAdmin( {
->
def records = Database.query("SELECT io_uuid, io_name FROM io_basic_object_for_test");
return records.size();
});
Max.executeAsAdminInCurrentTransaction(Callable<T> closure)
After a previous transaction is created, executes the code block (or closure) passed as parameter in the context of the current transaction, executed under system user rights.
Max.executeAsAdminInCurrentTransaction( {
->
def records = Database.query("SELECT io_uuid, io_name FROM io_basic_object_for_test");
});
Max.executeAsUser(UUID userUUID(Callable<T> closure)
Executes the block (closure) of code passed as parameter in the context of a new transaction, under user rights, identified by userUUID.
Max.executeAsUser( someUserUUIDForTest, {
->
def records = Database.query("SELECT io_uuid, io_name FROM io_basic_object_for_test");
Assert.assertEquals(_userForTest, Max.effectiveUserUUID());
return "this is a just a test"
});
Max.executeAsUserInCurrentTransaction(UUID userUUID(Callable<T> closure)
After a previous transaction is created, executes the code block in the context of the current transaction under user rights, identified by userUUID.
Max.executeAsUserInCurrentTransaction( someUserUUIDForTest, {
->
def records = Database.query("SELECT io_uuid, io_name FROM io_basic_object_for_test");
Assert.assertEquals(_userForTest, Max.effectiveUserUUID());
return null; //there is no need to return something
});
Max.executeInTransaction(Callable<T> closure)
Executes a code block to create a new transaction in the context of the previously created effective user (or the authenticated user if no effective user has been created).
Max.executeInTransaction( {
->
def records = Database.query("SELECT io_uuid, io_name FROM io_basic_object_for_test");
Assert.assertEquals(_userForTest, Max.effectiveUserUUID());
});
Max.currentTransaction() (DEPRECATED)
* 
This API is deprecated in Max 22.1 and later.
Returns the current existing transaction.
IImmutableEntityTransaction someTransaction = Max.executeInTransaction ( {
->
return Max.currentTransaction();
}
Max.currentTransaction() Migration Guide
In Max 22.1 and later, Max.currentTransaction() is deprecated. To migrate code that previously used this API, replace Max.currentTransaction().putContextParameter with Max.putContextParameter(), and replace Max.currentTransaction().getContextParameter with Max.getContextParameter(), for example:
Deprecated Code
Replacement Code
Max.currentTransaction().putContextParameter('one_context_parameter', true)
Max.putContextParameter('one_context_parameter', true)
Max.currentTransaction().getContextParameter('one_context_parameter')
Max.getContextParameter('one_context_parameter')
For assistance migrating any other calls to Max.currentTransaction(), contact ServiceMax Support.
Max.putContextParameter(String key, Object value) and Max.getContextParameter(String key)
Puts and gets context parameter values from the current transaction.
Max.executeInTransaction( {
Max.putContextParameter('parameter_name', 'parameter_value')
Assert.assertEquals('parameter_value', Max.getContextParameter("parameter_name"))
});
Max.effectiveUsername() and Max.effectiveUserUUID()
Returns the username or userUUID of the user who is running the code block.
* 
In general, if you do not run a code block with executeAsAdmin or executeAsUser, the data returned by these methods are the username and UUID of the authenticated user.
Executing Operations
You can execute operations in two ways. You can directly run an operation by using its full identifier or use the Max.executeOperation(operationFullIdentifier) method.
For example, to directly run an operation associated with the io namespace whose full identifier is class_for_max_test:
def paramValue = "value33"
java.util.Map <String,Object> params = ["param1" : paramValue];


Max.executeInTransaction( {
->
Assert.assertEquals(paramValue, Max.io_class_for_max_test(params));
});
Max.executeOperation(operationFullIdentifier)
def paramValue = "value33"
java.util.Map <String,Object> params = ["param1" : paramValue];

Max.executeInTransaction( {
->
Assert.assertEquals(paramValue, Max.executeOperation("io_class_for_max_test",params));
});
For more information:
SMQL
Was this helpful?