- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-04-2021 03:48 AM
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.
Solved! Go to Solution.
- Labels:
-
Script Debugger
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-04-2021 04:03 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-04-2021 03:57 AM
Hi
in case your variable is really of type GlideDateTime you can get another format by something like this:
var date = '04 May 2020 12:58:00'; //replace with your variable
var simpleDateFormat = 'dd MMMM yyyy HH:mm:ss'; // Simple Date Time format
var gdt = new GlideDateTime();
gdt.setDisplayValue(date,simpleDateFormat); //Set time using current TZ
var newFormat = gs.addInfoMessage(gdt.getDisplayValue()); // get date in new format
Kind regards
Maik
If my answer replied your question please mark appropriate response as correct so that the question will appear as resolved for other users who may have a similar question in the future.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-04-2021 04:03 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-04-2021 06:55 AM
Hi Ankur,
Thats works a treat 🙂
Thanks for the assist.
Kind Regards
Chris
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-18-2022 04:22 AM
Need to know where i gone wrong
(function executeRule(current, previous /*null when async*/ ) {
var startDate = current.event_start_date;
var gdt = new GlideDateTime();
var format = 'YYYY-MM-dd HH:mm:ss';
gdt.setDisplayValue(startDate + '11:59:00');
var dt = new GlideDate();
dt.setValue(gdt.getDate());
var myFormattedStartValue = dt.getByFormat('MM/DD/YYYY');
current.short_description = current.variables.var_company.getDisplayValue() + ' ' + 'Onboarding' + ' / ' + current.variables.var_full_name + ' / ' + myFormattedStartValue + ' / ' + current.cat_item.getDisplayValue();
})(current, previous);