Pull requested for details from inbound email to create request
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-02-2025 04:43 AM
Hi Experts,
We have a requirement to create a Request from an inbound email action, which is working fine with my existing code. The client wants us to populate Requested for details from the Email Body. The format of the email would look something like this:
Requested By: (User Name)
Email address: (email address)
I'm not very good at coding, so kindly help me with this requirement.
Existing code is as below:
Thanks in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-02-2025 05:18 AM
@Community Alums
try this
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-02-2025 08:57 PM
@Community Alums
Hope you are doing good.
Did my reply answer your question?If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-02-2025 09:17 PM
Hi @Community Alums ,
You can use Revised code.
(function runAction(/*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
// Extracting 'Requested By' and 'Email address' from the email body
var emailBody = email.body_text.toString();
var requestedByMatch = emailBody.match(/Requested By:\s*(.*)/i);
var emailAddressMatch = emailBody.match(/Email address:\s*(.*)/i);
var requestedBy = requestedByMatch ? requestedByMatch[1].trim() : null;
var emailAddress = emailAddressMatch ? emailAddressMatch[1].trim() : null;
// Logging the extracted information for debugging
logger.info('Requested By: ' + requestedBy);
logger.info('Email Address: ' + emailAddress);
// Find the user in ServiceNow based on the extracted email address
var requestedFor = null;
if (emailAddress) {
var userGR = new GlideRecord('sys_user');
userGR.addQuery('email', emailAddress);
userGR.query();
if (userGR.next()) {
requestedFor = userGR.sys_id.toString();
}
}
// Create a request using the cart API
var item = cart.addItem('sys_ID_of_catalog_item');
cart.setVariable(item, 'short_description', email.subject.toString());
cart.setVariable(item, 'description', email.body_text);
cart.setVariable(item, 'assg_group', 'sys_id');
// If the requestedFor user was found, set it in the cart
if (requestedFor) {
cart.setVariable(item, 'requested_for', requestedFor);
}
var rc = cart.placeOrder();
// Update the RITM with additional details
var ritmRec = new GlideRecord("sc_req_item");
ritmRec.addQuery("request", rc.sys_id);
ritmRec.query();
if (ritmRec.next()) {
ritmRec.short_description = email.subject.toString();
ritmRec.comments = "Received from: " + email.origemail + "\n\n" + email.body_text;
ritmRec.contact_type = "email";
ritmRec.update();
}
// Update the email target
if (rc) {
var email_rec = new GlideRecord('sys_email');
email_rec.get(email.sys_id);
email_rec.instance = rc.sys_id;
email_rec.target_table = "sc_request";
email_rec.update();
}
})(current, event, email, logger, classifier);
-------------------------------------------------------------------------
If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.
Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay
-------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-06-2025 01:49 AM
@Runjay Patel Your solution worked. I appreciate your help.