- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2022 02:39 AM
Hello Community,
Hopefully someone can help and end my mental suffering.
I'm trying to setup an inbound action to create a REQ & RITM. I've been able to setVariables for everything (via script) from the email body text with the exception of one: -
The email contains a line that contains a user ID which corresponds to what the users ID is in ServiceNow. I have a catalog variable as a reference field to the sys_user table and what I'm wanting to achieve is setVariable(item, 'variable_name', value); after I've "extracted" the value from the email body.
The email body contains the below: -
"Worksite: Floor 1
Employee's managed service ID: AndyB_TEST
Line Managers First Name:David"
I can successfully pull out the ID above (in bold) from the middle line using this: -
var managedid2 = body.substring(body.indexOf("Employee's managed service ID:") +31, body.lastIndexOf("Line Managers First Name:"));
logger.info("Managed ID Found: " + managedid2);
And this shows up as expected in the inbound email log: -
Information | Managed ID Found: AndyB_TEST |
I've had a good dig through the community articles (apart from the ever increasing one's that come up with "
And as per the articles I understand I need to obtain the sys_id in the sys_user table for the ID that I want to use in 'setVariable'. I must have tried 20+ different ways, e.g. with .getValue, without. Adding .toString() etc., etc., etc. but the result is always the same - I don't get the sys_ID value of this user ID in the sys_user table: -
var manidSysID = '';
var manid = new GlideRecord('sys_user');
manid.addQuery('user_name', managedid2);
manid.query();
if (manid.next()) {
manidSysID = manid.sys_id;
}
logger.info("Set Var Managed ID: " + manidSysID);
Each variation of the code always comes up with either nothing (as shown below) or "undefined"
Information | Set Var Managed ID: |
The REQ & RITM get created but the catalog variable which should be set with the "ID" is just blank.
The really frustrating bit is that pretty much every variation on the code produces the expected result if I run it as a background script. I amend the 'var body =' to include the text from the email and I amend the logger.info' entries to 'gs.print' and I get the results I'm wanting: -
I can only assume I'm missing something obvious but I've been going around in circles with it and think I've gone "code blind". Can anyone help please!?
Below is my current script on my inbound action.
createReq();
function createReq() {
var cart = new Cart();
var item = cart.addItem('321bb20b1bed7010b2f97662164bcbec');
var body = email.body_text;
logger.info("Body Text: " + body);
var managedid2 = body.substring(body.indexOf("Employee's managed service ID:") +31, body.lastIndexOf("Line Managers First Name:"));
logger.info("Managed ID Found: " + managedid2);
var manidSysID = '';
var manid = new GlideRecord('sys_user');
manid.addQuery('user_name', managedid2);
manid.query();
if (manid.next()) {
manidSysID = manid.sys_id;
}
logger.info("Set Var Managed ID: " + manidSysID);
cart.setVariable(item, 'variable_name', manidSysID);
var rc = cart.placeOrder();
updateRITM(rc.sys_id);
}
Thank you in advance to anyone that can assist.
Best regards
Andy
Solved! Go to Solution.
- Labels:
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-27-2022 01:21 AM
After some further digging through community pages / posts I've managed to fix my script by amending this: -
var managedid2 = body.substring(body.indexOf("Employee's managed service ID:") +31, body.lastIndexOf("Line Managers First Name:"));
logger.info("Managed ID Found: " + managedid2);
To this: -
var managedid2 = email.body.employee_s_managed_service_id.toString();
The inbound action script now works as I need it to and the variable set's correctly on the catalog item (RITM).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2022 06:40 AM
Have you tried replacing 'user_name' with 'name'?
I'm not sure what value you receive, is it the login name, or is it the name?
If my answer helped you in any way, please then mark it as helpful.
Mark
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-27-2022 01:17 AM
Thanks Mark,
No it's definitely 'user_name' I need and yes the value I receive from the email body is the login name which corresponds to 'user_name' in the sys_user table.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-27-2022 01:21 AM
After some further digging through community pages / posts I've managed to fix my script by amending this: -
var managedid2 = body.substring(body.indexOf("Employee's managed service ID:") +31, body.lastIndexOf("Line Managers First Name:"));
logger.info("Managed ID Found: " + managedid2);
To this: -
var managedid2 = email.body.employee_s_managed_service_id.toString();
The inbound action script now works as I need it to and the variable set's correctly on the catalog item (RITM).