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

In the when to run condition there is an extra space in front of email.subject.startsWith(" Incident Number"), is this correct?



Can you please check if the inbound action was ever executed from mailbox -> Received logs?



You can open your specific email and verify the Email Logs to see if this inbound action was processed or not.



Thanks


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


Hi,



Email response is being inserted in my ticket everytime I mail back to servicenow from my GMail.


the logs I received for javascript are :



10:58:59 (708)sysevent_in_email_action_list.doUnloaded table does not match current table sysevent_in_email_action


10:59:00 (170)sysevent_in_email_action.doadded render event for ui_policy_onLoad


10:59:00 (172)sysevent_in_email_action.do[00:00:00.298] form rendered


10:59:00 (415)sysevent_in_email_action.dorunning inline scripts, count: 0


10:59:00 (416)sysevent_in_email_action.dorunBeforeRender


10:59:00 (469)sysevent_in_email_action.doCreating filter for incident.sysevent_in_email_action.template


10:59:00 (470)sysevent_in_email_action.dosysevent_in_email_action.template


10:59:00 (471)sysevent_in_email_action.do


10:59:00 (476)sysevent_in_email_action.do[00:00:00.000] onLoad calcReturn


10:59:00 (480)sysevent_in_email_action.do[00:00:00.004] ui_policy loaded


10:59:00 (481)sysevent_in_email_action.dorunAfterAllLoaded, functions: 16


10:59:00 (527)sysevent_in_email_action.dosetQuery Synchronously:  


10:59:02 (509)sysevent_in_email_action.do[00:00:01.979] *** WARNING *** GlideAjax.getXMLWait - synchronous function - processor: QueryParseAjax


10:59:05 (856)sysevent_in_email_action.do[00:00:03.313] *** WARNING *** GlideAjax.getXMLWait - synchronous function - processor: SysMeta


10:59:05 (928)sysevent_in_email_action.doTime: 5401 for: [13] function() { elems = $$('input.glide_destroy_filter'); elems.each(function(elem) { var ref = elem.id; if (ref) { filterExpanded = true; var depName = elem.getAttribute('data-dependent'); var depTable = elem.getAttribute('data-dependent_table') if (!depTable) depTable = elem.getAttribute('data-static-dependent'); var table = resolveDependentValue(ref, depName, depTable); if (table != null) { var fname = ref; var hinput = $(fname); var xfilter = unescape(hinput.value); var id = table + "." + fname; createCondFilter(id, xfilter, fname, elem, false); } addOnSubmitEvent(findParentByTag($(ref), "FORM"), function() { var hinput = $(ref); var cfilter = getFilter(ref); hinput.value = unescape(cfilter); }); } }); }


10:59:05 (930)sysevent_in_email_action.dofireAllChangeHandlers start


10:59:05 (931)sysevent_in_email_action.dofireAllChangeHandlers end


10:59:05 (932)sysevent_in_email_action.dolate load functions: 3


10:59:05 (937)sysevent_in_email_action.do[00:00:05.456] runAfterAllLoaded finished


10:59:06 (273)sysevent_in_email_action.doafter page loaded starting


10:59:06 (383)sysevent_in_email_action.do[00:00:00.110] after page loaded complete, functions called: 10


10:59:14 (662)sysevent_in_email_action.do[00:00:07.317] *** WARNING *** GlideAjax.getXMLWait - synchronous function - processor: SysMeta


11:00:31 (101)sysevent_in_email_action.dodirty form focus


11:12:49 (046)sysevent_in_email_action_list.do[00:00:00.052] Reclone headers


how to do this "You can open your specific email and verify the Email Logs to see if this inbound action was processed or not."


Go To System Mailboxes -> Received logs -> Locate the latest email send by you and open it.



At the bottom you have a related list called as Email Logs which will show if the inbound action was processed or not.



So I see this has nothing to do with the extra space in your condition.



Thanks


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


step1.PNG