In servicenow, in catalog item we allocate asset to the users, my requirement is that in catalog item if user select the reason is vendor then the record created in alm_hardware table owned by email id is populate as the requested for managers email id.
Assistant
To achieve this requirement in ServiceNow, you'll need to utilize catalog client scripts and server-side scripts (like Business Rules or Script Includes) in your catalog item to ensure the correct fields are populated based on the user's selection.
Here is a step-by-step guide on how to set this up:
Step 1: Update Catalog Item with a Client Script
Open your catalog item: Go to your Service Catalog and open the specific catalog item where assets are allocated.
Create a new Client Script:
Navigate to the catalog item's related lists and find the "Catalog Client Scripts" related list.
Create a new Catalog Client Script.
Configure the Client Script:
Name: Give it a descriptive name, like "Set Manager's Email ID for Vendor Reason".
UI Type: Select "All".
Type: Select "onChange".
Field Name: Select the field that holds the reason (assuming the field name is reason).
Script:
javascript
// Get the value of the 'reason' field
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
// Avoid processing during form load or template creation:
if (isLoading || isTemplate) {
return;
}
// Check if the new value of 'reason' is 'vendor'
if (newValue === 'vendor') {
// Get the user's manager's email
var userId = g_user.userID;
var gr = new GlideRecord('sys_user');
if (gr.get(userId)) {
var managerGr = new GlideRecord('sys_user');
if (managerGr.get(gr.manager)) {
// Set the manager's email in a hidden variable to be used on the server-side
g_form.setValue('manager_email', managerGr.email);
}
}
} else {
// Clear the manager's email if the reason is not 'vendor'
g_form.setValue('manager_email', '');
}
}
Save the Client Script.
Step 2: Create a Hidden Variable
Create a hidden variable in your catalog item to store the manager's email. The client script will populate it.
Add a new variable:
Type: Single Line Text or Email
Name: manager_email
Question: Hidden (or you can set the Write roles to nobody to keep it hidden)
Step 3: Update the Server Script
Use a Business Rule to handle the creation of the alm_hardware record and to set the owned_by field to the manager's email.
Go to Business Rules:
Navigate to System Definition -> Business Rules.
Create a new Business Rule.
Configure the Business Rule:
Name: Give it a meaningful name like “Set Asset Owner to Manager's Email for Vendor Reason”.
Table: Select the table where the catalog item request details are stored, typically sc_req_item.
Advanced: Check this option to enable advanced scripting.
Conditions: You can specify that this BR should run only if the reason is ‘vendor’.
Script:
javascript
(function executeRule(current, previous /*null when async*/) {
if (current.variables.reason == 'vendor' && current.variables.manager_email) {
var assetGR = new GlideRecord('alm_hardware');
assetGR.initialize();
assetGR.owned_by = current.variables.manager_email;
// Populate other necessary fields here
assetGR.insert();
}
})(current, previous);
Save and activate the Business Rule.
Final Steps
Test your implementation: Try with different scenarios to make sure if the manager's email is correctly assigned when the reason is "vendor".
Debugging: Utilize logs or debug options in ServiceNow if the expected behavior isn't met.
By following these steps, you ensure that when a user selects "vendor" as the reason, the newly created record in the alm_hardware table will have the owned_by field set to the manager of the user who made the request.