- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-01-2024 08:17 AM
business rule to restrict start date field to select from tomorrow only and end date field is from yesterday.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2024 12:36 AM
Hi @vinnus ,
Oh okayy now I'm clear with your reqiurments you can use below code now
It will work for you
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
var currentDate = new GlideDateTime();
var startDate = current.start_date;
var endDate = current.end_date;
var convertStartDate = new GlideDateTime(startDate);
var convertEndDate = new GlideDateTime(endDate);
gs.log('Inside Restrict Date ' + currentDate + " SatrtDate = " + convertStartDate + " End Date = " + convertEndDate);
if (currentDate.compareTo(convertStartDate) == -1 || currentDate.compareTo(convertStartDate) == 0) {
gs.log("Inside if 2");
gs.addErrorMessage("Can not select from future");
current.setAbortAction(true);
}
})(current, previous);
Result
Highlighted date is selected from future , when I save this record it shows me the error
Please mark my answer correct and helpful if this works for you
Thanks and Regards
Sarthak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-01-2024 09:48 AM
Hi @vinnus ,
I tried your problem in my PDI and it works for me please refer below script
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
var currentDate = new GlideDateTime();
var startDate = current.start_date;
var endDate = current.end_date;
var convertStartDate = new GlideDateTime(startDate);
var convertEndDate = new GlideDateTime(endDate);
gs.log('Inside Restrict Date ' + currentDate + " SatrtDate = " + convertStartDate + " End Date = " + convertEndDate);
if (currentDate.compareTo(convertEndDate) == 1 || currentDate.compareTo(convertEndDate) == 0) {
gs.log("Inside if 1")
gs.addErrorMessage("Can not select from past");
current.setAbortAction(true)
}
if (currentDate.compareTo(convertStartDate) == -1 || currentDate.compareTo(convertStartDate) == 0) {
gs.log("Inside if 2")
gs.addErrorMessage("Can not select from future");
current.setAbortAction(true)
}
})(current, previous);
Please mark my answer correct and helpful if this works for you
Thanks and Regards
Sarthak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-01-2024 10:14 AM
Hi Sarthak,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-01-2024 10:30 AM
Hi @vinnus
Just create a BR with trigger condition and
then use this script
(function executeRule(current, previous /*null when async*/) {
currentDate=new GlideDateTime().getDate();
endDate=new GlideDateTime(current.work_end).getDate();
if(endDate==currentDate||endDate>currentDate)
{
gs.addErrorMessage('End date should not be not current or future date');
current.setAbortAction(true);
}
})(current, previous);
Also check the time zone and validate accordingly.
Thanks & Regards
Seraj
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2024 12:36 AM
Hi @vinnus ,
Oh okayy now I'm clear with your reqiurments you can use below code now
It will work for you
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
var currentDate = new GlideDateTime();
var startDate = current.start_date;
var endDate = current.end_date;
var convertStartDate = new GlideDateTime(startDate);
var convertEndDate = new GlideDateTime(endDate);
gs.log('Inside Restrict Date ' + currentDate + " SatrtDate = " + convertStartDate + " End Date = " + convertEndDate);
if (currentDate.compareTo(convertStartDate) == -1 || currentDate.compareTo(convertStartDate) == 0) {
gs.log("Inside if 2");
gs.addErrorMessage("Can not select from future");
current.setAbortAction(true);
}
})(current, previous);
Result
Highlighted date is selected from future , when I save this record it shows me the error
Please mark my answer correct and helpful if this works for you
Thanks and Regards
Sarthak