- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 06-20-2019 05:13 AM
Hi,
In this article, I will explain to you a few of the advanced features related to Scripting during importing of data in ServiceNow.
As you already know, during import there are basically 4 steps.
- Create Import Table
- Load Data
- Mapping
- Transform
This is pretty straight forward import. However, in real time, it is not always this simple. For such instances, in ServiceNow, you have the flexibility to manipulate the source data at various intervals.
1. If you wanted to perform any data manipulations on the source record before inserting the row in the target table, then you can do this using Script option in the transform map record.
- Go to your transform map
- You will see a field "Run Script"
- Check this and you will see a script box, where you can write your code.
This script runs on every row of the source record before importing into the target table.
For example, if you wanted to generate Employee number based on employee first name and last name and some random number, then you can use below script
(function transformRow(source, target, map, log, isUpdate) {
// Add your code here
var fname = source.u_first_name;
var lname = source.u_last_name;
var emp_num = fname.substring(0,2) + lname.substring(0,2) + Math.floor((Math.random() * 1000) + 1);
target.u_employee_number=emp_num;
})(source, target, map, log, action==="update");
2. If you wanted to perform any other tasks during import or at the start/finish of import, then you can create transform scripts.
Once the transform map is created, then in that record, you will see Transform Scripts Tab. Click on New and you will have options to create scripts on various events such as onStart, onComplete, onBefore, onAfter, etc
For example, if I want to send out a notification whenever import is happening, then I can write the onStart script, in which I can trigger an event which is listening by a notification. This way I can have the email received whenever import starts.
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
// Add your code here
gs.eventQueue('Import.started',source,'recipient_email@abc.com');
})(source, map, log, target);
3. There could be cases, where you need to fetch the reference value or some other value based on the source field. For this, there is a field level scripting also available.
- Go to your transform map
- under Field maps, click on the field for which you need to add some scripting
- Then select "use source script" and it will provide you an option to write a script.
Here is a sample script for reference.
answer = (function transformEntry(source) {
// user_id shoudl be first 3 chars of firstname + department id.
var fname=source.u_first_name;
var gr = new GlideRecord("cmn_department");
gr.addQuery("name",source.u_department);
gr.query();
if(gr.next()) {
var user_id = fname.substring(0,3)+gr.getValue("id");
return user_id; // return the value to be put into the target field
}
})(source);
Do let me know if you have any questions in the comments section.
Mark the article as helpful if this helps.
- 1,888 Views