Bulk Record Creation: The Ultimate Shortcut You Wish You Knew Sooner
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
- Navigate to Load Data
- Create an Excel file with some dummy data as shown in the image below.
Upload this file in the load data application and hit submit
-
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
-
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.
- Click on the mapping assist link to map the fields.
- Navigate to System Import Sets → Scheduled Imports
- Click On New to Create New Scheduled Data Import
- Click on Data Source to Select Data Source you have just created.
- Save the record.
Setting Up Record Producer
- Navigate to "Record Producer"
- Click "New"
- Give a name to the record producer.
- Click on Table and select Incident.
- Populate the short description field [optional].
- Open the Accessibility tab.
- Select a catalog from the Catalogs field.
- Select a Category from the Category field
- Open the "Portal Settings" tab
- Check the Mandatory Attachment field
- Right-click "Record Producer → Save. Click "Save"
- Scroll to the script Section In Record Producer.
- 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!
Related Links
Subscribe to my newsletter
Read articles from Sandeep Rana directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Sandeep Rana
Sandeep Rana
I'm a dedicated ServiceNow Developer and Analyst with four years of experience. I previously worked at Deloitte and am currently with QBRAINX. My journey in technology started as a freelance web developer, where I developed a passion for creating user-friendly web solutions. In my current role, I specialize in various aspects of ServiceNow, including Portal design, Flow, Integration, Common Configuration, and HRSD modules. What truly excites me is experimenting with the amalgamation of web development and ServiceNow capabilities. My work allows me to blend creativity with technical prowess, ensuring the solutions I create are both functional and intuitive. I bridge the gap between complex technical concepts and user-friendly designs, striving for excellence in every project. Beyond my professional endeavors, I'm a lifelong learner, constantly exploring new technological horizons. My enthusiasm for innovation fuels my commitment to delivering high-quality results. If you share a passion for technology and innovation, I'd love to collaborate and create something extraordinary together. Let's connect!