Trying to do the color code for a field value

Nowlearner
Kilo Guru

Can someone help me with switch statement here or find what i am doing wrong below: i think i am not using the correct format in my second switch statement

i can set the color for STATUS field but not for BIMPACT field, any help is appreciated.

(function runMailScript(current, email,  email_action,
          event) {

	//Set up constants for easier readability. First statuses, then colors
	var TO_BE_DETERMINED = 1,
		UNAVAILABLE = 2,
		AVAILABLE_WITH_ISSUES = 3,
		IMPACT_MITIGATED = 4,
		AVAILABLE = 5,
		INFORMATIONAL = 6;
	
	var HIGH = 'high',
		LOW = 'low',
		TBD = 'tbd';
	
	var GREEN = '#008000',
		YELLOW = '#e2cd2d', //not used currently
		RED = '#ff0000',
		BLUE = '#0000ff',
		ORANGE = '#ff6600', //orange was the original color, used for backup
		BLACK = '#000000';
	
    // Get source incident's status
	var incGR = new GlideRecord('incident');
	incGR.get(current.incident_alert.source_incident);
	var status = incGR.getValue('u_status');
	var bimpact = incGR.getValue('u_initial_risk');
	
	// Check what the status is and set the color accordingly
	var color = '';
	switch (Number(status)) {
		case TO_BE_DETERMINED:
			color = ORANGE;
			break;
		case UNAVAILABLE:
			color = RED;
			break;
		case AVAILABLE_WITH_ISSUES:
			color = ORANGE;
			break;
		case IMPACT_MITIGATED:
			color = GREEN;
			break;
		case INFORMATIONAL:
			color = BLUE;
			break;
		case AVAILABLE:
			color = GREEN;
			break;
		default:
			color = ORANGE; //this is a just in case. Code shouldn't make it this far
			break;
	}
	template.print(color); //print the color to the email
	
	switch (string(bimpact)){	
		case HIGH:
			color = RED;
			break;
		case LOW:
			color = YELLOW;
			break;
		case TBD:
			color = ORANGE;
			break;
		default:
			color = BLACK; //this is a just in case. Code shouldn't make it this far
			break;
	}
	template.print(color);
	

})(current, template, email, email_action, event);
7 REPLIES 7

Also I would consider Decision Tables for this. It would take the "if this than that" part (the switch statements) out of the code into configuration.

This is my new switch statement just removing the string. So the code is already working fine from so long for STATUS field, i am trying to replicate the same for BIMPACT field

 

(function runMailScript(current, email,  email_action,
          event) {

	//Set up constants for easier readability. First statuses, then colors
	var TO_BE_DETERMINED = 1,
		UNAVAILABLE = 2,
		AVAILABLE_WITH_ISSUES = 3,
		IMPACT_MITIGATED = 4,
		AVAILABLE = 5,
		INFORMATIONAL = 6;
	
	var HIGH = 'high',
		LOW = 'low',
		TBD = 'tbd';
	
	var GREEN = '#008000',
		YELLOW = '#e2cd2d', //not used currently
		RED = '#ff0000',
		BLUE = '#0000ff',
		ORANGE = '#ff6600', //orange was the original color, used for backup
		BLACK = '#000000';
	
    // Get source incident's status
	var incGR = new GlideRecord('incident');
	incGR.get(current.incident_alert.source_incident);
	var status = incGR.getValue('u_status');
	var bimpact = incGR.getValue('u_initial_risk');
	
	// Check what the status is and set the color accordingly
	var color = '';
	switch (Number(status)) {
		case TO_BE_DETERMINED:
			color = ORANGE;
			break;
		case UNAVAILABLE:
			color = RED;
			break;
		case AVAILABLE_WITH_ISSUES:
			color = ORANGE;
			break;
		case IMPACT_MITIGATED:
			color = GREEN;
			break;
		case INFORMATIONAL:
			color = BLUE;
			break;
		case AVAILABLE:
			color = GREEN;
			break;
		default:
			color = ORANGE; //this is a just in case. Code shouldn't make it this far
			break;
	}
	template.print(color); //print the color to the email
	
	switch (bimpact){	
		case HIGH:
			color = RED;
			break;
		case LOW:
			color = YELLOW;
			break;
		case TBD:
			color = ORANGE;
			break;
		default:
			color = BLACK; //this is a just in case. Code shouldn't make it this far
			break;
	}
	template.print(color);
	

})(current, template, email, email_action, event);

The code is OK, though I would change it a bit:

var status = 0;
var bimpact = '';

if (incGR.get(current.incident_alert.source_incident)) {
	status = incGR.u_status.nil() ? 0 : Number(incGR.getValue('u_status'));
	bimpact = incGR.u_initial_risk.nil() ? '' : incGR.getValue('u_initial_risk');
}

Other than that the code works - have just tested it in Scripts - Background using constant values for status and bimpact.