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

Alikutty A
Tera Sage

Hi,



email.subject in the Inbound Action script returns you the email subject. You can perform additional validation on it as per your logic.



var key = email.subject.indexOf("search_keyword");


if(key > 0){


current.field_name = // extract content from email.subject using substring, key as index or other java script functions


}



Thanks


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


nehasr1288
Tera Expert

Hi ,



Thank you for the hint.


I applied this script:


var rsubject=email.subject;
var key = rsubject.indexOf("I");


if(key > 0)
{


current.u_ast_id = rsubject.substring(key,6);// extract content from email.subject using substring, key as index or other java script functions
current.insert();


}



But the script is not updating the "u_ast_id" field or rather not inserting any value in this field.


[Though the email received is shown in the incident for which the mail is being sent]


Capture.png


but no ID is being inserted


Capture.png


The ID field is empty


Hi,



Should it be current.update(); ?



Was the incident already created and What is Type of Target field?



Thanks


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


Hi,



I will try current.update..Yes the incident was already created.


Based on a mail I am supposed to insert the "ID" for that incident.



Type of field is "string"