Copy date and time given in email body and update in a datetime field

ROS4349
Tera Expert

Hi Experts,

I have a requirement to copy date and time provided in email body and update a hardware asset table depretiation start date field.

I have created a inbound email action to parse email body but struck at trimming date and time from recieved email body.

Can you please help me with the coding pls.

Sent mail will be like below.

ROS4349_0-1703662115483.png

from above mail i need to copy date and time and update a date time type field.

 

1 ACCEPTED SOLUTION

Thank you...
Fixed it using below code

var gr = new GlideRecord("sys_email");
gr.addQuery("sys_id", "27a6e32a1b1bbd50582add7cbc4bcb18");
gr.query();

if (gr.next()) {
    var str = gr.body;
    var matchResult = str.match(/[0-9][0-9][0-9][0-9]\-[0-1][0-9]\-[0-3][0-9]\s[0-2][0-9]\:[0-6][0-9]\:[0-6][0-9]/);

    if (matchResult && matchResult.length > 0) {
        var str2 = matchResult[0];
        gs.info(str2);

        var grInc = new GlideRecord('alm_hardware');
        grInc.addQuery('sys_id', '000e28f7476b011042fca852736d4323');
        grInc.query();

        if (grInc.next()) {
            grInc.depreciation_date = str2;
            grInc.update();
        } else {
            gs.info("Asset not found based on the specified condition.");
        }
    } else {
        gs.info("No valid date-time pattern found in the email body.");
    }
}

View solution in original post

9 REPLIES 9

newhand
Mega Sage

@ROS4349 

It because the str (gr.body) doesn't contain a datetime string .

Should put some null check logic before using the match result.

 

str2 = str.match(/[0-9][0-9][0-9][0-9]\-[0-1][0-9]\-[0-3][0-9]\s[0-2][0-9]\:[0-6][0-9]\:[0-6][0-9]/);
if(str2){
    gs.info(str2[0])
}else{
   gs.info("can't  match !")
}

 

Please mark my answer as correct and helpful based on Impact.

can you help me with running below code for all hardware assets with recieved mail in servicenow inbox.

Because cant find a unique value in alm_hardware table and sys_email table.

var gr = new GlideRecord("sys_email");
gr.addQuery("sys_id", "27a6e32a1b1bbd50582add7cbc4bcb18");
gr.query();

if (gr.next()) {
    var str = gr.body;
    var matchResult = str.match(/[0-9][0-9][0-9][0-9]\-[0-1][0-9]\-[0-3][0-9]\s[0-2][0-9]\:[0-6][0-9]\:[0-6][0-9]/);

    if (matchResult && matchResult.length > 0) {
        var str2 = matchResult[0];
        gs.info(str2);

        var grInc = new GlideRecord('alm_hardware');
        grInc.addQuery('sys_id', '000e28f7476b011042fca852736d4323');
        grInc.query();

        if (grInc.next()) {
            grInc.depreciation_date = str2;
            grInc.update();
        } else {
            gs.info("Asset not found based on the specified condition.");
        }
    } else {
        gs.info("No valid date-time pattern found in the email body.");
    }
}

@ROS4349 
I cant understand your problem clearly..

Do you mean  the email is binding to some hardware,but there isn't enought information to find the hardware from the email ?

And what you are trying to do  is  to update the hardware's depreciation_date  from the email body?

 

I'm sorry , it's 18:00 here , and i will come back after 16 hours...

 

 

Please mark my answer as correct and helpful based on Impact.

Sure Thank you... I will be waiting for your response.

 

Requirement is :

Configured a email to ask accept the depritiation date or propose a new date instead of. So below is the email notification i got in my inbox

ROS4349_0-1703673460250.png

Here user can click on defer button to compose a mail with new date and time as below

ROS4349_1-1703673545790.png

 

once servicenow got this email inbound email action should read the email body and update given date and time in depreciation date field.

So below background script need to write in inbound action script

=====================================================

var gr = new GlideRecord("sys_email");
gr.addQuery("sys_id", "27a6e32a1b1bbd50582add7cbc4bcb18");
gr.query();

if (gr.next()) {
    var str = gr.body;
    var matchResult = str.match(/[0-9][0-9][0-9][0-9]\-[0-1][0-9]\-[0-3][0-9]\s[0-2][0-9]\:[0-6][0-9]\:[0-6][0-9]/);

    if (matchResult && matchResult.length > 0) {
        var str2 = matchResult[0];
        gs.info(str2);

        var grInc = new GlideRecord('alm_hardware');
        grInc.addQuery('sys_id', '000e28f7476b011042fca852736d4323');
        grInc.query();

        if (grInc.next()) {
            grInc.depreciation_date = str2;
            grInc.update();
        } else {
            gs.info("Asset not found based on the specified condition.");
        }
    } else {
        gs.info("No valid date-time pattern found in the email body.");
    }
}
 
================================================
Above script is working in background script but not working with inbound action. need your help in fixing this.
 
Instead of working on single sys_id it should work on all records and all emails.

@ROS4349 
I got it.

1.when using inbound email  action ,check if the 【When to run】 tab is defined correctly。

2. You don't need to  query the email table to get the email body .

    Use email obecjt to get the information (exp:  var bodyText = email.body_text;)

3.  I found that asset tag (p1000092) is included in the eamil title.

    use asset tag to query the alm_hardware table maybe the correct way.(you can use match function to extract the asset tag from the email title)

 

 

 

     

Please mark my answer as correct and helpful based on Impact.