Leave date greater than 5 days and asset not returned will trigger a task in workflow
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2024 08:25 AM
Hi,
I am trying to implement a script in the if activity in the workflow to trigger a task based on the yes condition.
1- There are multiple dynamic tasks in WF which is triggering for different assets assigned to the offboarded user. We have a customized choice field " Assets Returned " with Yes/No values in all the tasks.
2- There is a variable "Leave date" in the item . The scenario is if Leave date is greater than 5days and if Asset returned is "NO", then it should trigger a task to a group.
I have implemented the below code and it is not working . Please let me know where to modify this.
(function() {
var leaveDate = new GlideDateTime(current.variables.leave_date);
gs.log('Leave Date: ' + leaveDate);
// Get the current date and time
var currentDate = new GlideDateTime();
gs.log('Current Date: ' + currentDate);
// Create a GlideDateTime object for the date 5 days from now
var date5DaysLater = new GlideDateTime();
date1DaysLater.addDaysUTC(5);
// Calculate the number of days between the leave date and current date
var daysDifference = (leaveDate.getNumericValue() - currentDate.getNumericValue()) / (1000 * 60 * 60 * 24);
gs.log('Days Difference: ' + daysDifference);
// Fetch the value of field from catalog task
var taskGR = new GlideRecord('sc_task');
taskGR.addQuery('request_item', current.sys_id);
taskGR.addQuery('short_description','CONTAINS',"Offboarding - Local IT");
taskGR.query();
var assetreturned = '';
if (taskGR.next()) {
assetreturned = taskGR.getValue('u_assets_returned');
gs.log('Asset: ' + assetreturned);
}
// Check if the leave date is greater than 5 days from now and field X is "No"
if (daysDifference > 5 && assetreturned == 'No') {
return 'yes';
} else {
return 'no';
}
})();
Please suggest .
Thanks in Advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2024 08:57 AM
There might be other issues here but from what i can see so far:
- Line 11/12 - A new GlideDateTime object is being defined on line 11 but a different variable is having 5 days added to it on line 12. From the looks of your code, you don't need this or Line 11. It looks like both can be removed. I don't know if there would be a parsing error with date1DaysLater being referenced without being defined
- Line 19 - Can the asset returned check not performed as part of the GlideRecord query? This would be more performant but also it's more reliable. If there is more than one task, it's possible the task cycled through in the GlideRecord has an assets returned value of Yes and the if statement on line 33 fails, whereas if the ordering had changed for the GlideRecord it could be No
- Line 28 - Are the choice values for u_assets_returned definitely upper case like you're using in the condition
Do you have any outputs at all in your log statements? If not, do you have an error/warning statement in the general logs at the point the workflow is evaluating the condition?
Do the values for the Workflow outcomes map to the values of 'yes' and 'no' like in your script? These are the defaults for an If activity but someone could have updated them at some point in the past
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2024 08:58 AM
Hi @Koyel Guha
Here is the updated script:
You have called different value so I think it is not working:
var date5DaysLater = new GlideDateTime();
date1DaysLater.addDaysUTC(5);// here you made wrong
(function() {
var leaveDate = new GlideDateTime(current.variables.leave_date);
gs.log('Leave Date: ' + leaveDate);
var currentDate = new GlideDateTime();
gs.log('Current Date: ' + currentDate);
var date5DaysLater = new GlideDateTime();
date5DaysLater.addDaysUTC(5);
var daysDifference = (leaveDate.getNumericValue() - currentDate.getNumericValue()) / (1000 * 60 * 60 * 24);
gs.log('Days Difference: ' + daysDifference);
var taskGR = new GlideRecord('sc_task');
taskGR.addQuery('request_item', current.sys_id);
taskGR.addQuery('short_description', 'CONTAINS', 'Offboarding - Local IT');
taskGR.query();
var assetreturned = '';
if (taskGR.next()) {
assetreturned = taskGR.getValue('u_assets_returned');
gs.log('Asset: ' + assetreturned);
}
if (daysDifference > 5 && assetreturned == 'No') {
return 'yes';
} else {
return 'no';
}
})();
Thanks and Regards
Sai Venkatesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2024 09:02 AM
Hi @Koyel Guha
Could you please check the below log:
gs.log('Leave Date: ' + leaveDate); //Expected format should be in 2015-07-24 19:01:00(YYYY-MM-DD hh:mm:ss)
var date5DaysLater = new GlideDateTime();
date1DaysLater.addDaysUTC(5);
It should be:
var date5DaysLater = new GlideDateTime();
date5DaysLater.addDaysUTC(5);
Although, you have not used this variable anywhere in the script.
Please share till where the logs are getting logged in sys_log?
Mark this as Helpful / Accept the Solution if this helps.