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.
31 Comments
MrMuhammad
Giga Sage

@Mark C_ ,  According to the above article, no it won't check the RITMs with that attachment but if you want you can have checks to confirm before attaching the attachment.

Pallavi12
Tera Contributor

Hi @MrMuhammad ,

 

This is really helpful but I am trying same kind of requirement can you please help , instead of user table I need to upload the data in cmdb_ci_service table and following is the requirement.

 

     Application Services ending with ‘Prod’ must set value ‘Production’ in ‘Used for’ field
• New Business Services must create a related Service Offering with naming convention: *Business Service Name* - *current date*
• If any errors occur, they must be logged with the format: *Import set number* - *row* - *error*
• Any existing Service should be updated (coalesce on name), taking to account that blank fields must be ignored

 

Also, I have tried all the above given steps by you for cmdb table but its not working , it is not updating my cmdb table with the excel file data.

 

PFB excel file:

Pallavi12_0-1686472907756.png

 

Could you please help.

 

Adam43
Tera Contributor

This is good @MrMuhammad,

I'm trying to help our ATF team create their Test Management 2.0 records with a catalog item.  They pass around excel files to validate their steps before creating- and today they have to copy/paste the steps from the excel to the application in ServiceNow.  I'd like to give them a catalog item to upload these steps, but struggling with creating the test, then a second phase to add the 'test steps' and having to reference the 'test version' as the reference between the test and the steps.

Any ideas on how to adapt your approach?

Mussarat
Tera Contributor

its importing data but sending duplicate values

maciekBerb
Tera Contributor

What is a sys_id hardcoded in 3rd line of business rule:

var attached = attachment.copy('sc_req_item', current.getUniqueValue(), 'sys_data_source', 'b3e781382f5c9110344f2b5df699b6a9');

Shouldn't we exchange it as well?

divyal09
Tera Contributor

@MrMuhammad Thank You. This helped alot

tidert_24
Kilo Sage

Thank you so much for this @MrMuhammad . This is very helpful

Is there a way for user records to be imported like this if the file will be uploaded on an Attachment variable in the catalog item?

Md Saquib Ansar
Giga Guru

Hi

How can we control RITM stage i.e when execution started it should be fulfilment but as soon as import is done, it should close the RITM

rogerio_felix
Tera Contributor

Great article, I saved a long time of development!

Bharat Tagad
Tera Contributor

Hi @MrMuhammad , 

 

Thanks for the awesome article.

We have implemented same functionality for our client. We have created a separate record producer in which attaching excel as an attachment and when submit the request the attachment is copying to sys_data_source table and then scheduled import is executing and transform map is getting started.

 

But nowadays we are facing an issue:

When we are uploading attachment first time that getting copied in data source record and transform map starts. This is ok.

But when the first transform map is in progress and we are uploading next file, as expected the new excel file is getting copied into data source record and transform map is getting started. But the new transform map also importing the records from the first excel file only. It doesn't pick the newly loaded excel file.

And this is happening for the subsequent uploads.(we can see multiple attachments in data_source record but the only first file is used in every transform map)

In this process there is creation of duplicate records in target table. 

We don't know where we are doing wrong.

Please can anyone help on this..?

 

 

Thanks in advance.

Bharat.