Business Rule with abort action running in Contextual Sidebar on clicking 'Schedule'

Ron Legters
ServiceNow Employee
ServiceNow Employee

Change Management: Requirement is planned start and end dates can't be in the past. I'm enforcing this with onChange Client Scripts on the 'start_date' and 'end_date' fields and displaying a field error. To prevent saving with the bad dates, I also wrote an onSubmit script that stops the save. All of this works fine in the legacy view. 

In Service Operations Workspace, the onSubmit script doesn't work because it uses a GlideAjax call, that's synchronous. If it's asynchronous the Submit completes before the answer comes back that it shouldn't save. So - I created a business rule, that essentially does the same thing as the onSubmit, and scripted it to only run on the SOW, so users in legacy don't get two error messages. Here's where it get weird: in the 'Schedule' card in the contextual side panel (accessed by clicking 'Schedule') if you set dates in the past and click the 'Schedule' button, it simply fails to save - no error. If you then click the 'edit' icon in the Schedule card, you get the error message. So, it appears the BR is running when a user clicks the 'Schedule' button on that card (it's aborting) but the timing is off, so it doesn't tell the user why it's not saving. 

Any idea what's going on here?

1 ACCEPTED SOLUTION

Thank you very much for your help on this. I've fixed it!

In an OOB dev instance, the client script 'Form Submitted Failure' is linked to the data resource 'Form Controller' for the event 'Form Submit Completed'. In my customer's instance, it was not, so I linked it. Also, the Form message property of 'Form 1' in my customer's instance was populated by 'record_1>form>message' and OOB it's a Client State parameter called 'formMessages'. I created this client state parameter in the instance, and linked it to the Form Message property. 
Only issue now is that there is a scroll bar in the 'Invalid value on Update' message for some reason, but that's just cosmetic. 

View solution in original post

6 REPLIES 6

Joni V B
Tera Guru

Hi @Ron Legters ,

 

Could it be that the message is there upon clicking schedule but simply not shown? Are you adding a gs.addErrorMessage or something?

What if you write something to the log, do you see it appear immediately after the first click (that doesn't execute the save)?

 

edit: Wouldn't it be possible to add your client side validation in the Workspace client script block of the schedule UI Action and prevent submission from there? 

 

Best regards,

Joni

Ron Legters
ServiceNow Employee
ServiceNow Employee

Thanks for the reply!

Here is the relevant part of the script:

var start = current.start_date;
var end = current.end_date;
var diffst = gs.dateDiff(start, gs.nowDateTime(), true);
var diffend = gs.dateDiff(end, gs.nowDateTime(), true);

if (diffst >= 0 || diffend >= 0) {
gs.info('RL: Planned Start date in the past');
gs.addErrorMessage(gs.getMessage('Planned Start and End dates must be in the future'));
current.setAbortAction(true);
}
I just added the gs.info - it does write to the log as soon as I click the 'Schedule' button. The 'addErrorMessage' doesn't display on the card until I click the 'edit' pencil again, so maybe it's there, but just not visible?
I can't just add the validation to the Schedule UI Action because it's not a UI Action. It's whatever clickable things are called in UI Builder - and it's not in the list of Declarative actions, either, so I'm digging for it in UI Builder.

Hi @Ron Legters ,

 

The button can be found inside the page Schedule (Change Conflict Calendar Default).
It executes a client script onClick 'On schedule button click' and then runs a serverside save, maybe it's possible to add your date logic in there.

You'll also find that there is already a DatesErrorMessage Client stage param, you can probably use that one to add your error message to.

Another alternative might be looking at the form controller on the page. There is allready an event handler from 'Form submitted failure'. If you are setting the abortaction, maybe this is already getting captured in the workspace.

 

Best regards,

Joni

Ron Legters
ServiceNow Employee
ServiceNow Employee

Hi -

I've learned there's an baseline BR that checks if the planned end date is before the start date. It aborts the save and displays an error. In my customer's instance, this BR behaves exactly like mine, but in a new developer instance, it displays the error message in the Schedule card. It seems now I need to figure out how my customer broke this functionality. 🙂 (or how I did, earlier in this engagement) 

I'll compare the things you mentioned in your reply in my base instance to my customer's. Hopefully something obvious turns up.