- 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 08:24 PM
Hi @matsui ,
U can use a onChange script to call a script include using GlideAjax n return today's date using new GlideDateTime as it does not work in client side.
Thanks,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2023 09:14 PM
@DanishBhairag2
Thank you for your answer.
Could you give me sample code ?
Regards,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2023 09:16 PM
Sure will send one
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2023 09:36 PM - edited 10-23-2023 09:39 PM
Hi @matsui ,
Script Include: Please provide snc_internal role when asked for which role can access the script after submitting the script
var SetDate = Class.create();
SetDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
fetchDate: function() {
var date = new GlideDateTime().getDate();
return date;
},
type: 'SetDate'
});
Client Script: OnChange
UI Type : All
Select proper variable name on change of which u wish to trigger this script.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('SetDate');
ga.addParam('sysparm_name', 'fetchDate');
ga.getXML(respond);
function respond(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('date_field', answer);// replace proper backend name of your variable
}
}
Thanks,
Danish