Web Service Integration / Salesforce Integration

3062

What We learn from this post

  1. Will understand Web Service Integration. Like when and how we will use it.
  2. Will talk about Inbound and Outbound web service.
  3. Will talk about Inbound Import set and Scripted Rest APIs.
  4. Will talk about Outbound Rest Message.
  5. Will set up Inbound and Outbound in ServiceNow Instance to see live data exchange. We will use Salesforce tool to consume the data.

Web Service Integration

HTTP-based web services integration allow diverse applications to talk to each other. ServiceNow supports both inbound (provider) and outbound (consumer) web services.

ServiceNow supports multiple web service formats like csv, xml, excel, json, pdf, rest, rss, soap, odbc etc. 

Note: It is important to know that the tool which you are going to integrate with Servicenow supports web service or not, if yes then what format?

ServiceNow provides various Inbound and Outbound web services:

Inbound web services allow you to access and modify ServiceNow data using a client application.

 

  • Direct Web Services: query tables and records directly using SOAP, REST, or other web service formats.
  • Import Set: access the import set tables and import data through a web service interface.
  • Scripted Web Services: define custom web service endpoints using JavaScript.

Outbound web services allow you to send SOAP and REST messages to external web service providers.

  • Outbound REST
  • Outbound SOAP

Find below scripts which i have used in video.

Post Method code

// implement resource here
(function process( /RESTAPIRequest/ request, /RESTAPIResponse/ response) {
var params;
try {
    params = request.body.data;
    gs.info("My Incident API request body params = " + JSON.stringify(params));
} catch (e) {
    gs.error("Error occured " + e.getMessage());
    throw sn_ws_err.BadRequestError("malformed request");
}


var myIncidentHandle = new MyIncident();
var result = myIncidentHandle.createMyIncidentFromAPI(params, 'u_my_incident');

if (gs.nil(result)) {
    response.setStatus(500);
} else {
    response.setStatus(201);
}
return result;
})(request, response);

Script Include 1

var MyIncident = Class.create();
MyIncident.prototype = {
initialize: function() {},
createMyIncidentFromAPI: function(params, tableName) {

    var myIncGr = global.DaoUtils.prepareGlideRecord(params, tableName);
    var tid = myIncGr.insert();
    this.logger.info("DaoUtils : created My Incident tid = " + tid);

    return {
        "sys_id": tid,
        "number": myIncGr.getValue("number")
    };
},

getMyIncidentById: function(id) {

    var myInc = {};
    if (id) {
        var myIncGr = new GlideRecord("u_my_incident");
        if (myIncGr.get(id)) {
            myInc = {
                'number': myIncGr.number + '',
                'short_description': myIncGr.short_description + '',
                'state': myIncGr.state.getDisplayValue() + '',
                'company': myIncGr.company.getDisplayValue() + '',
                'assignment_group': myIncGr.assignment_group.getDisplayValue()
            };
        }
    }
    return myInc;
},



type: 'MyIncident'
};

Script Include 2

 

var DaoUtils = Class.create();
DaoUtils.prototype = {
initialize: function() {},
type: 'DaoUtils'
};
DaoUtils.prepareGlideRecord = function(params, tableName) {
var gr = new GlideRecordSecure(tableName);
gr.initialize();
for (var attr in gr) {
if (attr == "comments") {
gr.comments = params[attr];
} else if (params[attr]) {
gr.setValue(attr, params[attr]);
}
}
return gr;
};
DaoUtils.prepareUpdateGlideRecord = function(sysId, params, tableName) {
var gr = new GlideRecordSecure(tableName);
gr.get(sysId);
for (var attr in gr) {
if (attr == "comments") {
gr.comments = params[attr];
} else if (params[attr]) {
gr.setValue(attr, params[attr]);
}
}
return gr;
};

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here