Web Services Archives - ServiceNow Guru https://servicenowguru.com/category/web_services/ ServiceNow Consulting Scripting Administration Development Tue, 01 Oct 2024 15:19:54 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.2 https://servicenowguru.com/wp-content/uploads/2024/05/cropped-SNGuru-Icon-32x32.png Web Services Archives - ServiceNow Guru https://servicenowguru.com/category/web_services/ 32 32 Using Import Sets for REST Integration in ServiceNow https://servicenowguru.com/integration/using-import-sets-rest-integration/ Mon, 26 Aug 2024 17:18:21 +0000 https://servicenowguru.com/?p=16807 Integrating data from external sources into ServiceNow is a common requirement for organizations aiming to maintain a unified system of record. One effective method for achieving this is through the use of Import Sets, which can be enhanced using REST APIs for seamless data transfer. In this blog post, we will explore how to use

The post Using Import Sets for REST Integration in ServiceNow appeared first on ServiceNow Guru.

]]>

Integrating data from external sources into ServiceNow is a common requirement for organizations aiming to maintain a unified system of record. One effective method for achieving this is through the use of Import Sets, which can be enhanced using REST APIs for seamless data transfer. In this blog post, we will explore how to use Import Sets for REST integration in ServiceNow, focusing on key components such as Transform Maps, Transform Scripts, and the Import Set API.

What are Import Sets?

Import Sets in ServiceNow are used to import data from various data sources and map that data into ServiceNow tables. They act as staging tables where raw data is initially stored before being processed and transformed into the target tables in ServiceNow.

Transform Maps

Definition

A Transform Map is a set of field mappings that determines the relationships between fields in an import set and fields in the target table. Transform Maps ensure that the data imported from the staging table is correctly mapped and transferred to the desired table in ServiceNow.

Creating Transform Maps

  1. Navigate to: System Import Sets > Administration > Transform Maps.
  2. Click on New to create a new transform map.
  3. Provide a name for the Transform Map and select the source Import Set Table and the Target Table.
  4. Define field mappings by adding field map records, specifying the source field from the Import Set Table and the target field in the ServiceNow table.

Transform Maps Validation

  • Ensure the Transform Map is reusable by defining source and target tables that can be applied to multiple data imports.
  • Use the Auto Map Matching Fields feature to automatically map fields with the same name.
  • Validate the field mappings by testing with sample data to ensure accuracy.

Transform Scripts

Definition

Transform Scripts are server-side JavaScript that can be used to manipulate data during the transformation process. These scripts provide additional flexibility to handle complex data transformations and custom logic.

Types of Transform Scripts

  1. onBefore: Executed before any record is processed.
  2. onStart: Executed at the start of the transformation.
  3. onAfter: Executed after each record is processed.
  4. onComplete: Executed after all records have been processed.

Example Usage

(function transformRow(source, target, map, log, isUpdate) {
// Example: Set a default value if a field is empty
if (!source.field_name) {
target.field_name = ‘Default Value’;
}
})(source, target, map, log, isUpdate);
Import Set API

The Import Set API allows for the import of data into ServiceNow from external sources using RESTful web services. This API provides endpoints to create, update, and delete records in Import Set Tables.

Key Endpoints

  1. POST /api/now/import/{table_name}: Import data into a specified Import Set Table.
  2. GET /api/now/import/{table_name}: Retrieve data from a specified Import Set Table.
  3. PUT /api/now/import/{table_name}/{sys_id}: Update a record in a specified Import Set Table.
  4. DELETE /api/now/import/{table_name}/{sys_id}: Delete a record from a specified Import Set Table.

Example Usage

To import data into an Import Set Table using REST, you can use the following example:

Request:

POST /api/now/import/u_my_import_table
Content-Type: application/json
Authorization: Bearer {your_token}{
“field1”: “value1”,
“field2”: “value2”,

}

Response:

{
“status”: “success”,
“sys_id”: “1234567890abcdef”
}

