I have the requirement if current logged in user's calender integration is outlook in sys user table then in the catalog item requested for field auto populate the name of that user while raising the request
Assistant
User
Not working
Assistant
To achieve the auto-population of the "Requested For" field with the name of the logged-in user when the user's calendar integration is Outlook, you can use a combination of client scripts and GlideRecord queries in ServiceNow. Here is a step-by-step guide:
Step 1: Determine User Calendar Integration
First, ensure that the "sys_user" table has a field indicating the calendar integration used by the user (e.g., a custom field like calendar_integration). This field should specify whether the user is using Outlook for their calendar integration.
Step 2: Create a Client Script
Create an onLoad client script for the catalog item to fetch the current user's details and set the "Requested For" field appropriately.
Navigate to Client Scripts:
Go to the catalog item for which you want to auto-populate the "Requested For" field.
Under "Catalog Item" → "Client Scripts", click on "New" to create a new client script.
Configure the Client Script:
Name: Give your script a meaningful name, like "Auto Populate Requested For".
Type: Choose "onLoad".
Table: Select the table used for the catalog item (e.g., sc_req_item).
Script Content:
Here’s an example script that checks the calendar integration and sets the "Requested For" field:
javascript
function onLoad() {
// Get the currently logged-in user
var userID = g_user.userID;
// Query the sys_user table to check their calendar integration
var userGR = new GlideRecord('sys_user');
if (userGR.get(userID)) {
var calendarIntegration = userGR.getValue('calendar_integration');
// Check if the user's calendar integration is Outlook
if (calendarIntegration === 'Outlook') {
// Set the 'Requested For' field to the current user's name
g_form.setValue('requested_for', userID);
}
}
}
Step 3: Test the Implementation
After implementing the client script, navigate to the catalog item, impersonate a user with Outlook calendar integration, and confirm that the "Requested For" field is automatically populated with the user's name when creating a new request.
Additional Considerations
Ensure the calendar_integration custom field exists in the sys_user table and is correctly populated.
Handle any user interface policies that might interfere with the client script setting the field value.
Verify the script permissions and availability across different user roles and catalog items if needed.
By following these steps, you'll implement the required functionality while raising a request based on the user's calendar integration setting.
Let's troubleshoot and refine the approach to ensure the script functions correctly. Here are some potential issues and solutions:
Verify Field and Table Names:
Double-check that calendar_integration is indeed the correct field name in the sys_user table and that it stores the integration type as 'Outlook'.
Check User Permissions:
Ensure that the script has permission to read the sys_user table and the calendar_integration field. You might need to ensure that read access is granted for the roles that will use this functionality.
Debugging:
Add logging to your client script to track its execution and identify any issues. Use gs.info or gs.error for server-side logs and console.log for client-side debugging:
javascript
function onLoad() {
console.log('Client script loaded');
// Get the currently logged-in user
var userID = g_user.userID;
console.log('User ID:', userID);
// Query the sys_user table server-side
var ga = new GlideAjax('CatalogRequestedFor');
ga.addParam('sysparm_name', 'getRequestedFor');
ga.addParam('sysparm_user_id', userID);
ga.getXMLAnswer(function(response) {
var calendarIntegration = response;
console.log('Calendar Integration:', calendarIntegration);
// Check if the user's calendar integration is Outlook
if (calendarIntegration === 'Outlook') {
// Set the 'Requested For' field to the current user's name
g_form.setValue('requested_for', userID);
}
});
}
Server-Side Script Include:
Since direct GlideRecord queries aren't possible in a client script, use a Script Include to fetch the data:
Create a Script Include:
Go to "System Definition" > "Script Includes" and create a new Script Include named "CatalogRequestedFor".
javascript
var CatalogRequestedFor = Class.create();
CatalogRequestedFor.prototype = {
initialize: function() {},
getRequestedFor: function(userID) {
var userGR = new GlideRecord('sys_user');
if (userGR.get(userID)) {
return userGR.getValue('calendar_integration');
}
return '';
},
type: 'CatalogRequestedFor'
};
Adjust the Client Script:
Use GlideAjax in the client script to call the script include method as shown above in Step 3.
Double-Check the Requested For Field:
Make sure the field you're trying to set is correctly referenced (verify it matches the exact field name in the form).
Impersonation for Testing:
Impersonate a user who has Outlook as their calendar integration to ensure the logic is triggered correctly.
Browser Console Errors:
Open the browser's developer console to check for any client-side errors when the form loads. This can provide insights into any JavaScript issues.
By checking these aspects, you should be able to identify and fix the issues preventing the client script from working as expected. Make sure to test thoroughly after implementing these steps.