Inbound Email Action Script Not Working When Field Can be Different Lengths

jmiskey
Kilo Sage

I have an Inbound Email Action where I am parsing and email, and trying to get the Employee Number from it in order to use in a GlideRecord to make updates to a field on the User table.

 

The issue is with the Employee Number field.  It could be 6 or 7 characters long.  So that row in the body of the email will look something like this:

Employee Number: 012345

or

Employee Number: 0123456

 

Listed below is my my section of the script that is pulling off the employee number.

//find employee number
var temp = ebody;
var a = temp.indexOf("Employee Number:");
temp = temp.substr(a + 16, a + 26);
temp = temp.trim();
var ee_num = temp.substr(0, 7);

and then I am using that in my GlideRecord like this to find the record to update:

//update Transfer Date on user record
var usr = new GlideRecord('sys_user');
usr.addQuery('employee_number', ee_num);
usr.query();

while (usr.next()) {
	usr.u_transfer_date = xfer_date;
	usr.update();
}

 (the xfer_date field is pulled elsewhere in the code and that part is working fine, so no need to worry about that).

 

The issue I am having is that if the Employee Number is exactly 7 digits long, it works fine and it updates the Transfer Date field.  However, if it is only 6 digits long, it does not work (Transfer Date is not updated).

 

If I change the last line in the first  block of code above to this:

var ee_num = temp.substr(0, 6);

 then the 6 digit Employee Number update correctly, but the 7 digits ones do not.

 

What is the most efficient way of updating this code so that it will work for BOTH 6 and 7 digit Employee Numbers?

 

Thanks

1 ACCEPTED SOLUTION

sunil maddheshi
Tera Guru

@jmiskey 

Can you try with below script:

// Extract employee number using regex
var ee_num = '';
var match = ebody.match(/Employee Number:\s*(\d{6,7})/);
if (match) {
    ee_num = match[1];
}

// Now query user table with this ee_num
if (ee_num) {
    var usr = new GlideRecord('sys_user');
    usr.addQuery('employee_number', ee_num);
    usr.query();

    while (usr.next()) {
        usr.u_transfer_date = xfer_date;
        usr.update();
    }
}

Please mark correct/helpful if this helps you!

View solution in original post

6 REPLIES 6

sunil maddheshi
Tera Guru

@jmiskey 

Can you try with below script:

// Extract employee number using regex
var ee_num = '';
var match = ebody.match(/Employee Number:\s*(\d{6,7})/);
if (match) {
    ee_num = match[1];
}

// Now query user table with this ee_num
if (ee_num) {
    var usr = new GlideRecord('sys_user');
    usr.addQuery('employee_number', ee_num);
    usr.query();

    while (usr.next()) {
        usr.u_transfer_date = xfer_date;
        usr.update();
    }
}

Please mark correct/helpful if this helps you!

Thank you!

This solution worked!