1. Web Services Integration – ServiceNow
  2. What is integration?
  3. Type of Integration
  4. Basic Requirements for Integrate any system
  5. Integration Testing Tools
  6. Integration Module
  7. Inbound Integration
  8. SOAP VS REST API
  9. Table API Web Service – REST
  10. Direct web services – SOAP
  11. Import Set Web Service – SOAP
  12. Import Set API – REST
  13. Scripted Web Services – SOAP
  14. Scripted Web Services – REST
  15. SOAP Message – Outbound
  16. REST Message – Outbound
  17. ServiceNow to ServiceNow Incident Integration
  18. OAuth 2.0 Authentication
  19. ServiceNow – OAuth application connect
  20. Access Token VS Refresh Token

Scripted Web Services – REST

Create the same API & apply the same validation that is done in Scripted Web Services – SOAP

One Scripted REST can contain multiple Resources, and each resource can work individually based on the script written inside.

getGroupInfo – GET

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    // implement resource here
	var grp = request.queryParams.group_name.toString();
    var gr = new GlideRecord('sys_user_group');
	var json={};
    if (gr.get('name', grp)) {
        json.group_name = gr.name;
        json.group_manager = gr.manager.name;
        json.group_email = gr.email;
        json.Results = "Group " + grp + " is found in servicenow";
        json.Users = getUsers(gr.sys_id);
        json.Role = getRoles(gr.sys_id);
		response.setStatus(200);
    } else {
        json.Results = "Group " + grp + " not found in servicenow";
		response.setStatus(405);
    }
	response.setBody(json);
	
    function getUsers(grId) {
        var users = "";
        var grm = new GlideRecord('sys_user_grmember');
        grm.addQuery('group', grId);
        grm.query();
        while (grm.next()) {
            if (users) {
                users = users + ',' + grm.user.name;
            } else {
                users = grm.user.name;
            }
        }
        return users;
    }

    function getRoles(grId) {
        var roles = "";
        var grrl = new GlideRecord('sys_group_has_role');
        grrl.addQuery('group', grId);
        grrl.query();
        while (grrl.next()) {
            if (roles) {
                roles = roles + ',' + grrl.role.name;
            } else {
                roles = grrl.role.name;
            }
        }
        return roles;
    }

})(request, response);

IncidentRecord – POST

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    // implement resource here
	var data = request.body.data;
	var outputJSON={};
	var v1 = mandatoryValidation();
    if (v1 == true) {
		var grInc = new GlideRecord('incident');
		grInc.initialize();
		grInc.caller_id = data.Caller_ID;
		grInc.short_description = data.Short_Description;
		var grci1 = new GlideRecord('cmdb_ci');
		grci1.get('name',data.CI);
		grInc.cmdb_ci = grci1.sys_id;
		if(data.Impact){
			grInc.impact = data.Impact;
		}
		if(data.Urgency){
			grInc.urgency = data.Urgency;
		}
		grInc.insert();
		outputJSON.Result = "Incident Created";
		outputJSON.sys_id=grInc.sys_id;
		outputJSON.Number = grInc.number;
    } else {
        outputJSON.Result = v1;
		response.setStatus(405);
    }
	
	response.setBody(outputJSON);

    function mandatoryValidation() {
        if (data.Caller_ID) {
            if (data.CI) {
                var grci = new GlideRecord('cmdb_ci');
                grci.addQuery('name', data.CI);
                grci.addQuery('operational_status', 1);
                grci.query();
                if (grci.next()) {
                    if (data.Short_Description) {
						if(data.Impact && data.Impact == 1){
							return "Impact can't be 1";
						}else{
							if(data.Urgency && data.Urgency == 1){
								return "Urgency can't be 1";
							}else{
								return true;
							}
						}
                    } else {
                        return "Short Description can't be blank";
                    }
                } else {
                    return "CI may be not found or not in operatonal";
                }
            } else {
                return "CI Can't be blank";
            }
        } else {
            return "Caller Id Can't be blank";
        }
    }

})(request, response);

Total Views: 1393

One response

Leave a Reply

Your email address will not be published. Required fields are marked *