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

Shree_G
Mega Sage

Hello @jmiskey ,

 

If you are not expecting any characters after the Employee Number i.e. if

Employee Number: 012345<----------Blank-------------->

 

Then you can skip the step "var ee_num = temp.substr(0, 7);" and use code:

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

 

temp = temp.substr(a+16) // this will take substring starting from index a+16 to the end.

 


If this solution helped resolve your issue, please consider marking it as helpful or correct.
This will assist others in finding the solution faster and close the thread.

No, that did not work.  That code update did not process either record.

It is almost like the spaces after the last characters are not normal spaces, which are removed by "trim".

I wonder if it is perhaps some special space (like a non-breaking space), or perhaps a carriage return.

@jmiskey ,

 

Interesting. Have you tried accepting 7 chars only and then trying to trim. If the Employee Number chars are 6 then the extra white space should be removed.

//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);
ee_num = ee_num.trim(); // Trim for extra white space if present.

 

 Tried in the Background script, Output is as below:

With 6 chars:

Shree_G_0-1744980281261.png

Output:

Shree_G_1-1744980299464.png

 

Hope this little tweak helps.

 


If this solution helped resolve your issue, please consider marking it as helpful or correct.
This will assist others in finding the solution faster and close the thread.

Sorry, this did not work.