Help with Transform script returning Undefined

mdjoseph12
Giga Contributor

Im calculating whether the dates coming within the imports are today or later and based upon that value it'll  determine what to set the field map too, but I am continuing to receive undefined: 

 

answer = (function transformEntry(source) {
	
	var today = new GlideDate();
	var stage;
	var year = today.addYears(1);
	if (source.u_date_type =="Construction Start") {
		var date = source.u_actual_start_date;
		if (date >= today) {
			stage = "Under Construction";
		}
	} else if (source.u_date_type =="Reopen Date") {
		var dates = source.u_actual_start_date;
		if (dates >= today) {
			stage = "Re-opened";
		} else if (dates >= year) {
			stage = "Complete";
		}
	}
	return stage; // return the value to be put into the target field
	
})(source);
1 ACCEPTED SOLUTION

dvp
Mega Sage
Mega Sage

Try this 

answer = (function transformEntry(source) {
	
	var stage;
	var today = new GlideDateTime();
	
	var year = new GlideDateTime();
	year.addYears(1);
	
	var actual = new GlideDateTime(source.u_actual_start_date);
	
	var diff = gs.dateDiff(today, actual, true);

	if (source.u_date_type =="Construction Start") {
		
		if (diff >= 0) {
			stage = "Under Construction";
		}
	} else if (source.u_date_type =="Reopen Date") {
		
		//Update the below logic as the above one
		var dates = source.u_actual_start_date
		if (dates >= today) {
			stage = "Re-opened";
		} else if (dates >= year) {
			stage = "Complete";
		}
	}
	return stage; // return the value to be put into the target field
	
})(source);

View solution in original post

6 REPLIES 6

Tony Cattoi
Mega Guru

I'm guessing that the date comparison if statements are never being entered.  On your source table is u_actual_start_date a string?  If so you will need to convert it to a glidedate to do the comparison.

 

Maybe try something like this and see if that helps the comparison process.

var date = new GlideDate();

date.setValue(source.u_actual_start_date);

 

https://developer.servicenow.com/app.do#!/api_doc?v=newyork&id=r_ScopedGlideDateSetValue_String

 

Hey Tony - THank you for your recommendation unfortunately the value is still appearing as undefined 

dvp
Mega Sage
Mega Sage

Try this 

answer = (function transformEntry(source) {
	
	var stage;
	var today = new GlideDateTime();
	
	var year = new GlideDateTime();
	year.addYears(1);
	
	var actual = new GlideDateTime(source.u_actual_start_date);
	
	var diff = gs.dateDiff(today, actual, true);

	if (source.u_date_type =="Construction Start") {
		
		if (diff >= 0) {
			stage = "Under Construction";
		}
	} else if (source.u_date_type =="Reopen Date") {
		
		//Update the below logic as the above one
		var dates = source.u_actual_start_date
		if (dates >= today) {
			stage = "Re-opened";
		} else if (dates >= year) {
			stage = "Complete";
		}
	}
	return stage; // return the value to be put into the target field
	
})(source);

mdjoseph12
Giga Contributor

worked thanks!