Script Understanding

Hitesh Ramba
Tera Contributor

Can I get some exercise to work on scripting from the basics , To be successful in development...

 

Thank You!!

5 REPLIES 5

SAI VENKATESH
Tera Sage
Tera Sage

Hi @Hitesh Ramba  

 

The Index of is useful to search a substring within a string. and -1 in this context indicates that the substring "NEW" was not found in the subject of the email.

 

Thanks and Regards

Sai Venkatesh

Brian Lancaster
Tera Sage

email.subject.indexOf('NEW')!=-1 is like saying NEW existed in the subject. Index of return the location where it found the word in this case in the string. When you see ==-1 then that means the word was not found in the string.

Wind0004
Mega Guru

Hi Hitesh,

Breaking it down by each condition:

Condition 1 (email.from == 'exampe1@gmail.com'): Checks if the sender of the email is 'exampe1@gmail.com'.
Condition 2 (email.subject.indexOf('NEW') != -1): Checks if the subject of the email contains the substring 'NEW'.
Condition 3 (email.subject.indexOf('UPDATE') == -1): Checks if the subject of the email does not contain the substring 'UPDATE'.
Condition 4 (email.subject.indexOf('RESOLVED') == -1): Checks if the subject of the email does not contain the substring 'RESOLVED'.
Condition 5 (email.from != 'example1@gmail.com'): Checks if the sender of the email is not 'example1@gmail.com'.

Now to explain some of the methods used within the conditions:

The indexOf() method is used to find the position of a substring within a string. If the substring is not found, it returns -1. So, indexOf('NEW') != -1 checks if the string 'NEW' is present in the subject of the email.

The logical operators && (logical AND) and || (logical OR) are used to combine these conditions.

In summary, this code is  checking certain conditions related to the sender's email address and the subject of the email, and will then trigger the notification accordingly.

Also, there seems to be a typo in the code where 'exampe1@gmail.com' is spelled incorrectly and different to that of condition 5. 

Hope that helps. 

Regards,
Wind

SAI VENKATESH
Tera Sage
Tera Sage

Hi @Hitesh Ramba  

 

There is a course in now learning "Scripting in servicenow" and it will be very helpful and below is the Link

https://developer.servicenow.com/dev.do#!/learn/courses/washingtondc/app_store_learnv2_scripting_rom...

 

Thanks and Regards

Sai Venkatesh