Creating an Outbound REST Message in Service Now



This tutorial assumes you have created an application and the application is approved:

For more info on this see:

https://developers.hp.com/hp-proactive-management/guide.

https://developers.hp.com/hp-proactive-management/api/hp-techpulse-analytics-api

snow/Untitled.png



Creating the application

snow/Untitled%201.png

Navigate to the application registry and provide the Client ID and Secret from the HP Developer website. Enter the Authorization, Token and Redirect URL’s.

snow/Untitled%202.png



Create the OAuth Profile and OAuth Scope.

snow/Untitled%203.png

snow/Untitled%204.png

Navigate to the “Outbound HTTP Request” module and create a REST message with the requested HTTP Methods.

Verify authentication with “Get Oauth Token” link. Enter your TechPulse credentials when prompted.

snow/Untitled%205.png

In the method enter the content-type as application/json and click Test

snow/Untitled%206.png

snow/Untitled%207.png

This will return the JSON you need to parse into your import table.

snow/Untitled%208.png

The Import set table needs to be created with the requested fields to map. A transform map will place the data into the final Service Now table. https://docs.servicenow.com/bundle/newyork-platform-administration/page/administer/import-sets/reference/import-sets-landing-page.html

Script Include that will place data into an Import set table is provided. You can call this script include from a scheduled job.

//Follow Service Now's guides on Import set tables and Trandform maps.  This is a script include that can be used to place the JSON data into an import set table.  

GetAssetAPI.prototype = {
    initialize: function() {
    },
    getAssetList: function() {

        try {
            //get new access token
            //GlideOAuthClientRequest
            var clientRequest = new sn_auth.GlideOAuthClientRequest();
            clientRequest.setGrantType('refresh_token');
            clientRequest.setRefreshToken(token);
            //GlideOAuthClient
            var client = new sn_auth.GlideOAuthClient();
            //GlideOAuthClientResponse
            var tokenResponse = client.requestTokenByRequest('CMDB Test', clientRequest);
            //GlideOAuthToken
            var token = tokenResponse.getToken();

            if (token) {
                var count = 1000; //This is the number of assets you can get in a single query
                var startIndex = 1; //This is the page number to query
                var cont = true; //This is the boolean to say if you need to query more records
                //Keep querying while there are more assets to get
                while (cont) {
                    var response = getTPAssets(startIndex,count, token);
                    if(response.getStatusCode() === 200) {
                        //Get the body of the response where the assets reside
                        var respData = JSON.parse(response.getBody());
                        var resourceCount = respData.resources.length;
                        //Check to see if we got any results
                        if(resourceCount > 0) {
                            //Prepare to create a new u_asset_rest import set record
                            var rec = new GlideRecord('u_asset_rest');
                            //Loop through the resources to create a new u_asset_rest record for each
                            for (var i = 0; i < resourceCount; i++) {
                                rec.initialize();
                                rec.u_devicename = respData.resources[i].deviceName;
                                rec.u_devicetype = respData.resources[i].deviceType;
                                rec.u_devicemfg = respData.resources[i].deviceMfg;
                                rec.u_devicesn = respData.resources[i].deviceSn;
                                rec.u_deviceos = respData.resources[i].deviceOs;
                                rec.u_memory = respData.resources[i].memory;
                                rec.u_graphics = respData.resources[i].graphics;
                                rec.u_processor = respData.resources[i].processor;
                                rec.u_deviceosoriginal = respData.resources[i].deviceOsOriginal;
                                rec.u_warstatus = respData.resources[i].warStatus;
                                rec.u_locationcc = respData.resources[i].locationcc;
                                rec.u_companyname = respData.resources[i].companyName;
                                rec.u_osrelease = respData.resources[i].osRelease;
                                rec.u_osversion = respData.resources[i].osVersion;
                                rec.u_osstatus = respData.resources[i].osStatus;
                                rec.u_discovery_source = 'TechPulse';
                                rec.model_id = '';
                                rec.asset = '';
                                rec.insert();
                            }
                            //If the number of results returned was less than the max query, query the next page
                            resourceCount != count ? cont = false : startIndex++;
                        } else {
                            //If no results, stop querying
                            cont = false;
                        }
                    } else {
                        cont = false;
                    }
                }
            }
        } catch (e) {
            gs.error("Error occurred during script execution for Assets Reason: "+ e.getMessage());
        }
        /*
        * REST query the TechPulse Asset API
        */
        function getTPAssets(startIndex,count, token){
            var r = new sn_ws.RESTMessageV2('Asset Test', 'Asset HW INV POST');
            r.setQueryParameter('grant_type', 'refresh_token');
            r.setQueryParameter('refresh_token', token);
            r.setQueryParameter('startIndex', startIndex);
            r.setQueryParameter('count', count);
            var response = r.execute();
            return response;
        }
    }
};

This is an example of the data feed into the cmdb_ci_computer table.

snow/Untitled%209.png