How can i script Contains("string") or matching

alpha2nl
Kilo Explorer

Below script don't works, what is de function contains in SN

 

if(this.rec.u_service.containg("SAM") && this.rec.u_subtype == ""){

      allowed = true;

      }

Maby something like indexOf

Thanks!

6 REPLIES 6

josh_nerius
ServiceNow Employee
ServiceNow Employee

Is u_service a string? If so, you are correct: indexOf is the way to go. Example:



if(this.rec.u_service.indexOf("SAM") > -1 && this.rec.u_subtype == ""){


      allowed = true;


}


    Thank you Josh!



I have below code but i still get all time ------>   ("Subtype needs to be provided before leaving this phase.");



The code don't works well






if(this.rec.u_service.indexOf("SAM") > -1   && this.rec.u_subtype == ""){


      allowed = true;


      }


      else if(!this.rec.u_service.indexOf("SAM") > -1 && this.rec.u_subtype == ""){


  gs.addErrorMessage("Subtype needs to be provided before leaving this phase.");


                allowed = false;


      }


josh_nerius
ServiceNow Employee
ServiceNow Employee

Do you know what kind of field u_service is? Is it a reference to another table? If so, you may need to dot-walk to the "display value" field on the referenced table before you check indexOf. For troubleshooting purposes, try taking out the second conditional statement and just test with the first half until you know it's working.


alpha2nl
Kilo Explorer

Josh,



It works! it   is a reference to another table. dot-walk resolve it



Thank you very much!