Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

normalization in servicenow

keerthi35
Tera Contributor

how do you normalize the department name in the user record?

2 REPLIES 2

Chaitali_Vale
Mega Sage

Hello @keerthi35 ,

 

To normalize the department name in a user record in ServiceNow, you can use GlideScript, which is based on JavaScript. Below is an example script with detailed explanations:
javascript
// Define the function to normalize department names
function normalizeDepartment(user) {
// Access the department field from the user record
var department = user.department.getDisplayValue();

// Check if the department field is not empty
if (department) {
// Convert the department name to lowercase
department = department.toLowerCase();

// Remove leading and trailing spaces
department = department.trim();

// Replace multiple spaces with a single space
department = department.replace(/\s+/g, ' ');

// Set the normalized department name back in the user record
user.department = department;
}
}

// Example of how to use this function
var user = new GlideUser(); // Assuming you have access to the user object
normalizeDepartment(user); // Call the function with the user object

// Save the user record if needed
user.update();


Explanation of code:

1. function normalizeDepartment(user) { ... }: This defines a function named normalizeDepartment that takes a user object as an argument.

2. var department = user.department.getDisplayValue();: This retrieves the department name from the user record. getDisplayValue() is used to get the display value of a field.

3. if (department) { ... }: This checks if the department field is not empty.

4. Inside the if block, we perform the following normalization steps:
- department = department.toLowerCase();: Converts the department name to lowercase.

- department = department.trim();: Removes leading and trailing spaces.

- department = department.replace(/\s+/g, ' ');: Replaces multiple spaces with a single space. This regular expression replaces one or more consecutive spaces with a single space.

- user.department = department;: Sets the normalized department name back in the user record.

5. var user = new GlideUser();: This assumes you have access to the user object. If not, you need to obtain it through GlideRecord or some other means.

6. normalizeDepartment(user);: Calls the normalizeDepartment function with the user object.

7. user.update();: If needed, this saves the user record after making the changes.

 

Please mark my answer correct or helpful, if applicable.

Thanks & regards,

Chaitali Vale

which table are used?