- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-28-2017 05:30 AM
Hello all,
I am trying to sync the start & end date of a Change Request record with the planned start & planned end date of the corresponding Change Tasks, related to it (the Change Request record).
My goal is the following:
- let's imagine that I have a Change Request which contains 5 Change Tasks. I would like the start_date of the Change Request to be the value of the earliest planned_start_date of the five Change Tasks, while the end_date of the Change Request to be the value of the latest planned_end_date of the five Change Tasks.
Saying this, if I log into one of the Change Tasks and change its planned_start_date to even an earlier one -> then, this change should appear in the start_date of the Change Request and like-versa if I change the planned_end_date of a Change Task to later date -> the later date should be updated into the end_date of the Change Request.
NOTE: in my instance I did not have Planned Start / End Date fields in the Change Task table, thus I created custom ones: u_planned_start_date & u_planned_end_date, while in the Change Request table I have start_date & end_date;
To achieve it, I created a Business Rule against Change Task (change_task) table, which runs after Insert & Update when Planned start date or Planned end date changes (these are the filter conditions). However, I am not able to find the right function which I need to use and thus my Business Rule uses 'get.NumericValue()'... I believe this is what breaks my code, but in all cases I will appreciate if somebody may check and confirm my code. It follows below in blue:
(function executeRule(current, previous /*null when async*/) {
// This Business Rule ensures that a Change Request will have the earliest Planned Start Date of all its Change Tasks as Start Date & the latest Planned End Date of all its Change Tasks;
var dct1 = new GlideDateTime(current.u_planned_start_date);
dct1 = dct1.getNumericValue();
var dct2 = new GlideDateTime(current.u_planned_end_date);
dct2 = dct2.getNumericValue();
var cr = current.change_request;
var ct = new GlideRecord('change_task');
ct.addQuery('change_request', cr);
ct.addQuery('state', 'active');
ct.query();
var tempstart = dct1;
var tempend = dct2;
while(ct.next()){
var start = new GlideDateTime(ct.getDisplayValue('u_planned_start_date'));
start = start.getNumericValue();
var end = new GlideDateTime(ct.getDisplayValue('u_planned_end_date'));
end = end.getNumericValue();
if(tempstart <= start){
tempstart = tempstart;
}
else {
tempstart = start;
}
if(tempend >= end){
tempend = tempend;
}
else {
tempend = end;
}
}
var nov = new GlideRecord('change_request');
nov.addQuery('change_request', current.change_request);
nov.query();
while(nov.next()){
nov.start_date = tempstart;
nov.end_date = tempend;
nov.update();
}
})(current, previous);
Thank you in advance!
Cheers, Georgi
Solved! Go to Solution.
- Labels:
-
Change Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-28-2017 06:22 AM
Hi Georgi,
What is the data type of the custom field that you have created on change_task form? If it is date/time field you need not get the numeric value and all. You can directly copy the date/time values like change_request.start_date = current.u_planned_start_date.
A sample query for planned start date would be
var cr = new GlideRecord('change_request');
cr.addQuery('sys_id',current.getvalue('change_request');
cr.query();
{
if(current.u_planned_start_date <= cr.start_date)
cr.start_date = current.u_planned_start_date;
cr.update();
}
In case if you have created the custom fields of only date field, then while assigning the value, you will have to do something like
cr.start_date = current.u_planned_start_date + "23:00:00";
cr.update();
I suggest you to have date/time fields on both the forms as that would ease your entire trouble.
Best Regards,
Varad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-28-2017 11:45 PM
Good Morning, Varad!
I've managed to cover the scenario using the below Business Rule:
Table: Change Task (change_task);
Active: true;
Advanced: true;
When to run: before Insert & Update;
Filter Conditions: Planned start date changes OR Planned end date changes;
Script:
(function executeRule(current, previous /*null when async*/) {
var cr = new GlideRecord('change_request');
cr.addQuery('sys_id', current.getValue('change_request'));
cr.query();
if(current.u_planned_start_date <= cr.start_date) {
cr.start_date = current.u_planned_start_date;
cr.update();
}
if(current.u_planned_end_date >= cr.end_date) {
cr.end_date = current.u_planned_end_date;
cr.update();
}
})(current, previous);
I've tested couple of scenarios with several Change Tasks, even added on a later stage.
I may say the Business Rule works as expected.
Taking under consideration that you helped me a lot, I will mark this thread as "Answered" and will mark your initial reply to my question as the "Correct answer".
Thank you very much for helping me out, Varad! Enjoy the day ahead!
Best regarrds,
Georgi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-29-2017 01:13 AM
Thanks Georgi,
Glad, I am able to help you. Have a nice day
Best Regards,
Varad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-29-2017 09:57 AM
Ok, so the original script of the BR I pasted above was not a good one
Today I modified it. It is quite better now.
I will share it in here, in case someone encounters a similar task. It follows below, the rest of the BR details (Table, When to run, are the same as the listed above):
var cr = new GlideRecord('change_request');
cr.addQuery('sys_id', current.change_request.sys_id);
cr.query();
if(cr.next()){
// gs.addInfoMessage('Comapring Dates!');
if(current.planned_start_date <= cr.start_date){
cr.start_date = current.planned_start_date;
cr.update();
}
if(current.planned_end_date >= cr.end_date){
cr.end_date = current.planned_end_date;
cr.update();
}
}
Cheers, Georgi