- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2023 03:59 AM - edited 10-14-2023 04:03 AM
Hi Community, here is a use case I need to auto-populate a set of fields with a given set of values (like how we do it for standard change upon creation) but for a different change model.
Not using any kind of templates here, please suggest to me any other way that I could use to solve this use case.
Please help me resolve this use case.
Thanks in Advance.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2023 04:48 AM - edited 10-15-2023 05:03 AM
Here is how your html should be used.
g_form.setValue('u_html','<p> </p> <p> </p> <table style="border-collapse: collapse; width: 100.071%; height: 366.383px; margin-left: auto; margin-right: auto;" border="1"> <tbody> <tr style="height: 63.7969px;"> <td style="width: 41.1224%; height: 63.7969px; text-align: center;" colspan="3"> <p style="text-align: center;"><span style="font-family: verdana, geneva; font-size: 8pt;"><strong> </strong></span></p> <p style="text-align: center;"><span style="font-family: verdana, geneva; font-size: 8pt;"><strong>SOURCE</strong></span></p> </td> <td style="height: 63.7969px; width: 26.1365%; text-align: center;" colspan="2"> <p><span style="font-family: verdana, geneva; font-size: 8pt;"><strong> </strong></span></p> <p><span style="font-family: verdana, geneva; font-size: 8pt;"><strong>DESTINATION</strong></span></p> </td> <td style="width: 66.2292%; text-align: center; height: 63.7969px;" colspan="4"> <p><span style="font-family: verdana, geneva; font-size: 8pt;"><strong> </strong></span></p> <p><span style="font-family: verdana, geneva; font-size: 8pt;"><strong>KEY DETAIL</strong></span></p> </td> </tr> <tr style="height: 68.1953px; text-align: center;"> <td style="width: 12.429%; height: 68.1953px;"><span style="font-family: verdana, geneva; font-size: 8pt;"><strong>Application</strong></span></td> <td style="width: 14.7728%; height: 68.1953px;"> <p><span style="font-family: verdana, geneva; font-size: 8pt;"><strong>Hostname</strong></span></p> </td> <td style="width: 13.9205%; height: 68.1953px;"> <p><span style="font-family: verdana, geneva; font-size: 8pt;"><strong>IP Address</strong></span></p> </td> <td style="width: 13.6364%; height: 68.1953px;"> <p><span style="font-family: verdana, geneva; font-size: 8pt;"><strong>Hostname</strong></span></p> </td> <td style="width: 12.5001%; height: 68.1953px;"> <p><span style="font-family: verdana, geneva; font-size: 8pt;"><strong>IP Address</strong></span></p> </td> <td style="width: 9.73017%; height: 68.1953px;"> <p><span style="font-family: verdana, geneva; font-size: 8pt;"><strong>TCP Ports</strong></span></p> </td> <td style="width: 11.0057%; height: 68.1953px;"> <p><span style="font-family: verdana, geneva; font-size: 8pt;"><strong>UDP Ports</strong></span></p> </td> <td style="height: 68.1953px; width: 45.4933%;" colspan="2"> <p><span style="font-family: verdana, geneva; font-size: 8pt;"><strong>Traffic Direction</strong></span></p> </td> </tr> <tr style="height: 234.391px; text-align: center;"> <td style="width: 12.429%; height: 234.391px;"> </td> <td style="width: 14.7728%; height: 234.391px;"> </td> <td style="width: 13.9205%; height: 234.391px;"> </td> <td style="width: 13.6364%; height: 234.391px;"> </td> <td style="width: 12.5001%; height: 234.391px;"> </td> <td style="width: 9.73017%; height: 234.391px;"> </td> <td style="width: 11.0057%; height: 234.391px;"> </td> <td style="height: 234.391px; width: 45.4933%;" colspan="2"> </td> </tr> </tbody> </table>');
This how the end result looks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2023 04:58 AM
Hi @Shwetha Shenoy ,
Certainly! In your scenario, you can create an `onBefore` Business Rule that triggers before a new record is inserted. This rule will check the specific conditions you define and populate the fields accordingly. Here's a more specific explanation of the steps:
### Creating an `onBefore` 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.
- Choose the appropriate table (the table for your change model).
3. **Configure Business Rule Conditions:**
- Set the `When to Run` field to `Before`.
- Define the conditions under which the Business Rule should run. For example, you might want to check the type of change or other specific fields to identify the records that need auto-population.
4. **Write the Script:**
- In the `Advanced` tab, write a script that checks your defined conditions and populates the fields accordingly. For example:
(function executeRule(current, previous /*null when async*/) {
// Check conditions to identify the specific change model records
if (current.type == 'specific_type' && current.field_name == 'specific_value') {
// Populate fields with desired values
current.field_name1 = 'desired_value1';
current.field_name2 = 'desired_value2';
// ... Populate other fields as needed
}
})(current, previous);
Ensure that you customize the conditions (`current.type`, `current.field_name`, etc.) to match the criteria for the records you want to auto-populate.
5. **Test the Business Rule:**
- Create a new record that meets the defined conditions to trigger the Business Rule.
- Verify that the fields are auto-populated as expected.
By using an `onBefore` Business Rule, the fields will be populated before the record is inserted into the database, ensuring that the desired values are set during record creation based on your specified conditions.
Mark my answer helpful & accepted if it helps you resolve your query.
Thanks,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2023 09:56 AM
Hi,
This isn't working when we create a new record without saving, after saving the record the fields are getting auto-populated.
I need to auto-populate the values upon creation itself.
Please find the below screenshot of the Business Rule :
Thanks @Danish Bhairag2

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2023 08:13 PM
@Shwetha Shenoy You can use the Template feature to create a template for the change request and then apply the template on the form to auto populate the fields.
Also, from the previous post it looks like the business rules are out of equation for you as you would like the template to be applied on the form itself before the changes are saved in databse.
Here you can either use the template feature or use a onLoad client script to populate the data on the form.
Hope this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2023 01:02 AM - edited 10-15-2023 01:03 AM
Hi @Sandeep Rajput,
Templates weren't allowed to be used in this use case, but the on-load client script seems to work nicely. Thank you so much.
But here is one more problem that I am facing is how to populate a field (HTML type) with a table.
Please find the below screenshot for a better understanding :
Upon creation of a new change record , this Input field should have a table with rows and columns in it. So that the values could be populated by the user.
Thanks again.