- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2022 08:15 AM
Hey all,
We have a catalogue item which requires the user to enter a date field. There is another variable called 'Completion Date' which should auto-populate with the users entered date + 28 days.
I presume this cannot be done on the catalogue stage as the date the user is entering isn't held anywhere at this point, so instead I have tried to put together a business rule to run on the requested item once it has been created. Essentially I just want it to take the date from the requested_date variable, add 28 days to the value, and then insert this new date into the completion_date variable. I've tried the below and it's inserting a date into the variable but it's not adding the 28 days.
(function executeRule(current, previous /*null when async*/) {
var reqdate = current.variables.requested_date;
reqdate.addDaysLocalTime(30);
current.variables.completion_date = reqdate;
current.update();
})(current, previous);
Not tried something like this before so any assistance would be greatly appreciated - thank you!
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2022 08:39 AM
Try the below script:
Please try with before insert BR and remove current.update
(function executeRule(current, previous /*null when async*/ ) {
var upd = new GlideDateTime(current.variables.requested_date);
upd.addDaysLocalTime(30);
current.variables.completion_date = upd.getLocalDate();
})(current, previous);
(=tested)
Hope it helps
Thanks,
Murthy
Murthy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2022 08:29 AM
HI
Use the below script :
Type: OnChange()
Catalog Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var cdt = g_form.getValue('u_date_concern_reported'); //first date field
var addtime = 28;
var addtype = 'day';
var gr = new GlideAjax('global.ClientDateTimeUtils');
gr.addParam('sysparm_name', 'addDateAmount');
gr.addParam('sysparm_fdt', cdt);
gr.addParam('sysparm_addtime', addtime);
gr.addParam('sysparm_addtype', addtype);
gr.getXML(ajaxResponse);
function ajaxResponse(serverResponse){
var answer = serverResponse.responseXML.documentElement.getAttribute("answer");
g_form.setValue('u_th_day', answer); //second date field
alert(answer);
}
}
Mark my answer correct & Helpful, if Applicable.
Thanks,
Sandeep
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2022 08:39 AM
Try the below script:
Please try with before insert BR and remove current.update
(function executeRule(current, previous /*null when async*/ ) {
var upd = new GlideDateTime(current.variables.requested_date);
upd.addDaysLocalTime(30);
current.variables.completion_date = upd.getLocalDate();
})(current, previous);
(=tested)
Hope it helps
Thanks,
Murthy
Murthy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-24-2022 03:17 AM
Thank you Murthy - this has done the trick.