- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2017 03:19 PM
For reporting purposes, I need to know what Month an inspection has been performed. This is a scoped application, so GlideDateTime doesn't work and neither does getMonth() as far as I can tell. I have been able to pull the month number, but the closest I have come is "undefined" when I am trying to get the Month name. I have a single client script onChange from the variable date field 'initial_inspection_date' and want to populate 'inspection_month' field with January or the correlating name.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var inspectionDate = g_form.getValue('initial_inspection_date');
var date = inspectionDate.split('-');
var monthNumber = date[1];
function getMonthNameFromMonthNumber(monthNumber)
{
var monthNames = ["January", "February", "March", "Arpil", "May", "June", "July", "August", "September", "October", "November", "December"];
return monthNames[monthNumber];
}
alert(getMonthNameFromMonthNumber(monthNumber)); //results in alert with "undefined"
//g_form.setValue('inspection_month',getMonthNameFromMonthNumber(monthNumber)); ---populates 'inspection_month' field with "undefined"
}
Any help is greatly appreciated!!!
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2017 03:26 PM
Have you tried converting your month number from a string to an integer?
var monthNumber = parseInt(date[1], 10);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2017 08:20 AM
Thanks again Chuck and good suggestion on the "catch"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-17-2017 09:30 PM
Hi Chuck,
I was able to achieve this in native UI. When I try to put in date from portal in the same record producer. The field is highlighted with a red border and after submit the date field takes today's value instead of the month.
Please find below screen shots for reference.
before submit.
after submit:
Please suggest what steps should i follow to get an appropriate result.
Regards,
Zabeeulla.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2017 05:56 AM
I would have to know more about what the input field is doing. How is "November 2017" getting in there? Are you typing that? If you don't ever a properly formatted date, it defaults to the current date.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2017 05:38 PM
Hi Chuck,
I am using below script to populate the month and year in date field.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if(newValue!=oldValue){
var inspectionDate = g_form.getValue('date_value');
var date = inspectionDate.split('-');
var mnth = date[1];
var monthNumber = parseInt(mnth);
//alert(monthNumber);
var dateFinal = getMonthFromMonthNumber(monthNumber-1);
alert(dateFinal);
if(dateFinal!=undefined){
var dateFinal1 = dateFinal.toString();
var dat = date[0];
var finl = dateFinal1+' '+dat;
//alert(finl);
g_form.setValue('inf_cmt_preference1',finl);
//alert(dateFinal);
//alert(dateFinal); //
}
//Type appropriate comment here, and begin script below
}
function getMonthFromMonthNumber(monthNumber)
{
var monthNames = ["January", "February", "March", "Arpil", "May", "June", "July", "August", "September", "October", "November", "December"];
//alert(monthNames[monthNumber]);
return monthNames[monthNumber];
}
}
I am able to set this date in native UI but unable to submit.
Regards,
Zabeeulla.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2017 07:22 PM
Hello Zabeeulla,
A few things to consider that may help you:
1. What type of field is 'inf_cmt_preference1' (it appears to be a date field).
The way your code is set up to set a value, In ServiceNow you could ONLY use a string field to set the value with your desired format. ServiceNow date or date/time fields are very specific on what input and format they accept/display. For further information, see my 2nd point.
2. If you are trying to set any values in a ServiceNow date or date/time field, you should familiarize yourself with the API. http://wiki.servicenow.com/index.php?title=GlideDateTime#gsc.tab=0
As a note, a good practice when setting a new value in a date field use (as an example):
var gdt = new GlideDateTime(date);
Furthermore, if you only use "getValue()" as you did, your date field will be returning a value in GMT (if that matters). To get the value of your current time zone, you must you getDisplayValue()
Again, see the API or Scoped API docs for information. While you can get the standard JS "day" library to work in SN, the API SN uses is different and they will not work together without converting using the above method.
3. If you must use a date field for reporting purposes, may I suggest you default to the first day of the month and set the date value per Service-now API requirements. Although, I'm not really sure how you would use "November 2017" in a field for reporting.
I hope that helps... As Chuck mentioned, if we can understand your intended use of the field as well, we can be of further assistance.
Happy coding