- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2017 10:31 AM
Hi all,
I've got a client script and script include that I'm using to set a date field based on the value of a variable on an asset record. I need to incorporate some code to set the value in the new field to match the user's date format to ensure there are no errors. Would using GlideDateTime and setDisplayValue be the way to go? my script below is setting the field correctly, but it's setting it in the system default format, I need it to set the value in the user's date format. thanks!
right now I have this for client script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading ) {
return;
}
if (newValue == ''){
g_form.clearValue('computer_retirement_date');
return;
}
var ga = new GlideAjax('u_Computer_Requests_Ajax');
ga.addParam('sysparm_name', 'getRetirementDate');
ga.addParam('sysparm_ci', g_form.getValue('current_computer'));
ga.getXML(dateLookup); // Always try to use asynchronous (getXML) calls rather than synchronous (getXMLWait)
}
// Callback function to process the response returned from the server
function dateLookup(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('computer_retirement_date',answer);
}
and this is my script include:
getRetirementDate: function(){
var retVal; // Return value
var ci = this.getParameter('sysparm_ci');
var hardwareRec = new GlideRecord('alm_hardware');
hardwareRec.addQuery('ci',ci);
hardwareRec.query();
if(hardwareRec.next())
{
retVal = hardwareRec.retirement_date;
}
return retVal;
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2017 10:54 AM
modify the script include as:
getRetirementDate: function(){
var gdt = new GlideDateTime();
var retVal ; // Return value
var ci = this.getParameter('sysparm_ci');
var hardwareRec = new GlideRecord('alm_hardware');
hardwareRec.addQuery('ci',ci);
hardwareRec.query();
if(hardwareRec.next())
{
gdt= hardwareRec.retirement_date;
retVal = gdt.getDisplayValue();
}
return retVal;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2017 10:34 AM
Hi Patirck,
You can define the format in set display value
3.15 setDisplayValue(value, format)
Sets a date and time value using the current user's time zone and the specified date and time format. This method throws a runtime exception if the date and time format used in the valueparameter does not match the format parameter. You can retrieve the error message by calling getErrorMsg() on the GlideDateTime object after the exception is caught. This method is available starting with the Eureka release.
3.15.1 Input Fields
Parameters:
- value - (String) The date and time in the current user's time zone.
- format - (String) The date and time format to use to parse the value parameter.
3.15.2 Output Fields
Returns: void
3.15.3 Example
var gdt = new GlideDateTime("2011-02-02 12:00:00"); gdt.setDisplayValue("20-5-2011 12:00:00", "dd-MM-yyyy HH:mm:ss"); gs.print(gdt.getValue());
Output:
2011-05-20 19:00:00
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2017 10:38 AM
Hi Varun,
I'm seeing that, but am having trouble seeing how I add that in to my client script...can you guide me?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2017 10:44 AM
will the system pick up the user's specified date format field value? or do I need an additional query to the user's record to get that?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2017 10:54 AM
modify the script include as:
getRetirementDate: function(){
var gdt = new GlideDateTime();
var retVal ; // Return value
var ci = this.getParameter('sysparm_ci');
var hardwareRec = new GlideRecord('alm_hardware');
hardwareRec.addQuery('ci',ci);
hardwareRec.query();
if(hardwareRec.next())
{
gdt= hardwareRec.retirement_date;
retVal = gdt.getDisplayValue();
}
return retVal;