Automatically populate the "Start Date" field with the current date and time using Business Rule
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-15-2023 08:05 PM
Automatically populate the "Start Date" field with the current date and time using Business Rule
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-15-2023 08:54 PM
Hi @sivasankar Yash ,
Certainly! You can use a Business Rule in ServiceNow to automatically populate the "Start Date" field with the current date and time when a record is created. Here's how you can create the Business Rule:
1. **Navigate to Business Rules:**
- Go to `System Definition` > `Business Rules` in ServiceNow.
2. **Create a New Business Rule:**
- Click on `New` to create a new Business Rule.
- Fill out the necessary fields:
- **Name:** Provide a descriptive name for your Business Rule.
- **Table:** Select the table for which you want to populate the "Start Date" field.
3. **Configure the Business Rule:**
- Set the `When to run` field to `Before` for "Insert".
- Set the `Insert` condition to `true`. This ensures the Business Rule runs when a new record is inserted.
4. **Write the Script:**
- In the `Advanced` tab, write the script to populate the "Start Date" field with the current date and time. Use the `current` object to refer to the record being inserted.
(function executeRule(current, previous /*null when async*/) {
// Populate the "Start Date" field with the current date and time
current.start_date = new GlideDateTime();
})(current, previous);
In this script, `current.start_date` is set to a new `GlideDateTime()` object, which represents the current date and time.
5. **Save the Business Rule:**
- Save the Business Rule configuration.
Now, whenever a new record is created in the specified table, the "Start Date" field will automatically be populated with the current date and time due to the Business Rule. Adjust the field and table names in the script as per your specific requirements.
Mark my answer helpful & accepted if it helps you resolve your issue.
Thanks,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-15-2023 08:55 PM
Where you are running this script?
The solution will be:
1. When: Before
2. Insert: True
3. Script:
current.start_date = new GlideDateTime();//Check the back end name of start date
Regards, Shekhar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-15-2023 08:57 PM
Hi @sivasankar Yash ,
The other alternative except business rule would be just under default value tab for start field write below line
javascript: new GlideDateTime();
Copy the above code by removing &colon.
Mark my answer helpful & accepted if it helps you.
Thanks,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-16-2023 12:06 AM
Hi @sivasankar Yash ,
In the Start Date field you can find the Default field in the section in that you can write like this
javascript:new GlideDateTime()
Thanks