- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2024 10:11 AM
I'm fairly new in a SN Associate position - not even certified quite yet. Mainly been working with flows and forms so far, so this code is out of my league at this point. One of my tasks is to deal with a multi-step flow that fires when the system receives an email. The server that sends the email sometimes creates these New Hire emails with null values for the name, email, etc. SN doesn't recognize the email body has no info attached after the Name: ... Email: ... labels and fires the Flow. I've been unable to cause the Flow to stop based on null values, since most of the steps are coded in. So, is there a way to script in an ignore action directly in the inbound email action script if there's no info in the body?
Thanks in advance for any help!
Email body example:
Inbound Email Action:
(function runAction( /*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {
gs.include('validators');
//if email is recevied from 'acctadmin@abc.com'
try {
var twoStepEnabled = gs.getProperty('glide.sc.checkout.twostep', false);
var debugLogger = '**Create Request: Workstation Build Req**';
var catItemId = WorkstationCatalogItemSysId.WORKSTATION_REQUEST_ID;
var companyEmailDomainInfo = WorkstationCatalogItemSysId.COMPANY_EMAIL_DOMAIN;
var emailFrom = email.origemail;
var emailBodyText = email.body_text;
var emailName = email.body.name.toString();
var emailLogin = email.body.login.toString();
var emailDept = email.body.dept.toString();
var emailLocation = email.body.location.toString();
var reqForUser = WorkstationCatalogItemSysId.WORKSTATION_REQUESTED_FOR_USER_ID; //emailFrom;
var newHireUserId = getUserInfo(emailLogin, companyEmailDomainInfo, emailName) || reqForUser;
debugLogger += '\nemailLogin= ' + emailLogin;
debugLogger += '\nnewHireUserId= ' + newHireUserId;
debugLogger += '\nemailUserName= ' + emailName;
debugLogger += '\nemailLocation= ' + emailLocation;
/*
The Following User is new hire. Please proceed with Workstation Build.
Email to AcctAdmin@abc.com Body Contains: "The Following User is new hire"
*****
Name:
Login:
Dept:
Location:
*/
var ciInfoparser = '';
var cart = '';
var item = '';
var cartDetails = '';
cart = new sn_sc.CartJS();
item = {
'sysparm_id': catItemId.toString(),
'sysparm_quantity': '1',
'variables': {
'requested_for': newHireUserId.toString(), //new user or itoadmin
'userid': emailLogin.toString(),
'name': emailName.toString(),
'dept': emailDept.toString(),
'location': emailLocation.toString(),
'is_new_hire': 'Yes'
}
};
cartDetails = cart.addToCart(item);
debugLogger += '\n' + JSON.stringify(cartDetails);
//cart.setRequestedFor(cartReqFor);
var cartItems = cart.getCartItems();
var checkoutInfo = false;
//var checkoutInfo = cart.checkoutCart();
debugLogger += '\ntwoStepEnabled= ' + twoStepEnabled;
//If two-step checkout is not enabled
if (!twoStepEnabled || twoStepEnabled == 'false') {
checkoutInfo = cart.checkoutCart();
request_number = checkoutInfo.request_number.toString();
debugLogger += '\nCartItems: ' + JSON.stringify(checkoutInfo);
} else {
checkoutInfo = cart.checkoutCart();
debugLogger += '\nCartItems twoStepEnabled: ' + JSON.stringify(checkoutInfo);
requestDetails = cart.submitOrder(checkoutInfo);
request_number = requestDetails.request_number.toString();
debugLogger += '\nRequestDetails : ' + JSON.stringify(requestDetails);
}
cart.empty();
debugLogger += '\nrequest_number: ' + request_number;
gs.info(debugLogger);
//set current to match the request that was created
if (!JSUtil.nil(request_number)) {
var scReqRec = new GlideRecord('sc_request');
scReqRec.addQuery('number', request_number);
scReqRec.query();
if (scReqRec.next()) {
//current = scReqRec;
scReqRec.work_notes = "received from: " + email.origemail + "\n\n" + email.body_text;
scReqRec.update();
sys_email.target_table = 'sc_request';
sys_email.instance = current.sys_id;
sys_email.update();
}
var scReqItemRec = new GlideRecord('sc_req_item');
scReqItemRec.addQuery('request.number', request_number);
scReqItemRec.query();
while (scReqItemRec.next()) {
scReqItemRec.work_notes = "received from: " + email.origemail + "\n\n" + email.body_text;
scReqItemRec.update();
sys_email.target_table = 'sc_req_item';
sys_email.instance = scReqItemRec.sys_id;
sys_email.update();
}
}
} catch (err) {
gs.error('Error in inbound action: Create Request: Workstation Build Req: ' + err);
gs.info(debugLogger);
}
})(current, event, email, logger, classifier);
function getUserInfo(userName, companyEmailDomainInfo, firstLastName) {
var userId = '';
//if userName is empty or if undefined, return
if (JSUtil.nil(userName)) {
return userId;
}
//query user table for existing user and if found, return, else create new user based on userName
var userRec = new GlideRecord('sys_user');
userRec.addActiveQuery();
userRec.addQuery('user_name', 'STARTSWITH', userName);
userRec.query();
if (userRec.next()) {
userId = userRec.getValue('sys_id');
} else {
userRec.initialize();
userRec.user_name = userName + '' + companyEmailDomainInfo;
userRec.email = userName + '' + companyEmailDomainInfo;
if (!JSUtil.nil(firstLastName)) {
var firstName = firstLastName.split(" ")[0];
var lastName = firstLastName.split(" ")[1];
if (!JSUtil.nil(firstName)) {
userRec.first_name = firstName;
}
if (!JSUtil.nil(lastName)) {
userRec.last_name = lastName;
}
}
userId = userRec.insert();
}
return userId;
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2024 12:46 PM
Hey @darronf,
So are you saying if any of the fields (name, login, dept, location) is empty, the email should not be processed?
Try adding the following line on line 16 (right after var emailLocation = ....)
if( gs.nil(emailName) || gs.nil(emailLogin) || gs.nil(emailDept) || gs.nil(emailLocation))
{
return; //if any of the fields are empty, terminate the script action
}
On a side note, have you considered using an Inbound Email Flow instead of writing an Inbound Script Action? It's low-code and should be easier to maintain.
Cheers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2024 12:46 PM
Hey @darronf,
So are you saying if any of the fields (name, login, dept, location) is empty, the email should not be processed?
Try adding the following line on line 16 (right after var emailLocation = ....)
if( gs.nil(emailName) || gs.nil(emailLogin) || gs.nil(emailDept) || gs.nil(emailLocation))
{
return; //if any of the fields are empty, terminate the script action
}
On a side note, have you considered using an Inbound Email Flow instead of writing an Inbound Script Action? It's low-code and should be easier to maintain.
Cheers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2024 05:57 AM
Thanks, James! I'll take this back to the team and watch for resolution. Unfortunately, we can't replicate the problem, so I'll just watch to verify that we don't receive any more of these in the next little while.
In the meantime, I didn't think you could scrape emails from Flows? In doing some more digging, I discovered Ben Scherer's Parse Email Flow Action, so, thanks for suggesting a different option. Great tip! Think I'm going to play around with that some.