MrMuhammad
Giga Sage

find_real_file.png

USE CASE

Many times there are situations where we need to import data from attachments into the ServiceNow table without relaxing the ACLs.

One of the common example is to import users manually in Servicenow because of any of the following reasons:

  • No AD or any other integration is in place to auto-import users.
  • Sometimes external users must be imported that can’t be synchronized via AD integration.
  • We want to allow users to import new users without giving them higher privileges and native platform access.
  • Many other reasons as well……..

WHAT WE ARE BUILDING?

Glimpse of output

find_real_file.png


LET's BUILD

Let’s get your hands dirty with guided steps.

Step 1: Create a Catalog item by navigating to Service Catalog > Catalog Definition > Maintain items.

Reference: Create a Catalog item | Servicenow Docs

For the sake of the demo, I've created a basic catalog item without any variables as I'm just going to import users from the file attached.

find_real_file.png


Step 2:
Next we'll create a Data source to parse the file attachment and import users. Navigate to System Import Sets > Administration > Data source.

The data source configuration will be as per the below screenshot.

find_real_file.png

Reference: Create a data source | Servicenow Docs

Step 3: Attach the sample User's excel file that contains the users. Click on Paper-click on the top grey header and upload the excel file. Next click on Related Link > Test Load 20 Records.

find_real_file.png

Note: This step is important to auto-create the Import set table and import sample data with columns from the attachment.

Step 4: On the Next screen click on Create Transform map.

find_real_file.png

Reference: Create a Transform map | Servicenow Docs

Step 5: Fill out the form as per the screenshot below & Click on Related Link > Auto Map matching fields

find_real_file.png

Note: Auto Map Matching Fields creates the Field mapping between the import set table and target user table. In case any field mapping is missing then please click on Mapping assist and manually map the field.

After the successful previous step, you should see Field maps created against all the columns coming from the excel sheet.

find_real_file.png

Note: I've only 4 columns populated with user information in my attachment so 4 field maps can be seen created. Your field maps could be different in number.

Step 6: Navigate to System import sets > Administration > Scheduled import sets.

find_real_file.png

SCRIPT

//delete attachments form data source after execution
var attach = new GlideSysAttachment();
attach.deleteAll(data_source);

Note: Active checkbox must be unchecked because we are going to write a script that will import attachment data upon request submission, not on a scheduled basis. Copy the sys_id of scheduled data import, we'll use it in the last step.

Step 7: The last step in our process is to create a Business rule that will copy attachments from the Requested item to the Data source so that the Data source can process it and create users.   

Business Rule Configurations as per screenshot below

find_real_file.png

Under Advance Tab in the Script editor paste the below code:

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	var attachment = new GlideSysAttachment();
	var attached = attachment.copy('sc_req_item', current.getUniqueValue(), 'sys_data_source', 'b3e781382f5c9110344f2b5df699b6a9');

	if(attached){
		gs.info('Attachment Copied from: ' + current.number);
		var grImp = new GlideRecord("scheduled_import_set");
		if(grImp.get('25b941b82f5c9110344f2b5df699b6d4')){ // Replace "25b941b82f5c9110344f2b5df699b6d4" with sys_id of Schdeuled import
			gs.executeNow(grImp);	
		}	
	}
	
})(current, previous);

Note:  Replace "25b941b82f5c9110344f2b5df699b6d4" with sys_id of Scheduled import copied in step 6.

Watch again the video output at the top to see what we implemented.

Voila, Automation accomplished!! ✌️
Comment below your questions and queries & I would highly appreciate it if you hit bookmark or mark it as helpful.
33 Comments
MrMuhammad
Giga Sage

That's great. 

MrMuhammad
Giga Sage

Thanks Yousaf!

Patrick Steele
Tera Contributor

Great article, this was very helpful to me and I was able to get it working.

I have one question though. Is there any way to link a specific execution of the script to the RITM? I would like to be able to track down individual submissions to the actual execution of the scheduled job in the event that there are any issues. Is there any way to do this?

MrMuhammad
Giga Sage

@Patrick Steele - Glad that it was helpful to you.

To your question, yes the execution is stored in the sys_import_Set_execution table. So you can query that and get the required information to store as work notes on the RITM. 

Below is the sample script that helps you fetch the latest execution. You can adjust the below code after small tweaks to the Business rule in step 7 after gs.exeucteNow()

var grSysImportSetExecution = new GlideRecord('sys_import_set_execution');
grSysImportSetExecution.addEncodedQuery("scheduled_import=25b941b82f5c9110344f2b5df699b6d4");
grSysImportSetExecution.orderByDesc('sys_created_on');
grSysImportSetExecution.setLimit(1);
grSysImportSetExecution.query();
if (grSysImportSetExecution.next()) {

    gs.info('number: ' + grSysImportSetExecution.getValue('number'));
    gs.info('execution_context: ' + grSysImportSetExecution.getValue('execution_context'));
    gs.info('import_set: ' + grSysImportSetExecution.getValue('import_set'));
    gs.info('scheduled_import: ' + grSysImportSetExecution.getValue('scheduled_import'));
    gs.info('sys_created_on: ' + grSysImportSetExecution.getValue('sys_created_on'));
  
}
Gunjan Kiratkar
Kilo Patron

Hi @MrMuhammad ,

 

It helps me a lot. Thanks for the article.

MrMuhammad
Giga Sage

Thanks Gunjan. Glad to be of help!

dathib
Tera Contributor

 

Hi @MrMuhammad, What is the sys_id b3e781382f5c9110344f2b5df699b6a9 of in the business rule?  

'sys_data_source', 'b3e781382f5c9110344f2b5df699b6a9'

 

MrMuhammad
Giga Sage

@dathib Its a sys_id of Data Source we created in step 2.

Zainal
Tera Contributor

can we load data with xls password protected?

Mark C_
Tera Expert

Thanks for sharing this, @MrMuhammad ! I am exploring an automation too similar to this one.

My question though is, since the execution of the import set is controlled by a BR after insert, could I confirm that it will check all RITMS of that item with attachment and process the transform?

 

Thanks

Mark