How to define if condition with "contains"

phr
Tera Contributor

Hi All 

 

I have a requirement where i need to present the submission of the catalog form.

i did write the following code:

 

 

function onSubmit() {
var sn=g_form.getValue('service');
var cbox1 = g_form.getValue('did_you_read_kb_document_kb0007347_please_refer_description_for_the_link_to_kb');
var cbox2 = g_form.getValue('do_you_agree_with_the_automatic_expiration_policy_of_the_license_mentioned_in_kb0007347');
if(sn== '155936831b268a908d8ddc6fe54bcb8b')
{
if(cbox1 !== 'true' || cbox2 !== 'true')
{
alert('Select both checkboxes to submit the form.');
return false;
}
return true;
}

}

 

But its preventing only when there is "155936831b268a908d8ddc6fe54bcb8b" but the sys id "155936831b268a908d8ddc6fe54bcb8b" can be with any values.

Here the sys id belongs to "Copilot for M365" 

phr_0-1718619555271.png

 

But copilot for M365 can have many values along with it.

Example: for the values below i am still able to submit.

phr_1-1718619594341.png

How to achieve this?

 

5 ACCEPTED SOLUTIONS

Yashsvi
Kilo Sage

Hi @phr,

please check below link:

https://www.servicenow.com/community/developer-forum/if-condition/m-p/1635449

Thank you, please make helpful if you accept the solution.

View solution in original post

Simon Christens
Kilo Sage

Hi

For Service Portal script it can be done by

var sn=g_form.getDisplayValue('service'); //returns the names of the services and not sys_id
//We make all the names lowercase to be more accurate when validating
if(sn.toLowerCase().indexOf("some special name with only lowercase chars") > -1){

}

View solution in original post

SN_Learn
Kilo Patron
Kilo Patron

Hi @phr ,

 

Please replace the below and try:

if(sn, 'IN', '155936831b268a908d8ddc6fe54bcb8b');

 

Final Code will look like:

 

function onSubmit() {
var sn=g_form.getValue('service');
var cbox1 = g_form.getValue('did_you_read_kb_document_kb0007347_please_refer_description_for_the_link_to_kb');
var cbox2 = g_form.getValue('do_you_agree_with_the_automatic_expiration_policy_of_the_license_mentioned_in_kb0007347');
if(sn, 'IN', '155936831b268a908d8ddc6fe54bcb8b');
{
if(cbox1 !== 'true' || cbox2 !== 'true')
{
alert('Select both checkboxes to submit the form.');
return false;
}
return true;
}

}

 

 Please Mark My Response as Correct/Helpful based on Impact

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

View solution in original post

Try

var sn=g_form.getValue('service'); //returns the list if sys_ids in the field

//Check if the list contains the sys_id your are looking for
if(sn.indexOf("sys_id") > -1){

}

View solution in original post

Anubhav24
Mega Sage
Mega Sage

Hi @phr ,

 

What about includes() function in javascript. Can you try that to see if it helps you to achieve the desired result.

https://www.w3schools.com/jsref/jsref_includes.asp

 

Please review this line of code as well :

if(cbox1 !== 'true' || cbox2 !== 'true') : two times equal to is used instead of "!="

Please mark helpful/correct if my response helped you.

View solution in original post

7 REPLIES 7

Yashsvi
Kilo Sage

Hi @phr,

please check below link:

https://www.servicenow.com/community/developer-forum/if-condition/m-p/1635449

Thank you, please make helpful if you accept the solution.

phr
Tera Contributor

Hi @Yashsvi ,

 

Since i need to pass only the sys id , i am not making use of the Alphabets. 

Instead of "==" i need the "Contains" to be replaced.

How to achieve this for sys id?

 

Simon Christens
Kilo Sage

Hi

For Service Portal script it can be done by

var sn=g_form.getDisplayValue('service'); //returns the names of the services and not sys_id
//We make all the names lowercase to be more accurate when validating
if(sn.toLowerCase().indexOf("some special name with only lowercase chars") > -1){

}

Hi @Simon Christens ,

 

Since i need to pass only the sys id , i am not making use of the Alphabets. 

How to achieve this for sys id?

 

Thank you