Script condition Flow designer

Natanael
Tera Contributor

Hello everyone, I have a workflow that runs every day to check whether it's an employee's birthday or not. For this, I created a custom field called 'Birthdate,' but I need to create a conditional script that selects only the day and month. Currently, it evaluates the condition based on the current date's year, which is causing an error. If anyone can help me, I would appreciate it. Thanks!!

 

1 ACCEPTED SOLUTION

So basically your scheduled job will run at 6 everyday.

In the scheduled job, query all the users which has birthday on the same day and month.

Once you find that, for all those user trigger event. And the event should then trigger the notification.

So basically you need to create a event registry and a notification which should trigger based on event.

Here is an article, how you create event and notification

https://tech.forums.softwareag.com/t/servicenow-email-notification-via-event/237437

 


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

View solution in original post

7 REPLIES 7

Sanuja Kulkarni
Mega Guru

Hello Natanael,

Create a new script action in your workflow and write following script

 

var currentDate = new GlideDateTime();
var currentDay = currentDate.getDayOfMonthLocalTime();
var currentMonth = currentDate.getMonthLocalTime();

var employeeBirthdate = new GlideDateTime(current.birthday); // Assuming 'current' refers to the current workflow context
var employeeBirthDay = employeeBirthdate.getDayOfMonthLocalTime();
var employeeBirthMonth = employeeBirthdate.getMonthLocalTime();


if (currentDay == employeeBirthDay && currentMonth == employeeBirthMonth) {
  
  gs.addInfoMessage("It's the employee's birthday!");
} else {
  // It's not the employee's birthday
  gs.addInfoMessage("It's not the employee's birthday.");
}

 

 

Perhaps I expressed myself incorrectly. The workflow runs every day at 6:00 AM. It retrieves records from a specific table and then sends an email only to those who are celebrating their birthdays on that day. However, it compares with the current date, including the year. Therefore, it only sends the email if the dates match day, month, and year. For example:

Employee 1 has a birthday on 27/02/1994, but he won't receive the email because the dates don't match. To receive the email, he would have to be born on 27/02/2024.

I would like the workflow to only compare the day and month, ignoring the year. However, I haven't been able to create a condition that meets this requirement. I believe it might only be achievable through a script action.

Hi,

Please find the below script 

 

 

// Get the current date and month in the format '00-MM-DD'
var currentMonthDay = '00-' + (new GlideDate().getMonthDay());

// Query the sys_user table for users with matching birthday
var userGR = new GlideRecord('sys_user');
userGR.addQuery('dob', 'LIKE', currentMonthDay);
userGR.query();

// Loop through the users and send the birthday notifications
while (userGR.next()) {
    var recipientEmail = userGR.email.getDisplayValue();
    var emailTemplate = 'TEMPLATE_NAME'; // Replace 'TEMPLATE_NAME' with the name of your created email template
    gs.eventQueue('birthday.notification', userGR, emailTemplate, recipientEmail);
}

 

 

Is there any way to add a condition that meets the requirements in the red-marked place or where there is a yellow arrow indicating?