Opsgenie's out of the box integration with ServiceNow currently populates the short description, description, and assignment group of an Opsgenie-created, ServiceNow incident. A lot of folks ask how to customize the web service script to populate additional fields in the ServiceNow incident, so I thought I'd write it up and share it with you all on the community. I'll talk about how to map the “Entity” field from the Opsgenie alert to the “Category” field of the ServiceNow incident.
You'll need to use the Opsgenie ServiceNow integration with action mapping configured for Opsgenie alerts to create ServiceNow incidents for this advice to work for you.
Click on the “Script Includes” > “OpsGenie_Client” - this contains all functions available to use in other actions in the app.
The “getAlertFromOpsgenie” function expects the alert ID, but the incident only has the alias, so we can add another function to get the alert using the alias (you can add this at the bottom):
getAlertFromOpsGenieAlias: function(alias) {
try {
var rest = new sn_ws.RESTMessageV2('x_86994_opsgenie.OpsGenie Alert API Endpoint', 'get');
rest.setRequestHeader("Authorization", "GenieKey " + this.apiKey);
rest.setStringParameter("endPoint", this.endpoint);
rest.setStringParameter("subEndPoint", "/" + alias + "?identifierType=alias");
rest.setRequestHeader("Content-Type", "application/json");
gs.debug("Sending GetAlert request to " + rest.getEndpoint());
var response = rest.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var responseLogMessage = "Response status: " + httpStatus + ", body: " + responseBody;
if(!httpStatus.toString().startsWith("2")) {
gs.error(responseLogMessage);
return "";
} else {
gs.debug(responseLogMessage);
return responseBody;
}
} catch(ex) {
var message = ex.getMessage();
gs.error("Exception occurred: ", ex);
}
}
var alias = source.u_opsgenie_alert_alias;
var alert = client.getAlertFromOpsGenieAlias(alias);
var getAlertResponse = new global.JSON().decode(alert);
var opsgenieEntity = getAlertResponse.data.entity;
log.warn("[OpsGenie Web Svc Create] entity is" + opsgenieEntity);
target.category = opsgenieEntity;
Line 1 grabs the alias received from the Opsgenie alert
Line 2 makes the Get Alert API call to Opsgenie using that alias to get the full alert details, and assigns it to a variable named “alert”
Line 3 grabs the actual alert payload from the “alert” variable
Line 4 grabs the value of the entity field from the payload, and assigns to a variable named “opsgenieEntity”
Line 5 logs the value of the the entity field to the Warning logs in ServiceNow
Line 6 sets the value of the ServiceNow incident’s “Category” field, as the value of the opsgenieEntity variable
Note: Line 5 is optional - it just logs what the value of the entity field from Opsgenie. You can see these logs under “Warnings” in the System logs in ServiceNow. (Search for “Warnings” in the ServiceNow Menu search bar)
Samir
3 comments