Business rule is not generating the error message but is aborting the action as expected

Rhonda9
Tera Expert

Hello,

 

I need help with a business rule I created to validate if a person attempt to create a timesheet with the date 30 days in future to abort action from mobile agent.  The br is aborting the action as expected but none of the error messages are being displayed.  The message I am getting is "no results found".

   Here is the before business rule 
// Ensure the required field is available
    if (!current.week_starts_on) {
        current.addErrorMessage('The "week_starts_on" field is required.');
        return;
    }

    // Get the current date and add 30 days
    var curDate = new GlideDateTime();
    curDate.addDaysLocalTime(30);

    // Convert the 'week_starts_on' field to GlideDateTime
    var wsoDate = new GlideDateTime(current.week_starts_on);

    // Log for troubleshooting
    gs.debug('Current date: ' + curDate + ', Week start date: ' + wsoDate);

    // Compare dates and check if the 'week_starts_on' date is more than 30 days in the future
    if (wsoDate.compareTo(curDate) > 0) {
        current.addErrorMessage('You cannot submit a time card for a date more than 30 days in the future.');
       
        // Abort the action to prevent the time sheet from being created
        current.setAbortAction(true);

        gs.debug('Action aborted due to invalid "week_starts_on" date.');
        return;
    }
})(current, previous
7 REPLIES 7

Bert_c1
Kilo Patron

Hi @Rhonda9 

 

the logic I have in my business rule for the 'time_sheet' follows:

 

	// Add your code here
	var wsoDate = current.week_starts_on;
	var curDate = new GlideDate();
	curDate.addDays(30);
	gs.addInfoMessage('wso date: ' + wsoDate + ' and date limit = ' +curDate);
	if (wsoDate > curDate) {
		gs.addErrorMessage('You cannot create a time card for a date more than 30 days in the future.');
		current.setAbortAction(true);  // Abort the insert/update action
	}

 

I hope this helps.

Rhonda9
Tera Expert

Thank you, I am getting the message  "the operation couldn't be completed (Agent.NowAPIError error 4"

 

What I posted works in my PDI, seems your may be in the wrong Application scope. Mine is in Global, the same app scope as 'time_sheet' table. mY BR has a filter contion: 'Week starts on' 'Changes'.

Rhonda9
Tera Expert

Yes, I am in the global scope and the same condition.   I believe it's something simple that I'm missing.