The CreatorCon Call for Content is officially open! Get started here.

scripts to check variable is empty or not

TMKAM
Tera Contributor

I have create 2 variable as per below

variable A = multiline text

variable B - attachment

 

the above variable is not set as mandatory field and I would like to create a on submit scripts to check base on the below scenario

 

Scenario 1

Variable A = empty

Variable B = empty meaning no attachment

the script should alert with message when requester press order now button

 

Scenario 2

Variable A = not empty

Variable B = empty meaning no attachment

the script is not alert with message and requester can press order now button

 

Scenario 3

Variable A = empty

Variable B = not empty meaning got attachment inside

the script is not alert with message and requester can press order now button

 

thank you

15 REPLIES 15

@Ashwini Jadhao 

the scripts does not work as expected and it should follow below scenario

 

Variable A = not fill

Variable B = no attachment

the script should alert with message when requester press order now button

 

Scenario 2

Variable A = fill

Variable B = no attachment

the script is not alert with message and requester can press order now button

 

Scenario 3

Variable A = not fill

Variable B = with attachment

the script is not alert with message and requester can press order now button

 

thank you

@TMKAM

Try below script

function onSubmit() {
    //Type appropriate comment here, and begin script below
    var text = g_form.getValue("test_text");
    var attachment = g_form.getValue("attachment");
    if (text == '' && attachment == '') {
        alert("please add values to variables");
        return false;
    } else if (text = '' && attachment == '') {
        return true;
    } else if (text == '' && attachment != '') {
        return true;
    } else {
        return true;
    }

}

Ashwini Jadhao
Giga Guru

Hi,

Please try this script

function onSubmit() {
    //Type appropriate comment here, and begin script below
    var text = g_form.getValue("test_text");
    var attachment = g_form.getValue("attachment");
    if (text == '' && attachment == '') {
        alert("please add values to variables");
        return false;
    } else if (text != '' && attachment == '') {
        alert("please attach document");
        return false;
    } else if (text == '' && attachment != '') {
        alert("please fill up Text");
        return false;
    }else{
       return true;
   }
}

TeamSnow_1-1687322045732.png

 

Please mark my answer to correct or helpful if it works for you.

Thank you!

@Ashwini Jadhao 

thank you for the scripts but my scenario should works as per below.

 

Variable A = not fill

Variable B = no attachment

the script should alert with message when requester press order now button

 

Scenario 2

Variable A = fill

Variable B = no attachment

the script is not alert with message and requester can press order now button

 

Scenario 3

Variable A = not fill

Variable B = with attachment

the script is not alert with message and requester can press order now button

 

thank you

Amit Gujarathi
Giga Sage
Giga Sage

HI @TMKAM ,
I trust you are doing great.

  1. If Variable A is empty and Variable B is empty:

    • Display an alert message to the requester when they press the "Order Now" button.
  2. If Variable A is not empty and Variable B is empty:

    • Do not display an alert message.
    • Allow the requester to press the "Order Now" button.
  3. If Variable A is empty and Variable B is not empty:

    • Do not display an alert message.
    • Allow the requester to press the "Order Now" button.

To implement this solution, you can create an on-submit script in ServiceNow. Here's an example of how the script might look like in Javascript:

(function() {
  var variableA = g_form.getValue('variableA');
  var variableB = g_form.getValue('variableB');

  if (!variableA && !variableB) {
    alert('Please provide a value for Variable A or attach a file.');
    return false; // Prevent the form submission
  }

  return true; // Allow the form submission
})();

 


Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi