Clone any record

CC0 Paris Musées / Musée Carnavalet

This frunction should help you to clone almost any record in ServiceNow. Pass a GlideRecord into it.

function copyRecord(gr) {
  var recordElement;
  var recordElementName;
  var recordTable = gr.getTableName();
  var recordFields = gr.getFields();
  var newRecord = new GlideRecord(recordTable);
  newRecord.initialize();
  for (var i = 0; i < recordFields.size(); i++) {
    recordElement = recordFields.get(i);
    if (
      recordElement.getName() != "sys_id" &&
      recordElement.getName() != "number"
    ) {
      recordElementName = recordElement.getName();
      newRecord.setValue(recordElementName, gr.getValue(recordElementName));
    }
  }
  var newSysid = newRecord.insert();
  return newRecord;
}
Andre Kosak
ServiceNow Developer, Freelancer

Develop whatever!

comments powered by Disqus

Related