Script to extract a number from the mail subject line

nehasr1288
Tera Expert

Hi,

I need to write a script to extract a number from the subject of the mail.user sends a mail to dev****.servicenow.com.

From System Policy   > Email > Inbound Actions I have configured:

Capture.png

and in actions tab I have mentioned the actions when a mail is received:

Capture.png

Now I need to write a script that may take a number from the mail subject and insert it in a custom field.

1 ACCEPTED SOLUTION

The code you added in the previous screenshot is wrong.



Can you please replace it with this



var rsubject=email.subject;


var keyword="Ticket#: ";


var len = keyword.length;
var key = rsubject.indexOf(keyword);   //Assuming this text would never change


if(key > 0){


var number = rsubject.substring(key+len, 7);


gs.log(number, "TEST");   //Try logging number


current.u_ast_id = number;
current.update();


}



Thanks


Please Hit like, Helpful or Correct depending on the impact of the response


View solution in original post

20 REPLIES 20

Also I believe you are trying to access Ticket#: I616584 from email subject, then the code should be



var rsubject=email.subject;


var keyword="Ticket#: ";


var len = keyword.length;
var key = rsubject.indexOf(keyword);   //Assuming this text would never change


if(key > 0){


var number = rsubject.substring(key+len, 7);


gs.log(number, "TEST");   //Try logging number


current.u_ast_id = number;
current.update();


}





Thanks


Please Hit like, Helpful or Correct depending on the impact of the response


Hi,



I tried hard coding values as well but it is not working.



What is the "TEST"? if I understand correctly it is the incident number?


Anyhow just one question why I am not getting "help" commands when I am using CTRL+SPACE while writing script?


is it like that way only?


TEST is a way to search on logs for the number, You can search on logs Source=TEST. Did you get this log with any value?



Have you checked if your inbound action is triggered from mailbox received logs? Can you please share a screenshot of all tabs in your inbound action.



What I think is API is not setup to work as autocomplete in inbound actions.



Thanks


Please Hit like, Helpful or Correct depending on the impact of the response


step1.PNG



step2.PNG