I have created a field %Complete where it should allow only numbers from 0 to 100

Shaik Najma
Tera Contributor

I have created a field %Complete where it should allow only numbers from 0 to 100 what can be the simple and best possible ways to do this ?
Can we do field validation with in dictionry?

4 REPLIES 4

Dr Atul G- LNG
Tera Patron

You can use a UI Policy and apply a relative operator.

*************************************************************************************************************
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/dratulgrover [ Connect for 1-1 Session]

****************************************************************************************************************

Ankur Bawiskar
Tera Patron

@Shaik Najma 

you can make the field as Type=Integer and then use onChange client script on that field and handle the value check + Business rule

Use onChange client script for Form validation

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

    var n = parseInt(newValue, 10);
    if (isNaN(n) || n < 0 || n > 100) {
        g_form.showFieldMsg('u_percent_complete', 'Enter a value between 0 and 100.', 'error');
        g_form.clearValue('u_percent_complete');
    }
}

You also need business rule in case somebody updates the field from list

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here

    if (!gs.nil(current.u_percent_complete)) {
        var n = parseInt(current.u_percent_complete, 10);
        if (isNaN(n) || n < 0 || n > 100) {
            gs.addErrorMessage('% Complete must be between 0 and 100.');
            current.setAbortAction(true);
        }
    }

})(current, previous);

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Tanushree Maiti
Tera Patron

Hi @Shaik Najma 

 

Refer : Percent complete field type 

Implement an onChange Client script to validate it.

  • Table: Select the table of your form.
  • Type: onChange
  • Field Name: Select your Percent Complete field (e.g., percent_complete)
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

       var enteredValue = parseFloat(newValue);    if (enteredValue > 100) {
        g_form.setValue('percent_complete', 100); // Resets to 100        g_form.showFieldMsg('percent_complete', 'Percent complete cannot exceed 100.', 'error', false);
    } else if (enteredValue < 0) {
        g_form.setValue('percent_complete', 0); // Resets to 0        g_form.showFieldMsg('percent_complete', 'Percent complete cannot be less than 0.', 'error', false);
    }
}

 

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

PoonkodiS
Giga Sage

Hi @Shaik Najma 

I used percent complete field type ,it allows only the numbers

PoonkodiS_0-1779194629278.png

and,you use the client script to check the range

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

if ( newValue < 0 || newValue >= 100) {
g_form.showFieldMsg('u_complete', 'Please enter a value between 0 and 100', 'error');
g_form.setValue('u_complete', ''); 
}
}

 

hope it helps