ServiceNow Business Rule Script

ServiceNow business rule script example.

Leave Feedback
(function executeRule(current, previous) { /*null when async*/

	//Map the SN priority with OpsRamp priority
	var snowPriority = current.priority;
	var opsRampPriority = snowPriority;
	if(snowPriority == '1') {//Critical in SN
		opsRampPriority = 'Urgent';//Critical in OpsRamp
	} else if(snowPriority == '2') {//High in SN
		opsRampPriority = 'High';//High in OpsRamp
	} else if(snowPriority == '3') {//Medium in SN
		opsRampPriority = 'Normal';//Normal in OpsRamp
	} else if(snowPriority == '4') {//Low in SN
		opsRampPriority = 'Low';//Low in OpsRamp
	} else if(snowPriority == '5') {//Low in SN
		opsRampPriority = 'Very Low';//Low in OpsRamp
	} 

	//gs.addErrorMessage('OpsRamp Priority: ' + opsRampPriority);
	//gs.addErrorMessage('SNOW Priority: ' + snowPriority);

	//Map the SN status with OpsRamp status
	var snowState = current.state;
	var opsRampStatus = snowState;
	if(snowState == '1') {//New in SN
		opsRampStatus = 'New';//New in OpsRamp
	} else if(snowState == '2') {//In Progress in SN
		opsRampStatus = 'Open';//Open in OpsRamp
	} else if(snowState == '3') {//On Hold in SN
		opsRampStatus = 'On Hold';//On Hold in OpsRamp
	} else if(snowState == '6') {//Resolved in SN
		opsRampStatus = 'Resolved';//Closed in OpsRamp
	}else if(snowState == '7') {//Closed in SN
		opsRampStatus = 'Closed';//Closed in OpsRamp
	}

	//gs.addErrorMessage('OpsRamp Status: ' + opsRampStatus);
	//gs.addErrorMessage('SNOW State: ' + snowState);

	//Update incident properties
	//gs.addErrorMessage('OpsRamp incident ID: ' + current.u_opsramp_incident_id);
	//gs.addErrorMessage('OpsRamp client ID: ' + current.company.u_opsramp_client_id);
	//gs.addErrorMessage('SNOW incident number: ' + current.number);

	//If it is Partner level integration, SNOW company is mapped to OpsRamp client
	//OpsRamp client id should be in available in SNOW company - Create custom field in Company 
	//and add the mapped OpsRamp client id here
	//var opsRampClientId = current.company.u_opsramp_client_id;

	//If it is client level integration, hard code the OpsRamp client id here
	var opsRampClientId = 'client_392';

	if(!JSUtil.nil(current.u_opsramp_incident_id)) {//Update incident
		var uRestMsg = new sn_ws.RESTMessageV2('See & Try OpsRamp Incident REST', 'Update Incident');

		uRestMsg.setStringParameter('tenantId',opsRampClientId);
		uRestMsg.setStringParameter('incidentId',current.u_opsramp_incident_id);
		uRestMsg.setStringParameter('priorityToken',opsRampPriority);
		uRestMsg.setStringParameter('statusToken',opsRampStatus);

		//If work notes added?
		//gs.addErrorMessage("Work notes changed? : " + current.work_notes.changes());
		if(current.work_notes.getJournalEntry(1) && current.work_notes.changes()){	
			var content = current.work_notes.getJournalEntry(1);

			content = content.replace(/(?:rn|r|n)/g, 'n');
			content = content.replace(/(?:t)/g, 't');
			//gs.addErrorMessage("Work notes: " + content);
			uRestMsg.setStringParameter('responseToken',content);
		}

		//Execute script
		var uRestMsgResponse = uRestMsg.executeAsync();
		//var opsRampUpdateIncJSON = new JSON().decode(uRestMsgResponse.getBody());
		//gs.addErrorMessage("Status Code: " + uRestMsgResponse.getStatusCode());
		//gs.addErrorMessage("Error Msg: " + uRestMsgResponse.getErrorMessage());	

	} else if(snowState == '1') { //New incident
		//gs.addErrorMessage('New incident creation...');
		var cRestMsg = new sn_ws.RESTMessageV2('See & Try OpsRamp Incident REST', 'Create Incident');

		cRestMsg.setStringParameter('tenantId',opsRampClientId);

		//Set SN incident short description as OpsRamp subject
		subject = current.short_description;
		subject = subject.replace(/(?:rn|r|n)/g, 'n');
		subject = subject.replace(/(?:t)/g, 't');
		cRestMsg.setStringParameter('subjectToken',subject);

		//Set SN incident description as OpsRamp description
		description = current.description;
		description = description.replace(/(?:rn|r|n)/g, 'n');
		description = description.replace(/(?:t)/g, 't');
		cRestMsg.setStringParameter('descriptionToken',description);

		cRestMsg.setStringParameter('extTicketIdToken',current.sys_id);
		cRestMsg.setStringParameter('priorityToken',opsRampPriority);

		var cRestMsgResponse = cRestMsg.executeAsync();

		//Read OpsRamp incidentId from response and update to SN incident.
		//gs.addErrorMessage('responseBody: ' + cRestMsgResponse.getBody());
		//gs.log('OpsRamp create incident response body: ' + cRestMsgResponse.getBody());
		var opsRampCreateIncJSON = new JSON().decode(cRestMsgResponse.getBody());
		var inc = new GlideRecord('incident');
		inc.get(current.sys_id);
		inc.u_opsramp_incident_id=opsRampCreateIncJSON.id;
		inc.updateWithReferences();
	}
})(current, previous);