Restrict user to fill 2 Decimal value in the Field on the Incident Form.

Insider
Giga Guru

Hello Everyone,

 

I need help on 2 issues.

 

1: Restrict user to fill only 2 Decimal values on a field on the INC form. Ex: 12.34

I have used a decimal Type field, but this works only when i save the record, but i want the system to restrict the user before saving the form itself 

I did this on a variable using the On Change client script for portal, but not able to achieve this on Field for a table. Please help

 

2: When a child case is opened, make the opened date of the child case to be the current date.

When a child case is closed, make the closed date of the child case to be the current date.

** Child case is being creating using a UI Action.

**

1 ACCEPTED SOLUTION

Hi @Insider 

The JavaScript RangeError: Maximum call stack size exceeded happens when a function keeps calling itself without any condition to stop, and eventually, the program runs out of space to keep track of these repeated calls. It's a sign that your program needs a way to stop the function from calling itself endlessly

try the following extended version in the first part

if (isLoading || newValue === '' || newValue == oldValue) {
return;
}

View solution in original post

7 REPLIES 7

Amitoj Wadhera
Kilo Sage

Hi @Insider ,

 

Issue 1: Restrict User to Fill Only 2 Decimal Values on a Field

To restrict the user to input only 2 decimal values on a field before saving the form, you can use a Client Script in ServiceNow. The Client Script will validate the input when the user tries to leave the field (onChange) or before the form is submitted (onSubmit).

Steps to Implement OnChange Client Script:

  1. Navigate to System Definition > Client Scripts.
  2. Click on New to create a new client script.
  3. Configure the client script as follows:

Name: Validate Decimal Precision
Table: Incident
Type: onChange
Field name: [Your Decimal Field Name]
UI Type: All (or select as per your requirement)

Script:

 

(function executeRule(current, previous /*null when async*/) {
    var fieldName = g_form.getValue('u_decimal_field'); // Replace with your field name

    function onChange(control, oldValue, newValue, isLoading) {
        if (isLoading || newValue === '') {
            return;
        }

        var decimalPattern = /^\d+(\.\d{1,2})?$/;
        if (!decimalPattern.test(newValue)) {
            g_form.showFieldMsg(fieldName, 'Please enter a value with up to 2 decimal places.', 'error');
            g_form.clearValue(fieldName); // Clear the field to force user to enter the correct value
        } else {
            g_form.hideFieldMsg(fieldName); // Hide the message if value is correct
        }
    }
})(current, previous);

 

 

Steps to Implement OnSubmit Client Script:

  1. Navigate to System Definition > Client Scripts.
  2. Click on New to create a new client script.
  3. Configure the client script as follows:

Name: Validate Decimal Precision
Table: Incident
Type: onSubmit
UI Type: All (or select as per your requirement)

Script:

 

(function executeRule(current, previous /*null when async*/) {
    function onSubmit() {
        var fieldName = 'u_decimal_field'; // Replace with your field name
        var fieldValue = g_form.getValue(fieldName);

        var decimalPattern = /^\d+(\.\d{1,2})?$/;
        if (!decimalPattern.test(fieldValue)) {
            g_form.showFieldMsg(fieldName, 'Please enter a value with up to 2 decimal places.', 'error');
            return false; // Prevent form submission
        }
        return true; // Allow form submission
    }
})(current, previous);

 

 

Issue 2: Setting Opened and Closed Dates for Child Cases

To set the opened_date to the current date when a child case is opened and closed_date to the current date when a child case is closed, you can use Business Rules in ServiceNow.

Business Rule for Setting Opened Date:

  1. Navigate to System Definition > Business Rules.
  2. Click on New to create a new business rule.
  3. Configure the business rule as follows:

Name: Set Opened Date for Child Case
Table: [Your Child Case Table]
Advanced: Checked
When: before
Insert: Checked

Script:

 

(function executeRule(current, previous /*null when async*/) {
    if (!current.opened_at) {
        current.opened_at = new GlideDateTime();
    }
})(current, previous);

 

 

Business Rule for Setting Closed Date:

  1. Navigate to System Definition > Business Rules.
  2. Click on New to create a new business rule.
  3. Configure the business rule as follows:

Name: Set Closed Date for Child Case
Table: [Your Child Case Table]
Advanced: Checked
When: before
Update: Checked

Filter Conditions:

  • Add a condition to check if the state is transitioning to a closed state.

Script:

 

(function executeRule(current, previous /*null when async*/) {
    var closedStates = ['closed', 'resolved']; // Add your closed states here
    if (closedStates.indexOf(current.state.toString()) !== -1 && !current.closed_at) {
        current.closed_at = new GlideDateTime();
    }
})(current, previous);

 

 

UI Action to Create Child Case:

When you create a child case using a UI Action, you can set the opened_date and closed_date directly in the script.

Example UI Action Script:

 

(function executeAction(current, action) {
    var childCase = new GlideRecord('child_case_table'); // Replace with your child case table name
    childCase.initialize();
    childCase.parent = current.sys_id;
    childCase.opened_at = new GlideDateTime(); // Set opened date to current date
    // Add other field values as necessary
    childCase.insert();
})(current, action);

 

 

This approach ensures that the opened_date and closed_date fields are correctly set when a child case is created or closed.

 

If you find my response helpful, please consider marking it as the 'Accepted Solution' and giving it a 'Helpful' rating. Your feedback not only supports the community but also encourages me to continue providing valuable assistance.

 

Thanks,

Amitoj Wadhera

Hello,

 

Thanks a lot for the detailed solution

I have issues here.

1: I want to round off the entered value to 2 Decimal.

Instead in your code provided I see your clearing the value.

 

Hi @Insider ,

 

Here is the updated code:

 



    function onChange(control, oldValue, newValue, isLoading) {
        if (isLoading || newValue === '') {
            return;
        }
            var fieldName = g_form.getValue('u_decimal_field'); // Replace with your field name
             
        var decimalPattern = /^\d+(\.\d{1,2})?$/;
        if (!decimalPattern.test(newValue)) {
            g_form.showFieldMsg(fieldName, 'Please enter a value with up to 2 decimal places.', 'error');
            // Instead of clearing the field, round off the entered value to 2 decimal places
            g_form.setValue(fieldName, parseFloat(newValue).toFixed(2));
        } else {
            g_form.hideFieldMsg(fieldName); // Hide the message if value is correct
        }
    }

 

 

If you find my response helpful, please consider marking it as the 'Accepted Solution' and giving it a 'Helpful' rating. Your feedback not only supports the community but also encourages me to continue providing valuable assistance.

 

Thanks,

Amitoj Wadhera

Rajesh Chopade1
Mega Sage

HI @Insider 

 

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	
	if (isLoading || newValue === '') {
		return;
	}
	var reg = /^[0-9]*\.[0-9]{2}$/;
	var ans = g_form.getValue('field_name');
	
	if(!reg.test(ans))
	{
		alert('Please enter a valid value.');
		g_form.clearValue('field_name');
	}
}

 

 

 

do you want them to restrict to 2 decimals then use above

OR

allow then to give as many decimal values and round off from script; this will round off to decimals places

 

 

var roundValue = parseFloat(newValue).toFixed(2);

 

 

 

2) update you UI action with 

childCase.opened_at = new GlideDateTime(); // Set opened date to current date

Thank you