Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
  • To speed up the sampling generation, it is recommended to use the the selectAttributes() method of the SimpleRecord class.
    The example below generates an array containing IDs of the records:
Code Block
languagejs
themeEclipse
titleselectAttributes
linenumberstrue
const user = new SimpleRecord new SimpleRecord('user');
user.addQuery('email',  'like',  'best.company@bestcompany');
user.selectAttributes('sys_id');  // select sys_id only
user.query();
const userIDs = [];
while while (user.next()) {
  userIDs    userIDs.push(user.sys_id);
}


  • Before adding query for sys_id value by addQuery(), add checking of value passed to queryUse operators in/not in for building conditions:
Code Block
languagejs
themeEclipse
titleis one of in query
linenumberstrue
const tableIds = [
    '156950363112415683',
    '156950616617772294',
    '155931135900000083',
    current.getValue('child_table_id')
]
const columnif (!!current.resolved_problems) {
  const problem = new SimpleRecord('itsmsys_db_problemcolumn');
  problemcolumn.addQuery('systable_id', 'in', current.resolved_problems.split(','));
  problemtableIds);
column.query();
  while (problem.next()) {
  if (!!problem.solved_by_changes) {
    problem.solved_by_changes += `,${current.sys_id}`;
  } else {
    problem.solved_by_changes = current.sys_id;
    }
  problem.update();
  }
}
Otherwise if passed value is null, the query will have condition:

instead of multiple addOrCondition:

Code Block
languagejs
themeConfluence
const 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
Code Block
languagejs
themeEclipse
problem.addQuery('sys_id', 'in', ['']);

which will lead to skipping of that condition row.

    Add a
  • records count preview before updating/deleting:
Code Block
languagejs
themeEclipse
titleupdateMultiple
linenumberstrue
const 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(`corrections`Correction for ${record.getRowCount()} objects`);
record.setMultipleValue('state', 'good');
//record.updateMultiple();


Code Block
languagejs
themeEclipse
titledeleteMultiple
linenumberstrue
const record = new SimpleRecord('imp_ldap_users');
record.addEncodedQuery( `imp'imp_company%3DSofaShop.ru^sys_created_atLIKE2019-`');
record.query();
ss.addInfoMessage(record.getRowCount());
//current.deleteMultiple();


  • Script that creates record and it's its related records (e.g. creation of the Task record and child tasks records), . It should contain checking of root record creation before related records creation:
Code Block
languagejs
themeEclipse
linenumberstrue
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:
Code Block
languagejs
themeEclipse
linenumberstrue
if if (current.stage ==  '4'  || current.stage ==  '6') {  // Canceled OR Completed
  const    const start = new SimpleDateTime new SimpleDateTime(current.start_time);
    //...


  • Use the getAttributes() method to check an object before inserting or updating:
Code Block
languagejs
themeEclipse
titlegetAttributes
linenumberstrue
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,
  ...
*/


  • Also, this method allows checking whether an object has a specified attribute or not before calling:
Code Block
languagejs
themeEclipse
titlegetAttributes
linenumberstrue
if if (task.getAttributes()['has_breached'] != undefined) {
  task    task.has_breached = true true;  // SLA Breached
  task.update(    task.update();
}


Code Block
languagejs
themeEclipse
titleSource Message as an argument
linenumberstrue
ss.addInfoMessage('The protocol was send on email');
// Source Message and Message was previously added

instead of message preparing:

Code Block
themeConfluence
titleMessage preparing
const sm = new SimpleMessage();
const localizedMessage = sm.getMessage('The protocol was send on email');
ss.addInfoMessage(localizedMessage);

instead of hardcode messaging:

Code Block
languagejs
themeConfluence
titleHardcode messaging
if (ss.getUser().language_id.language === 'en') {
    ss.addInfoMessage('The protocol was send on email');
} else {
    ss.addInfoMessage('Протокол был отправлен на почту');
}