- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2024 11:47 PM
Hi Team,
I have created variable's in catalogs
1 start date - Date and time type variable
2 required time - Duration type variable
3 End time - String type.
I need to add the duration and date and time variables. And populate the end time in string
Can you please help me requriment.
Thank you
Siva
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2024 06:03 AM - edited 07-08-2024 06:27 AM
Hi @Community Alums,
Check the below link, it may be of any help for you.
https://www.servicenow.com/community/now-platform-forum/add-duration-to-date-time/m-p/1153814
Try below script and check:
onChange Client Script (start date), make sure the duration is first selected and then the start date.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var gaChng = new GlideAjax('Test'); //script include name
gaChng.addParam('sysparm_name', 'getEndDate'); //second parameter - function name in the SI
gaChng.addParam('sysparm_date', newValue);
gaChng.addParam('sysparm_duration', g_form.getValue('u_duration'));
gaChng.getXML(getData2);
function getData2(response) {
var answer2 = response.responseXML.documentElement.getAttribute('answer');
if (answer2) {
g_form.setValue('end_date', answer2);
}
}
}
Script Include (client callable checkbox - true):
var Test = Class.create();
Test.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getEndDate: function() {
try {
var gdt = new GlideDateTime(this.getParameter('sysparm_date'));
var dur = new GlideDuration(this.getParameter('sysparm_duration'));
gdt.add(dur);
return gdt.getValue();
} catch (ex) {
gs.info('Error:' + ex.string() + '\nLine:' + ex.lineNumber);
}
},
type: 'Test'
});
Mark the response correct and helpful if the answer assisted your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2024 06:09 AM
Something like this should work.
Add pass the fields to line 1 and 2
var gdt = new GlideDateTime('pass fiueld value');
var dur = new GlideDuration(<pass field value>);
//dur.setValue('1970-01-02 00:00:00');
gdt.add(dur);
gs.info(gdt.getDisplayValue());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2024 05:47 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2024 06:00 AM
Hi @Community Alums
I am not a developer .. scripting is out of my bucket.
@Sandeep Rajput @Anurag Tripathi any help here.
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2024 06:09 AM
Something like this should work.
Add pass the fields to line 1 and 2
var gdt = new GlideDateTime('pass fiueld value');
var dur = new GlideDuration(<pass field value>);
//dur.setValue('1970-01-02 00:00:00');
gdt.add(dur);
gs.info(gdt.getDisplayValue());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2024 06:20 AM
Hi @Anurag Tripathi ,
We need to use gdt.getValue() instead of gdt.getDisplayValue().
Mark the response correct and helpful if the answer assisted your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2024 06:03 AM - edited 07-08-2024 06:27 AM
Hi @Community Alums,
Check the below link, it may be of any help for you.
https://www.servicenow.com/community/now-platform-forum/add-duration-to-date-time/m-p/1153814
Try below script and check:
onChange Client Script (start date), make sure the duration is first selected and then the start date.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var gaChng = new GlideAjax('Test'); //script include name
gaChng.addParam('sysparm_name', 'getEndDate'); //second parameter - function name in the SI
gaChng.addParam('sysparm_date', newValue);
gaChng.addParam('sysparm_duration', g_form.getValue('u_duration'));
gaChng.getXML(getData2);
function getData2(response) {
var answer2 = response.responseXML.documentElement.getAttribute('answer');
if (answer2) {
g_form.setValue('end_date', answer2);
}
}
}
Script Include (client callable checkbox - true):
var Test = Class.create();
Test.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getEndDate: function() {
try {
var gdt = new GlideDateTime(this.getParameter('sysparm_date'));
var dur = new GlideDuration(this.getParameter('sysparm_duration'));
gdt.add(dur);
return gdt.getValue();
} catch (ex) {
gs.info('Error:' + ex.string() + '\nLine:' + ex.lineNumber);
}
},
type: 'Test'
});
Mark the response correct and helpful if the answer assisted your question.