sys.context
This package contains methods to get information about execution context of the script, like the current user or the stack of calls that have been made.
getCurrentUserRecord()
Returns a sys.data.Record
object that contains the extended user fields for the user in the current
context of execution. If there is no user set in the current context of execution, this method will return null
.
Returns
sys.data.Record
- The user in the current context of execution or null
if it is not set.
Exceptions
badRequest
If extended user fields are not configured.
Samples
// prints the extended user information for the current user
var record = sys.context.getCurrentUserRecord();
log('current user extra info: ' + JSON.stringify(sys.utils.text.recordToString(record)));
getCurrentIp()
Retrieves the IP of the current request. If the script is running on a context without a request associated, then
it will return null
.
Returns
string
- Returns the IP of the current request.
Samples
// logs the current request IP
var ip = sys.context.getCurrentIp();
log('IP: '+ip);
setCurrentUserRecord(user)
Sets the current user in the context of execution using a sys.data.Record
object.
Keep in mind that if you don’t restore the current user in the context of execution it will be restored automatically after the script snippet is executed.
Parameters
Name | Type | Required | Default | Description |
---|---|---|---|---|
user |
yes |
The user to set as the current one in the context of execution. |
Samples
// sets the current user and then logs the email of the current user
var test1 = sys.data.findOne('sys.users', {email: 'test1.docs@slingr.io'});
sys.context.setCurrentUserRecord(test1);
var currentUser = sys.context.getCurrentUserRecord();
log('current user: '+currentUser.field('email').val());
setCurrentUserRecordById(userId)
Sets the current user in the context of execution using an user ID.
Keep in mind that if you don’t restore the current user in the context of execution it will be restored automatically after the script snippet is executed.
Parameters
Name | Type | Required | Default | Description |
---|---|---|---|---|
userId |
|
yes |
The ID of the user to set as the current one in the context of execution. |
Samples
// sets the current user and then logs the email of the current user
var test1 = sys.data.findOne('sys.users', {email: 'test1.docs@slingr.io'});
sys.context.setCurrentUserRecordById(test1.id());
var currentUser = sys.context.getCurrentUserRecord();
log('current user: '+currentUser.field('email').val());
setCurrentUserRecordByEmail(userEmail)
Sets the current user in the context of execution using a user email.
Keep in mind that if you don’t restore the current user in the context of execution it will be restored automatically after the script snippet is executed.
Parameters
Name | Type | Required | Default | Description |
---|---|---|---|---|
userEmail |
|
yes |
The email of the user to set as the current one in the context of execution. |
Samples
// sets the current user and then logs the email of the current user
sys.context.setCurrentUserRecordByEmail('test1.docs@slingr.io');
var currentUser = sys.context.getCurrentUserRecord();
log('current user: '+currentUser.field('email').val());
root()
Returns an object that provides utilities to check what’s the root context. To understand how context work, please read Context.
The root context is the one at the top of the stack of calls. For example, if you execute an action from the REST API, where the action has an script that creates a record, during the creation of the record the current context would be the creation of the record, but the root one is the execution of the action as that’s the operation that started the request.
This method is used like this:
if (sys.context.root().entity('companies').create().check()) {
// we are in the context of the creation of a record in the 'companies' entity
...
} else {
...
}
So basically you will always start with sys.context.root()
, then it will be followed by a chain
of methods that define the context, and at the end you have a call to check()
which is going to
evaluate if the context is the root one.
Here are the available methods to check contexts:
sys.context.root().entity('entityName').create().check()
sys.context.root().entity('entityName').update().check()
sys.context.root().entity('entityName').delete().check()
sys.context.root().entity('entityName').import().check()
sys.context.root().entity('entityName').action('actionName').check()
sys.context.root().listener('listenerName').check()
sys.context.root().users().create().check()
sys.context.root().users().update().check()
sys.context.root().users().delete().check()
sys.context.root().users().import().check()
Some context allow to add more information to be more precise on which context this is executing:
sys.context.root().entity('entityName').create().check()
sys.context.root().entity('entityName').update().with({recordId: id}).check()
sys.context.root().entity('entityName').delete().with({recordId: id}).check()
sys.context.root().entity('entityName').import().with({fileName: 'file.csv'}).check()
sys.context.root().entity('entityName').action('actionName').with({recordId: id}).check()
sys.context.root().users().create().with({email: 'email@test.com'}).check()
sys.context.root().users().update().with({email: 'email@test.com'}).check()
sys.context.root().users().update().with({id: '56cf0945ee42cf00064c105e'}).check()
sys.context.root().users().delete().with({email: 'email@test.com'}).check()
sys.context.root().users().delete().with({id: '56cf0945ee42cf00064c105e'}).check()
sys.context.root().users().import().with({fileName: 'file.csv'}).check()
Returns
object
- An object that can be used to define the context to check; see description.
Samples
// checks that the script isn't running in the context of an import
if (!sys.context.root().entity('companies').import().check()) {
// do something here
}
any()
Returns an object that provides utilities to check the context along the stack calls. To understand how context work, please read Context.
This is exactly the same as root()
but instead of just checking the root context, the method
check()
will evaluate to true
if any of the contexts in the call stack matches. For example, if you
execute an action from the REST API, where the action has an script that creates a record, during the creation
of the record the current context would be the creation of the record. If you check the context using any()
instead of root()
, checking the context for the action and for the creation of the record will return true
:
sys.context.any().entity('companies').action('createAnotherRecord').check(); // returns true
sys.context.any().entity('companies').create().check(); // returns true
Please refer to the documentation of root()
for more information about how to define a context
and check it.
Returns
object
- An object that can be used to define the context to check; see description.
Samples
// checks we are in the context of the creation of a record, no matter if it is the root or not
if (sys.context.().entity('companies').create().check()) {
// do something here
}