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

-O-
Kilo Patron
Kilo Patron

Unless you have defined function string somewhere else, that is undefined. The correct function is String. Though it is not necessary as getValue returns a string anyway.

i replaced string with String but it doesnt work, i completely removed the string too from switch doesnt work.

Now even the STATUS field stopped working, something wrong with the syntax

Could you post the new code?

Do note that getValue will return null if the field contains no value which cannot be "turned" into a valid Number. I would us a ternary whenever using getValue:

var status = incGR.u_status.nil() ? 0 : Number(incGR.getValue('u_status'));
var bimpact = incGR.u_initial_risk.nil() ? '' : incGR.getValue('u_initial_risk');