- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2024 02:06 AM
Hello Team,
I have requirement on catalog item, two variables we have
1. project name
2.project code
> If project name >15 character then project code should be mandatory.
> Project code should start from alphabet
> Project code should contain only alphabet, number, hyphen(-), underscore(_) and whitespace.
Please help with script
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2024 02:52 AM
Hi @Vinod S Patil ,
You can have a onChange client script on change of Project Name as below
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var projectName = g_form.getValue('project_name');
//Type appropriate comment here, and begin script below
alert(projectName.length)
if (projectName.length > 15) {
alert('Project code is mandatory when project name is more than 15 characters.');
g_form.setMandatory('project_code',true);
return;
}
}
and then on change of Project code as
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Project code should start from alphabet
// Project code should contain only alphabet, number, hyphen(-), underscore(_) and whitespace.
var projectCode = g_form.getValue('project_code');
var firstChar = projectCode.charAt(0);
if (!firstChar.match(/[a-zA-Z]/)) {
alert('Project code should start with an alphabet.');
g_form.clearValue('project_code');
return;
}
if (!projectCode.match(/^[a-zA-Z0-9_-\s]+$/)) {
alert('Project code should contain only alphabet, number, hyphen(-), underscore(_), and whitespace.');
g_form.clearValue('project_code');
return;
}
}
Test is out once and refine as required.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2024 02:52 AM
Hi @Vinod S Patil ,
You can have a onChange client script on change of Project Name as below
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var projectName = g_form.getValue('project_name');
//Type appropriate comment here, and begin script below
alert(projectName.length)
if (projectName.length > 15) {
alert('Project code is mandatory when project name is more than 15 characters.');
g_form.setMandatory('project_code',true);
return;
}
}
and then on change of Project code as
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Project code should start from alphabet
// Project code should contain only alphabet, number, hyphen(-), underscore(_) and whitespace.
var projectCode = g_form.getValue('project_code');
var firstChar = projectCode.charAt(0);
if (!firstChar.match(/[a-zA-Z]/)) {
alert('Project code should start with an alphabet.');
g_form.clearValue('project_code');
return;
}
if (!projectCode.match(/^[a-zA-Z0-9_-\s]+$/)) {
alert('Project code should contain only alphabet, number, hyphen(-), underscore(_), and whitespace.');
g_form.clearValue('project_code');
return;
}
}
Test is out once and refine as required.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2024 03:18 AM
Sure, you can achieve this by using a client script in ServiceNow. Here's a sample script that you can use: javascript function onChange(control, oldValue, newValue, isLoading) { if (isLoading || newValue == '') { return; } var projectName = g_form.getValue('project_name'); var projectCode = g_form.getValue('project_code'); if (projectName.length > 15 && (projectCode == '' || !/^[a-zA-Z][a-zA-Z0-9-_ ]*$/.test(projectCode))) { g_form.addErrorMessage('Project code is mandatory and should start with an alphabet. It should contain only alphabets, numbers, hyphen(-), underscore(_) and whitespace.'); g_form.clearValue('project_code'); } } Here's how it works: - This script is a client script that runs on change of the project name or project code. - It first checks if the project name is more than 15 characters. - If it is, it then checks if the project code is empty or does not match the specified pattern. - The pattern used is a regular expression that checks if the project code starts with an alphabet and contains only alphabets, numbers, hyphen(-), underscore(_) and whitespace. - If the project code does not meet these conditions, it shows an error message and clears the project code field. To implement this: 1. Navigate to Service Catalog > Catalog Definitions > Maintain Items. 2. Open the catalog item you want to add this script to. 3. Go to the Client Scripts related list and click on New. 4. Fill in the form with the following details: - Type: onChange - Control: project_name or project_code - Script: Copy and paste the above script. 5. Click on Submit. Remember to replace 'project_name' and 'project_code' with the actual names of your variables.