You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

To speed up the sampling generation, it is recommended to use the selectAttributes() method of the SimpleRecord class.

The example below generates an array containing IDs of the records:

selectAttributes
const user = new SimpleRecord('user');
user.addQuery('email', 'like', 'best.company');
user.selectAttributes('sys_id'); // select sys_id only
user.query();
const userIDs = [];
while (user.next()) {
  userIDs.push(user.sys_id);
}


Add a records count preview before updating/deleting:

updateMultiple
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 for ${record.getRowCount()} objects`);
record.setMultipleValue('state', 'good');
//record.updateMultiple();


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


Script that creates record and it's related records (e.g. creation of the Task record and child tasks records), should contain checking of root record creation before related records creation:

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:

if (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:

getAttributes
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:

getAttributes
if (task.getAttributes()['has_breached'] != undefined) {
  task.has_breached = true; // SLA Breached
  task.update();
}


  • No labels