How to generate an error message when the field value changes?

shiz
Tera Contributor

I have a Screen

When I changed the ‘Week Starts On' of value is not this Week of Manday date ,Then system generate an error message!

shiz_0-1731385769122.png

How to write a Business Rule?

shiz_1-1731385956618.pngshiz_2-1731385971022.png

 

1 ACCEPTED SOLUTION

@shiz 

 

Try with below script :

(function executeRule(current, previous /*null when async*/ ) {

    var weekStartOn = new GlideDateTime(current.u_week_starts_on); // Please replace u_week_starts_on with your column Name
    var day = weekStartOn.getDayOfWeekLocalTime();
    if (day != '1') {
        gs.addErrorMessage('Today is not Monday'); // Change the message as per your requirement
    }


})(current, previous);

Please mark this response as correct and helpful if it assisted you with your question.

View solution in original post

3 REPLIES 3

Amit Verma
Kilo Patron
Kilo Patron

Hi @shiz 

 

You can refer below posts to build this logic -

https://www.servicenow.com/community/virtual-agent-forum/how-to-validate-the-day-of-the-date/td-p/27...

https://www.servicenow.com/community/developer-forum/how-to-find-day-of-the-week-in-service-now/m-p/...

 

Thanks and Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

shiz
Tera Contributor

@Amit Verma  hello

I'm not very familiar with the syntax of this writing. How should I write it specifically? Thank you very much.

@shiz 

 

Try with below script :

(function executeRule(current, previous /*null when async*/ ) {

    var weekStartOn = new GlideDateTime(current.u_week_starts_on); // Please replace u_week_starts_on with your column Name
    var day = weekStartOn.getDayOfWeekLocalTime();
    if (day != '1') {
        gs.addErrorMessage('Today is not Monday'); // Change the message as per your requirement
    }


})(current, previous);

Please mark this response as correct and helpful if it assisted you with your question.