in servicenow, how to call business rule for on load client sscript
Assistant
User
my requirement is, i want to populate the value in reference field on alm_hardware table, i wrote a business rule that is working, but working only if i changed the value of assigned to. i want to populate the field on onLoad... my business rule is current.u_buu_head = current.department.business_unit.bu_head;
Assistant
User
i want to populate it on existing records whnever i opens the form
Assistant
User
but script include looks like you havents it for client callable
Assistant
User
i have a choice field in alm_hardware table named dual asset, i want to clear the field values whenver assigned to changes or state changes, i write a client script for this and it is working if fields get changed from form view, but from list view, it is not workinh
Assistant
In ServiceNow, invoking a business rule from a client script (for instance, an "onLoad" client script) directly isn't typically feasible, as business rules run on the server-side while client scripts run on the client-side (in the browser). However, you can achieve a similar result by using GlideAjax to call a script include from your client script. The script include can perform your server-side logic, much like a business rule.
Here’s how you can do it:
Step 1: Create a Script Include
Go to System Definition > Script Includes.
Click New.
Define the Name, for example, MyScriptInclude.
Check the Client Callable checkbox.
Write the needed server-side logic inside the script include.
Here's an example script include:
javascript
var MyScriptInclude = Class.create();
MyScriptInclude.prototype = {
initialize: function() {},
myServerSideFunction: function() {
// Your server-side code here
var someData = "Hello from server-side!";
return someData;
},
type: 'MyScriptInclude'
};
Step 2: Create an onLoad Client Script
Go to the desired table or form where you want the client script.
Create a new Client Script with:
Type: onLoad
Here’s an example onLoad client script:
javascript
function onLoad() {
// Define a GlideAjax object and pass the name of the script include
var ga = new GlideAjax('MyScriptInclude');
// Specify the function to be called in the Script Include
ga.addParam('sysparm_name', 'myServerSideFunction');
// Make the asynchronous call to the server-side script
ga.getXMLAnswer(processResponse);
}
function processResponse(response) {
// Process the response from the server-side script
var serverMessage = response;
alert(serverMessage); // Display the server-side message in an alert
}
In this example:
MyScriptInclude is the name of the script include.
myServerSideFunction is the function within the script include you want to call.
The ga.addParam('sysparm_name', 'myServerSideFunction'); line is used to specify the function inside the script include.
ga.getXMLAnswer(processResponse); calls the server-side function and when the response is received, the processResponse function is called with the response.
This approach ensures that you can perform server-side logic (akin to using a Business Rule) from a client script using GlideAjax and Script Includes.
To achieve the desired functionality where the field on the alm_hardware table is populated on the form load, you can use a combination of an onLoad client script and a GlideAjax call to a script include that mimics the business rule logic. This ensures that the field is populated correctly as soon as the form loads.
Let's break it down step-by-step:
Step 1: Create a Script Include
First, you'll encapsulate the business rule logic in a Script Include. This Script Include will be called by the client script during the form load.
Navigate to System Definition > Script Includes.
Click on New.
Define the name, for example, AlmHardwareScriptInclude.
Ensure the "Client Callable" checkbox is selected.
Implement the required business logic within the Script Include.
Example:
javascript
var AlmHardwareScriptInclude = Class.create();
AlmHardwareScriptInclude.prototype = {
initialize: function() {},
getBuuHead: function(almHardwareSysId) {
var result = '';
var almHardware = new GlideRecord('alm_hardware');
if (almHardware.get(almHardwareSysId)) {
var department = new GlideRecord('cmn_department');
if (department.get(almHardware.department)) {
var businessUnit = new GlideRecord('business_unit');
if (businessUnit.get(department.business_unit)) {
result = businessUnit.bu_head;
}
}
}
return result;
},
type: 'AlmHardwareScriptInclude'
};
Step 2: Create an onLoad Client Script
Next, you’ll create an onLoad client script to call the Script Include and populate the reference field in the form.
Navigate to the appropriate table or form where you want to add the client script (in this case, the alm_hardware table).
Create a new Client Script with:
Type: onLoad
Example:
javascript
function onLoad() {
// Only proceed if it's a new record to populate the field
if (g_form.isNewRecord()) {
var ga = new GlideAjax('AlmHardwareScriptInclude');
// Call the getBuuHead function in the Script Include, passing the current record's sys_id
ga.addParam('sysparm_name', 'getBuuHead');
ga.addParam('sysparm_almHardwareSysId', g_form.getUniqueValue());
// Make the asynchronous call to the server-side script and process the response
ga.getXMLAnswer(function(response) {
var buuHead = response;
if (buuHead) {
g_form.setValue('u_buu_head', buuHead);
}
});
}
}
Explanation:
Script Include: Contains the business logic that fetches the business unit head for a given alm_hardware record.
Client Script: Calls the Script Include on form load. It checks the current record's sys_id, fetches the corresponding business unit head, and populates the u_buu_head field.
By following these steps, you ensure that your u_buu_head field on the alm_hardware form is populated with the appropriate value whenever the form is loaded, mirroring the business rule logic that you've already implemented. The client script leverages the Script Include to execute server-side logic, ensuring data consistency and integrity.
If you want to ensure that the u_buu_head field is populated for existing records whenever you open the form, you will need to modify the client script to run on the onLoad event for all records, not just new ones. You’ll retain the same approach of using GlideAjax to call a Script Include, but you'll adjust it slightly to remove the condition that checks for new records.
Here are the steps:
Step 1: Create a Script Include
Ensure you have a Script Include that can fetch the necessary data. If you have already created it, you can use the same one:
javascript
var AlmHardwareScriptInclude = Class.create();
AlmHardwareScriptInclude.prototype = {
initialize: function() {},
getBuuHead: function(almHardwareSysId) {
var result = '';
var almHardware = new GlideRecord('alm_hardware');
if (almHardware.get(almHardwareSysId)) {
var department = new GlideRecord('cmn_department');
if (department.get(almHardware.department)) {
var businessUnit = new GlideRecord('business_unit');
if (businessUnit.get(department.business_unit)) {
result = businessUnit.bu_head;
}
}
}
return result;
},
type: 'AlmHardwareScriptInclude'
};
Step 2: Create/Modify the onLoad Client Script
Next, you need to create an onLoad client script that will run every time the form is loaded, regardless of whether the record is new or existing.
Navigate to the appropriate table or form where you want to add the client script (in this case, the alm_hardware table).
Create a new Client Script with:
Type: onLoad
Make sure to set the right conditions if needed (e.g., form state, user roles)
Example:
javascript
function onLoad() {
// Define a GlideAjax object and pass the name of the Script Include
var ga = new GlideAjax('AlmHardwareScriptInclude');
// Call the getBuuHead function in the Script Include, passing the current record's sys_id
ga.addParam('sysparm_name', 'getBuuHead');
ga.addParam('sysparm_almHardwareSysId', g_form.getUniqueValue());
// Make the asynchronous call to the server-side script and process the response
ga.getXMLAnswer(function(response) {
var buuHead = response;
if (buuHead) {
g_form.setValue('u_buu_head', buuHead);
}
});
}
Explanation:
Script Include: AlmHardwareScriptInclude with a function getBuuHead that fetches the business unit head for a given alm_hardware record.
Client Script:
It initializes a GlideAjax object to call the Script Include.
It passes the sys_id of the current record to the Script Include.
It sets the u_buu_head field with the returned value from the server-side, when the form loads, for any record, whether new or existing.
By following these steps, whenever you open an existing alm_hardware form, the u_buu_head field will be populated dynamically based on the business logic. The client script ensures that the server-side logic is executed every time the form is loaded, keeping the data up to date.
You're right. To make a Script Include callable from the client, you must set the clientCallable property to true. This allows it to be invoked from a client script using GlideAjax. I'll update the Script Include to include this property and then demonstrate how to call it correctly from a client script.
Step 1: Create or Update the Script Include
Ensure the Script Include is marked as client-callable and implement the required business logic within it:
Navigate to System Definition > Script Includes.
Click on New if you haven't created one already, or open your existing Script Include.
Define the script include as follows:
javascript
var AlmHardwareScriptInclude = Class.create();
AlmHardwareScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getBuuHead: function() {
var almHardwareSysId = this.getParameter('sysparm_almHardwareSysId');
var result = '';
var almHardware = new GlideRecord('alm_hardware');
if (almHardware.get(almHardwareSysId)) {
var department = new GlideRecord('cmn_department');
if (department.get(almHardware.department)) {
var businessUnit = new GlideRecord('business_unit');
if (businessUnit.get(department.business_unit)) {
result = businessUnit.bu_head;
}
}
}
return result; // This value is returned to the client script
},
type: 'AlmHardwareScriptInclude'
});
Step 2: Create or Update the onLoad Client Script
Now, create or update the onLoad client script to call this Script Include using GlideAjax.
Navigate to the appropriate table or form where you want to add the client script (in this case, the alm_hardware table).
Create a new Client Script with:
Type: onLoad
Script:
javascript
function onLoad() {
// Ensure this runs for existing records by checking it's not new
if (!g_form.isNewRecord()) {
var ga = new GlideAjax('AlmHardwareScriptInclude');
// Call the getBuuHead function in the Script Include, passing the current record's sys_id
ga.addParam('sysparm_name', 'getBuuHead');
ga.addParam('sysparm_almHardwareSysId', g_form.getUniqueValue());
// Make the asynchronous call to the server-side script and process the response
ga.getXMLAnswer(function(response) {
var buuHead = response;
if (buuHead) {
g_form.setValue('u_buu_head', buuHead);
}
});
}
}
Explanation:
Script Include: The clientCallable property is implicitly set by extending AbstractAjaxProcessor. This marks the Script Include as callable from the client-side code. The getBuuHead function fetches the business unit head for a given alm_hardware record.
Client Script:
Initializes a GlideAjax object to call the Script Include.
Passes the sys_id of the current alm_hardware record to the Script Include.
Sets the u_buu_head field with the value returned from the Script Include.
Ensures it runs for existing records by checking if the form is not new using g_form.isNewRecord().
By following these steps, the u_buu_head field on the alm_hardware form will be populated whenever you open an existing record, ensuring the field is updated based on the logic defined in the Script Include.
When dealing with field changes from the list view in ServiceNow, client scripts won't trigger because they are designed to work on the form view. To handle field updates from the list view, you'll need to use business rules or background scripts (or a combination of both) to achieve this.
Here is how you can achieve this:
Option 1: Business Rule
You can write a Business Rule that triggers on the update of the assigned_to or state fields.
Navigate to System Definition > Business Rules.
Click on New to create a new business rule.
Configure the business rule with the following settings:
When to run:
Table: alm_hardware
When: before
Update: Checked
Conditions:
Add a condition to check if assigned_to or state changes.
You can add logic to check if the value has changed.
Script:
javascript
if (current.assigned_to.changes() || current.state.changes()) {
current.dual_asset = ''; // Clear the value of the dual_asset field
}
Explanation:
This business rule will trigger before the record is updated.
The script checks if the assigned_to or state fields have changed using the .changes() method.
If either field has changed, it clears the value of the dual_asset field.
Option 2: Combination of Client Script and Business Rule
If you want to handle this both from the form view (for immediate user feedback) and list view, you can combine a client script and a business rule.
1. Client Script
This will handle changes when fields are updated from the form view.
Navigate to the alm_hardware table.
Create a new Client Script with:
Type: onChange
Field Name: assigned_to (or state, repeat the script for both fields)
Script:
javascript
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === oldValue) {
return;
}
g_form.clearValue('dual_asset');
}
2. Business Rule
This will handle changes when fields are updated from the list view.
Navigate to System Definition > Business Rules.
Create a new Business Rule with:
When to run:
Table: alm_hardware
When: before
Update: Checked
Advanced: Checked
Script:
javascript
if (current.assigned_to.changes() || current.state.changes()) {
current.dual_asset = ''; // Clear the value of the dual_asset field
}
Conclusion:
By implementing the business rule, you ensure that updates from the list view are also handled correctly. Client scripts can provide immediate feedback in the form view, while the business rule ensures data consistency even when updates are made through the list view or other scripts and integrations.
This approach covers both scenarios and ensures that the dual_asset field is cleared whenever the assigned_to or state field changes, regardless of whether the update is made from the list view or the form view.