General Recommendations
-
Create constants with meaningful names.
-
To speed up the selection, use the selectAttributes() method of the SimpleRecord class.
The following example generates an array that contains the record IDs:
selectAttributesconst user = new SimpleRecord('user');
user.addQuery('email', 'like', '@bestcompany');
user.selectAttributes('sys_id'); // select sys_id only
user.query();
const userIDs = [];
while (user.next()) {
userIDs.push(user.sys_id);
} -
Use operators in/not in for building conditions:
is one of in queryconst tableIds = [
'156950363112415683',
'156950616617772294',
'155931135900000083',
current.getValue('child_table_id')
]
const column = new SimpleRecord('sys_db_column');
column.addQuery('table_id', 'in', tableIds);
column.query();instead of multiple addOrCondition():
addOrConditionconst column = new SimpleRecord('sys_db_column');
column.addQuery('table_id', current.getValue('child_table_id'));
column.addOrCondition('table_id', '156950363112415683');
column.addOrCondition('table_id', '156950616617772294');
column.addOrCondition('table_id', '155931135900000083');
column.query(); -
Add record count preview before the update/delete operation:
updateMultipleconst record = new SimpleRecord('sys_vcs_preview_log');
record.addQuery('retrieved_pack_id', current.sys_id);
record.addQuery('state', 'skipped');
record.addQuery('error_text', 'The system has a newer record');
record.query();
ss.addInfoMessage(`Correction for ${record.getRowCount()} objects`);
record.setMultipleValue('state', 'good');
//record.updateMultiple();deleteMultipleconst record = new SimpleRecord('imp_ldap_users');
record.addEncodedQuery('imp_company%3DSofaShop.ru^sys_created_atLIKE2019-');
record.query();
ss.addInfoMessage(record.getRowCount());
//current.deleteMultiple(); -
The script that creates the record and its related records (for example, a Task record and child records of the tasks) must check a root record creation before the related records are created:
const recordTask = new SimpleRecord('task');
//recordTask.subject = 'Main Task';
const insertedID = recordTask.insert(); // ID of inserted record OR 0
const activities = [
'Analysis',
'Coding',
'Review',
'Testing',
'Merging',
'Release'
];
if (insertedID != 0) { // if Task created than create related tasks
const subtasksID = [];
activities.forEach(activityName => {
const subTaskRecord = new SimpleRecord('task');
subTaskRecord.parent_id = insertedID;
subTaskRecord.subject = activityName;
subtasksID.push(subTaskRecord.insert());
});
} else {
ss.addErrorMessage(JSON.stringify(recordTask.getErrors()));
} -
Comment your code when you use the Choice attributes in your scripts:
Adding commentsif (current.stage == '4' || current.stage == '6') { // Canceled OR Completed
const start = new SimpleDateTime(current.start_time);
//... -
Use the getAttributes() method to check an object before inserting or updating:
const task = new SimpleRecord('task');
task.initialize();
ss.info(JSON.stringify(task.getAttributes(), null, 2));
/* Info: {
"sys_id": "",
"parent_id": "",
"assigned_user": "",
"number": "",
"short_description": "",
"active": 1,
...
*/This method also checks whether an object has a specified attribute or not before calling:
getAttributesif (task.getAttributes()['has_breached'] != undefined) {
task.has_breached = true; // SLA Breached
task.update();
} -
Use the source messages as an argument:
Source Message as an argumentss.addInfoMessage('The protocol was send on email');
// Source Message and Message was previously addedinstead of message preparing:
Message preparingconst sm = new SimpleMessage();
const localizedMessage = sm.getMessage('The protocol was send on email');
ss.addInfoMessage(localizedMessage);and instead of hardcode messaging:
Hardcode messagingif (ss.getUser().language_id.language === 'en') {
ss.addInfoMessage('The protocol was send on email');
} else {
ss.addInfoMessage('Протокол был отправлен на почту');
}