Bulk Record Creation: The Ultimate Shortcut You Wish You Knew Sooner

Bulk Record Creation: The Ultimate Shortcut You Wish You Knew Sooner

ServiceNow Series

Introduction

Are you tired of long, clunky code for bulk record creation? Well, good news—there’s a better way, and it’s so simple, it might make you laugh. In this blog, I’ll show you the best method to turn attachments into records in just a few lines of code. Say goodbye to the hassle and hello to lightning-fast efficiency. Ready to become a bulk record wizard? Let’s dive in!

Create the Masterpiece

Setting Up Data Source, Transform Map, and Scheduled Imports

  1. Navigate to Load Data

  1. Create an Excel file with some dummy data as shown in the image below.

  1. Upload this file in the load data application and hit submit

  2. Once data is loaded, you will see a data source being generated. To check, navigate to System Import Sets → Administration → Data Sources and sort by latest created date.

Note: The data source name will be the same as the uploaded file name

  1. You can go ahead and rename this data source as you choose and create a transform map for it.
    Note: The Transform map’s target table should be on the table you want your record to load. Example: incident.

  1. Click on the mapping assist link to map the fields.

  1. Navigate to System Import Sets → Scheduled Imports

  1. Click On New to Create New Scheduled Data Import

  1. Click on Data Source to Select Data Source you have just created.

  1. Save the record.

Setting Up Record Producer

  1. Navigate to "Record Producer"

  1. Click "New"

  1. Give a name to the record producer.

  1. Click on Table and select Incident.

  1. Populate the short description field [optional].

  1. Open the Accessibility tab.

  1. Select a catalog from the Catalogs field.

  1. Select a Category from the Category field

  1. Open the "Portal Settings" tab

  1. Check the Mandatory Attachment field

  1. Right-click "Record Producer → Save. Click "Save"

  1. Scroll to the script Section In Record Producer.

  1. Add the following Script.
var data_source = {
  table: "sys_data_source",
  sys_id: "cfe921dac39dd6106fc6d0dc0501318b",
  transformMapSysID: "883ae99ec3511a106fc6d0dc05013126",
};
var scheduled_import_set_sys_id = "45aa65dec3511a106fc6d0dc0501314b";

var sys_attachment = GlideSysAttachment();

// If exists, delete existing attachment from Data Source

var data_source_record = new GlideRecord(data_source.table);
data_source_record.get(data_source.sys_id);

sys_attachment.deleteAll(data_source_record); // delete all existing attachment from data source

// once attachments are deleted, copy the attachment from incident record to data source record.

sys_attachment.copy(
  "incident",
  current.sys_id.toString(),
  data_source.table,
  data_source.sys_id
);

// once attachment is copied, start the transformation process.
// Now its time to load the excel file into the import table

var gr_job = new GlideRecord("scheduled_import_set");
gr_job.addEncodedQuery("sys_id=" + scheduled_import_set_sys_id);
gr_job.query();

if (gr_job.next()) {
  // Uncomment it if you want to change the run as field in Scheduled Import

  /* 
    gr_job.run_as = gs.getUserID();
    gr_job.update();
  */

  SncTriggerSynchronizer.executeNow(gr_job);
}

/*
 * Uncomment the below code if you dont want to create a default record on the target table i.e incident in this example
*/

// To Avoid to create a default record use this
// current.setAbortAction(true);

Result

Now, if you Navigate to Portal and Search for this record producer, you will find it. You can attach the file and hit submit and incidents from the file will be generated on the incident table.

Ending Note

And there you have it—the best way to bulk**-**create records with minimal code and maximum flexibility! It's fast, efficient, and won’t leave you pulling your hair out. So next time you're drowning in data, remember this little trick and watch your workload disappear in just a few lines of code. Because why work harder when you can work smarter? You're officially a bulk record wizard now—congrats!

  1. Mastering Bulk Record Creation in ServiceNow: The Right Way

  2. How Not to Use Record Producers for Bulk Record Creation: A Cautionary Tale

Did you find this article valuable?

Support Sandeep Rana by becoming a sponsor. Any amount is appreciated!