- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2023 07:59 PM
Hello,
I want to set today to Date column with client script.
I understand that I can set it with javascript: new GlideDateTime().getDate() in default value.
But, it needs to set today, only when the value of other column is changed.
So I think that I need to use client script.
How I can set it with client script ?
Regards,
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2023 08:37 AM
Hi @matsui ,
You can use onChange client script. Here, I have set "Today's Date" on change of priority.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var today_date = new Date();
var today_date_str = formatDate(today_date, g_user_date_format);
//Type appropriate comment here, and begin script below
if (oldValue != newValue) {
g_form.setValue('u_todays_date', today_date_str);
}
}
Please mark my answer helpful if you find it useful.
Thanks,
Kavita Bhojane
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2023 09:34 PM - edited 10-23-2023 09:37 PM
Hi @matsui
There's couple of ways to achieve this.
1. We can call AJAX from a Clientcallable Script Include and return the function "new GlideDateTime().getDate()" you mentioned.
Sample below.
//Script Include
var CLIncidentUtils = Class.create();
CLIncidentUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getTodayDate: function(){
return new GlideDateTime().getDate();
},
type: 'CLIncidentUtils'
});
//Client Script => just changes to onChange per your requirement
function onLoad() {
var ga = new GlideAjax('global.CLIncidentUtils');
ga.addParam('sysparm_name', 'getTodayDate');
ga.getXMLAnswer(function(response){
alert(response);
});
}
2. You can also get today date right on Client Script, just use this object "Date()"
Sample below.
var today = new Date();
var today_date = formatDate(today_date, g_user_date_format);
alert(today_date);
Let me know if it works for you.
Cheers,
Tai Vu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2023 10:32 PM
I fixed your code like the following and it worked as expected:
var today = new Date();
var today_date = formatDate(today , g_user_date_format);
g_form.setValue(dare_field, today_date );
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2023 10:52 PM
@matsui Great!
If the approach can save your time. Can you help to mark my comment as solution?
So, it might help others in the same situation. 🤙
Cheers,
Tai Vu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2023 08:37 AM
Hi @matsui ,
You can use onChange client script. Here, I have set "Today's Date" on change of priority.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var today_date = new Date();
var today_date_str = formatDate(today_date, g_user_date_format);
//Type appropriate comment here, and begin script below
if (oldValue != newValue) {
g_form.setValue('u_todays_date', today_date_str);
}
}
Please mark my answer helpful if you find it useful.
Thanks,
Kavita Bhojane
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-28-2023 10:09 AM