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 
Try this 

var str = "abccc2023-01-01 12:33:33dfdfdf";
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]/)[0];
gs.info(str2)

 

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

Thank you newhand.

Seems it is working but can you pls help me in fixing below error

ROS4349_0-1703666767485.png

 

Below are the lines 

var gr = new GlideRecord("sys_email");

gr.addQuery("sys_id","7417d26a1b5f7d50582add7cbc4bcb21");

gr.query();

if(gr.next())

{

var str = gr.body;

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]/)[0];

gs.info(str2)

}

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.");
    }
}