Sample REST API Call using PostMan

 

Integrating It All Together

  1. Setup Import Set Table: Create or identify the Import Set Table where data will be initially stored.
  2. Define Transform Map: Create and configure a Transform Map to map fields from the Import Set Table to the target table.
  3. Write Transform Scripts: Add any necessary Transform Scripts to handle custom logic and data manipulation.
  4. Use Import Set API: Utilize the Import Set API to import data from external sources into the Import Set Table.
  5. Run Transform: Execute the Transform Map to move data from the Import Set Table to the target table, applying any Transform Scripts in the process.

Conclusion

Using Import Sets for REST integration in ServiceNow provides a robust solution for importing and transforming data from various external sources. By leveraging Transform Maps and Transform Scripts, you can ensure that data is accurately and efficiently mapped to your ServiceNow tables. The Import Set API further enhances this process by allowing seamless integration through RESTful web services. By following these best practices and utilizing the tools provided by ServiceNow, you can achieve a streamlined and effective data import process.

The post Using Import Sets for REST Integration in ServiceNow appeared first on ServiceNow Guru.

]]>
Scripted Web Services https://servicenowguru.com/integration/scripted-web-services/ https://servicenowguru.com/integration/scripted-web-services/#comments Fri, 20 Aug 2010 20:50:23 +0000 https://servicenowguru.wpengine.com/?p=1930 One of ServiceNow’s principal strengths comes from its extensible framework. Built into the framework is the ability to retrieve information using one of a myriad of methods. If you want to get data out of any table, you can get it via direct web services, using basic auth data retrieval, having it pushed to a

The post Scripted Web Services appeared first on ServiceNow Guru.

]]>
One of ServiceNow’s principal strengths comes from its extensible framework. Built into the framework is the ability to retrieve information using one of a myriad of methods. If you want to get data out of any table, you can get it via direct web services, using basic auth data retrieval, having it pushed to a client, FTP server or  linux server by using the scheduled data extract, and more.  However, there are times when none of these solutions give you a logical way to achieve what you want in a simple manner.  I will give two examples: 1) adding an item to a cart, 2) fetching an attachment from a record.  For tasks such as these, the flexible Scripted Web Services is the answer.

Scripted web services are used in many integrations where a product wants to accomplish something that doesn’t have a simple solution already built-in.

How to use Scripted Web Services

This functionality requires the Web Service Provider – Scripted plugin.

There are four parts to scripted web services: WSDL (generated for you), script, input variables, output variables.  The WSDL is created for you and is what you’ll use on the client side to access your new web service.  The script is simple JavaScript and performs the required action.  Within the script you retrieve the input variables that you expect the client to send your web services.  Those variables are references using request. notation.  Once the script nears an end, you can set the output variables – the data you want to send back to the client – by using  response. notation.

This is the most flexible way to receive SOAP data and to return whatever you wish to return.  Let me explain by solving to two problems mentioned above.

Ordering a Blackberry

var cart = new Cart();
var item = cart.addItem('e2132865c0a801650010e411699');
cart.setVariable(item, 'original', request.phone_number);

// set the requested for
var gr = new GlideRecord("sys_user");
gr.addQuery("user_name", request.requested_for);
gr.query();
if (gr.next()) {
  var cartGR = cart.getCart();
  cartGR.requested_for = gr.sys_id;
  cartGR.update();
}

var rc = cart.placeOrder();
response.request_number = rc.number;

The script is a simple example of how to order a backberrry for a specific user.  Phone_number and requested_for are sent via the soap client to the script and the scripted web service returns the request_number back to the client.  Something that would be very difficult to figure out using traditional web service methodologies is quite simple to do with using this technique.

Fetching an attachment from a task record

if (typeof GlideStringUtil != 'undefined')
   var StringUtil = GlideStringUtil;
else
   var StringUtil = Packages.com.glide.util.StringUtil;

