script for date and date/time fields
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2016 08:23 AM
Hello,
I'm having an issue creating a script to set a date field based on a date/time field. This is the part script that I'm trying to use:
function createChange(){
var change = new GlideRecord('change_request');
change.initialize();
change.setValue("requested_by_date", current.u_need_by_date);
The Requested By Date is a date / time field and the Need By Date is a date field. When I use this, the Requested By Date is not set correctly; it sets the date about a day early. I assume the issue is because the field types are different and that someone else has already figured this out.
Any help would be appreciated. Thanks in advance!
Albert
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2016 06:41 AM
Hi Venkat,
This is what I have so far:
function createChange(){
var glideDateTime = new GlideDateTime(current.u_need_by_date);
var change = new GlideRecord('change_request');
change.initialize();
change.setValue("short_description", current.short_description);
change.setValue("parent", current.sys_id);
change.setValue("description", current.description);
change.setValue("cmdb_ci", current.cmdb_ci);
change.setValue("assignment_group", current.assignment_group);
change.setValue("requested_by_date", glideDateTime.getDate());
var changeId = change.insert();
if(JSUtil.nil(current.related_records))
current.related_records = changeId;
else
current.related_records = current.related_records + "," + changeId;
gs.addInfoMessage('Change ' + change.number + ' has been created.<br/>');
}
Thanks,
Albert
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2016 08:02 AM
Hi Albert,
Say your current.u_need_by_date is 01-02-2011 06:00:00, the requested_by_date should be 01-02-2011. It will take the date value from the above. You can check the format and timezone in your user record but that should not have an impact if you were using the above code. Yes for the code you had given initially in your first post you could have faced issues.
Please let us know what error you are still getting if so. If so log the values and post it as well.