Change Date format from Variable Using Business Rule

Chris_Wilkinson
Tera Expert

I am trying to auto populate the short description field on the requeted item table using information from variables submitted via the service portal. This is being done using the business rule below

"(function executeRule(current, previous /*null when async*/) {
//On before insert business rule
//Condition where item contains Onboarding
//Updates the short description with concatenated variables
    
    current.short_description = current.variables.var_company.getDisplayValue() + ' ' + 'Onboarding' + ' / ' + current.variables.var_full_name + ' / ' + current.variables.var_start_date +' / ' + current.cat_item.getDisplayValue();
    
})(current, previous);

"

The rule works well except tha the date time variable (var_start_date) is being displayed in the wrong format. Our business have asked if the format can be changed to be  "DD-MM-YYYY" rather than the default "MM-DD-YYYY"

Is it possible to this within the business rule?

 

Thanks in advance.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Chris W 

yes you can convert that

update as this and let us know.

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

	var startDate = current.variables.var_start_date;

	var gdt = new GlideDateTime();
	var format = 'MM-dd-YYYY HH:mm:ss';
	gdt.setDisplayValue(startDate + ' 23:59:59');

	var dt = new GlideDate();
	dt.setValue(gdt.getDate());
	var myFormattedStartValue = dt.getByFormat('dd-MM-YYY');

	current.short_description = current.variables.var_company.getDisplayValue() + ' ' + 'Onboarding' + ' / ' + current.variables.var_full_name + ' / ' + myFormattedStartValue  +' / ' + current.cat_item.getDisplayValue();

})(current, previous);

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

5 REPLIES 5

Ankur,

Thanks for the script.   I modified what you provided and it's working perfect.  I needed a date format of 'MM-dd-YYYY'.  Below is my modified script:

 

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

// Add your code here
// added by MCD 4.13.23
var tedDate = current.variables.transfer_effective_date;

var gdt = new GlideDateTime();
var format = 'YYYY-MM-dd';
gdt.setDisplayValue(tedDate);

var dt = new GlideDate();
dt.setValue(gdt.getDate());
var myFormattedValue = dt.getByFormat('MM-dd-YYYY');

current.short_description = "Transfer - " + current.variables.full_name + ' - ' + '\n' + myFormattedValue;

 

})(current, previous);

 

Thanks.

Mike