var  gr = new GlideRecord("sys_attachment");
gr.addQuery("table_sys_id", request.sys_id);
gr.query();
if (gr.next()){
   if (typeof GlideSysAttachment != 'undefined')
      var sa = new GlideSysAttachment();
   else
      var sa = new  Packages.com.glide.ui.SysAttachment();
   var binData =  sa.getBytes(gr);
   var encData =  StringUtil.base64Encode(binData);
   response.file_name = gr.file_name;
   response.table_name = gr.table_name;
   response.encodedAttachment = encData;
}
else{
   gs.log("Record not found");
}

Again, this is pretty straightforward.  What would be the alternative if direct web services were used?  Well, there would need to be a call to the sys_attachment record to find out the file_name, table_name, and associated task record.  Then there would need to be several more calls to fetch every attachment chunch (attachments are stored as chunks of data internally) only to reassemble the data client-side….not very friendly.

I created an update set for the fetch attachment record that you may download and apply to your instance: Fetch Attachment scripted web service.  Remember to enable the Web Service Provider – Scripted plugin before applying the update set.

The post Scripted Web Services appeared first on ServiceNow Guru.

]]>
https://servicenowguru.com/integration/scripted-web-services/feed/ 6
Sending attachments to a 3rd-party service desk https://servicenowguru.com/integration/sending-attachments-3rdparty-service-desk/ https://servicenowguru.com/integration/sending-attachments-3rdparty-service-desk/#comments Wed, 14 Apr 2010 22:47:33 +0000 https://servicenowguru.wpengine.com/?p=1529 I often hear requests for this to be bidirectional. Sending attachments from Service-now to a third-party system isn’t something that we’ve actually implemented in the past. However, with the number of people asking for it lately, I decided to write up a solution. Fetch Attachment as Base64 Encoded Data The following code is assumed to

The post Sending attachments to a 3rd-party service desk appeared first on ServiceNow Guru.

]]>
I often hear requests for this to be bidirectional. Sending attachments from Service-now to a third-party system isn’t something that we’ve actually implemented in the past. However, with the number of people asking for it lately, I decided to write up a solution.

Fetch Attachment as Base64 Encoded Data

The following code is assumed to be within a business rule on the task table (or a table that inherits from task, such as incident, problem, change, etc).

var StringUtil = Packages.com.glide.util.StringUtil;

var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', current.sys_id);
gr.addQuery('table_name', current.getTableName());
gr.addQuery('file_name', &'truck7.jpg');
gr.query();

if (gr.next()){
var sa = new Packages.com.glide.ui.SysAttachment();
var binData = sa.getBytes(gr);
var encData = StringUtil.base64Encode(binData);
}
else{
gs.log('record not found');
}

The above code is where the magic happens. The attachment to the incident is retrieved from the Service-now database and is now in base64 encoded data, ready to be sent to the integrating party via web services.

Send Attachment Data via SOAP

The code for sending the encoded data to a 3rd-party system might look something like this:

var envelope = new SOAPEnvelope();
envelope.createNameSpace('xmlns:imp', 'http://www.abcCompany.com/soap');
envelope.setFunctionName('imp:insert');
envelope.addFunctionParameter('uid', current.correlation_id);
envelope.addFunctionParameter('content', encData);
envelope.addFunctionParameter('content_type', gr.content_type);
var request = new SOAPRequest('http://www.abcCompany.com/addAttachment', 'user','pass');
var res = request.post(envelope);

Fetch Attachment Data – Alternate Method

You very well may want to have the business rule run on the sys_attachment table instead of the TASK table. If this is the case, your business rule will look like the following:

var StringUtil = Packages.com.glide.util.StringUtil;

var sa = new Packages.com.glide.ui.SysAttachment();
var binData = sa.getBytes(current);
var encData = StringUtil.base64Encode(binData);

The post Sending attachments to a 3rd-party service desk appeared first on ServiceNow Guru.

]]>
https://servicenowguru.com/integration/sending-attachments-3rdparty-service-desk/feed/ 14