I want to Restrict Form Submission if the It-fun_org field is Empty

JVINAY
Tera Contributor

Hi Team

 

I'm working on a requirement where I need to prevent the submission of a CIR  if the field it_fun_org is empty. The challenge is that this field is read-only on the form. This field is Currently populated based on Business application

Field Name : it_fun_org ( reference type : Business_app)

 

I want to restrict form when the it_fun_org field is empty.

 

Please help me how can i achieve this.

 

 

 

1 ACCEPTED SOLUTION

@JVINAY 

try this

function onSubmit() {
    var itFunOrg = g_form.getValue('it_fun_org');
    if (itFunOrg == '' || itFunOrg == undefined) {
        g_form.showFieldMsg('it_fun_org', 'IT Functional Org must be populated before submission.', 'error');
        return false; // Prevent submission
    }
    return true;
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@JVINAY 

it makes sense to stop submission if you expect agent to enter something in it.

But since it's readonly agent can't enter.

The code from where you are auto populating, you can do this

1) if auto population works and you are able to set value then set it and make it readonly

2) if it couldn't be populated, make it mandatory so that agent selects it and can't submit with that field as empty

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

AyushKumarM
Mega Guru

Hey @JVINAY , 
You can use onSubmit client script to abort submission of form.

example code =>

function onSubmit() {
var reqVal = g_form.getValue('it_fun_org');

if (!reqVal) {
return false;
}

return true; 



Please mark this answer helpful if it solves your issue.

HI @AyushKumarM 

 

I have written the code below, but it's not working. If the field is empty, the form still gets submitted.

function onSubmit() {
    var itFunOrg = g_form.getValue('it_fun_org');
    if (!itFunOrg) {
        g_form.showFieldMsg('it_fun_org', 'IT Functional Org must be populated before submission.', 'error');
        return false; // Prevent submission
    }
    return true;
}
 
Can please help me the Changes.

@JVINAY 

try this

function onSubmit() {
    var itFunOrg = g_form.getValue('it_fun_org');
    if (itFunOrg == '' || itFunOrg == undefined) {
        g_form.showFieldMsg('it_fun_org', 'IT Functional Org must be populated before submission.', 'error');
        return false; // Prevent submission
    }
    return true;
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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