Extract Date from Description and populate Due Date

kevinthury
Tera Guru

We are on week 3 of ServiceNow and I am
looking for a way to extract a date from in Inbound Email and insert this into
the Due Date field of the REQ and RITM records.   I was looking to do so
with an activity within the workflow associated with these inbound email.

How it works...

  1. Email received
  2. Inbound email Action
    created REQ (w/ email subj as Short Desc and body as Desc) and RITM
  3. Workflow Copies Short
    Desc and Desc from REQ to RITM

    4.   Workflow generates
Catalog Task based on contents of RITM Desc

These four steps are in place and
working.   What I would like to do is this create "Step 3.5" that extracts
the Start Date from the RITM Description and plugs that into the RITM

Due Date.

The Start Date in the description is a line
item that looks like this:  

Start Date: 6/23/2017

Since we are very new with ServiceNow, my
scripting skills are poor to non-existent.   Any guidance from this
community will be appreciated.

1 ACCEPTED SOLUTION

Got some javascript help from one of our developers and managed to get this working with the following script.   This script was included as an activity in a Workflow



var string = current.description;


var date = string.indexOf("Start Date: ");   //Mark starting point


var end = string.indexOf("Requestor: ");     //mark End Point


var finaldate = string.substring(date+12,end);




//******************************  


//format date here to yyyy-MM-dd  


//******************************  




var mydate = finaldate;


var chunks = mydate.split('/');


var year = chunks[2].substring(0,4);


var month = chunks[0].substring(0,2);


if(parseInt(month)<10){


    month = '0'+month;


    }


var day = chunks[1].substring(0,2);


var fulldate = year+'-'+month+'-'+day+ " 05:00:00";


var gDate = new GlideDateTime(fulldate);


current.due_date = gDate;


View solution in original post

7 REPLIES 7

Patrick Fedigan
Giga Guru

Hello Kevin,



This is certainly doable with a few lines of javascript. Do you have any script written so far?



You can make a business rule that sets the date field of the RITM from the RIMT description/when the record is created.



The JS would look something like this on an After business rule:



(function executeRule(current, previous /*null when async*/) {



var string = current.description;


var date = string.substring(string.indexOf('Start Date: ') + 12,


string.indexOf('\n')); //if the date ends with a new line



//******************************


//format date here to yyyy-MM-dd


//formattedDate


//******************************



var gDate = new GlideDateTime();


//gDate.setValue(formattedDate);



current.due_date = gDate;




})(current, previous);











Cheers,


Thanks for the tip.   I added the rule as suggested, but the due_date did not change.   Not sure where it's failing.  



BUSINESS RULE:


Name:   Update Due Date


Table:   Requested Item [sc_req_item]


Application:   Global


Active: [checked]     Advanced:   [checked]



WHEN TO RUN


When:   After


Order: 100


Insert: [checked]     Update:   [checked]   Delete: [unchecked]     Query:   [unchecked]


Filter Condition:   Request.Short description starts with {subject of inbound email}



ACTIONS


None



ADVANCED


find_real_file.png





What am I missing?


Hello Kevin,



The script wasn't 100% complete. Try this updated one:



(function executeRule(current, previous /*null when async*/) {



var string = current.description;


var date = string.substring(string.indexOf('Start Date: ') + 11,


string.indexOf('\n')); //if the date ends with a new line



//******************************


//format date here to yyyy-MM-dd


//******************************


var dayMonthYear = date.split("/");


var gDate = new GlideDateTime(dayMonthYear[2].trim() + "-" + dayMonthYear[0].trim() + "-" + dayMonthYear[1].trim() + " 12:00:00");



//setDate


current.due_date = gDate;


current.update();




})(current, previous);



Cheers,


Thanks, Patrick.   I believe we are getting closer, but not there yet.   The due-date field is getting wiped out instead of populating with what is captured from the email body.   Thinking that may be due to the RITM being empty upon the creation of the REQ from the email, I set the Business Rule to act on the Request [sc_request] table and performed another test.   This scenario had both due dates populating with the same default value...but not the value from the description.



If you're wondering why the RITM is empty, our consultants devised an Inbound Email Action (IEA) that would grab the email, run a script within the IEA to carry the contents to a Request.   That triggered a Workflow that copies the request.description to the req_item.description...but that's boring stuff.



My first hunch to this is the date formats.   Should there be some statement in that script to add a preceding '0' if there is only one digit.   Example, the date in my description is 7/18/2017.   ServiceNow does not allow a date of '2017-7-18 12:00:00' in the due date field.



Trust me...I am trying to do this on my own, but time constraints are prompting to to ask for help.   You've been great so far and further advice is appreciated.