- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2023 09:21 AM
Hello,
We are receiving service request submitted from using emails. The RITM description field captures the entire email.
I would like to search the name "From: John,Lopez" in the RITM description field and populate in "Requested For" field.
See attached
Could someone please help? Thank you
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2023 06:31 PM
You are using an after business rule to update current. That beaks rule no. 1 of Business rules: whenever the current record is to be updated, it must be done using a before business rule.
Rule no. 2 being: whenever a different record than the current one needs to be updated, it must be done using an after business rule.
The other issue is the RegExp itself; it should be:
/^From:\s*(.+)$/mi
Another issue is that you are using the name directly to set the field. You need to fetch the user's sys_id and use that to set the value of the field:
GetIDValue('sys_user', )
Something like:
// Script to extract sender name from RITM description and set as requested for
// Get RITM description
var description = '' + current.description;
// Use regular expression to extract name from "From" field
var senderName = description.match(/^From:\s*(.+)$/mi);
// Set extracted name as requested for
current.requested_for = GetIDValue('sys_user', senderName[1]);
Of course the code should check that the value returned by GetIDValue is not null and only set the requested for if it is not. This is also a very fragile solution as the slightest mistype, even an additional or missing space in the name and it will not work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2023 10:50 PM
Hello,
Something simple like
var senderName = description.match(/^To:.+<mailto:([^<>]+)/mi);
should do it, but if you want a solution where you match either the original patter or this one, a different RegExp is needed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2023 03:49 PM
E-mail is safer because it tends to be unique due to other system where it will be used. E.g. when creating a directory (LDAP) user, one cannot create two with the same user ID, which most of the time is and will match the e-mail. So if there are two John Does in the company, their user id will most likely be their e-mail, one john.doe@example.com, the other john.doe1@example.com. Also people will most likely copy it instead of typing ti. Also there's not gonna be a problem of spacing. Or reversing name and surname. Still not 100% safe, but way safer than names.
As for the code, yes it needs to be modified. In place of
current.requested_for = GetIDValue('sys_user', senderName[1]);
One needs something like:
current.requested_for = getUserByEMail(senderName[1]);
function getUserByEMail (eMail) {
var gr = new GlideRecord('sys_user');
if (gr.get('email', eMail))
return gr.getUniqueValue();
// There should be an else here returning some default user name,
// in case locating by e-mail fails
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2023 08:28 AM
Good morning @-O-
That was an excellent recommendation. I sincerely thank you and everyone who provides assistance and suggestions.
I'm stressing out on this request, because I do not understand Java enough to make it works.
I have spoke to HR and their agreed to include employee's email address in the body of the email.
The RITM description will capture the email like this: From: Doe,John <jdoe@xyz.com>
Can we have the BR compare the email in the description against the email in the sys_user table?
If it found the match email then populate the "name" in the sys_user table to the Request For field
If not, display a message "Request for name is not exists in the system. Please contact HR"
Here is the code, but I don't know how to modify it to get it to works.
Could you please continue to help? We really appreciate your help.
var description = '' + current.description;
var senderName = description.match(/^From:\s*(.+)$/mi);
current.requested_for = getUserByEMail(senderName[1]);
function getUserByEMail(eMail) {
var gr = new GlideRecord('sys_user');
if (gr.get('email', eMail))
return gr.getUniqueValue();
else
gs.addInfoMessage("Request for name is not exists in the system. Please contact HR for assistance");
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2023 08:58 AM - edited 01-24-2023 08:59 AM
Note that Java is not what you are dealing with here. It is JavaScript. They have nothing in common really besides the name.
As for the problem, all you need it to adjust the RegExp:
var senderName = description.match(/^From:\s*.+\s*<([^<>]+)>$/mi);
But for a better working solution you should re-arrange the code a bit.
- Introduce events and notifications triggered by events - because Inbound Actions are not interactive so there in no User Interface and no user that could show and view the messages.
- Report more detailed situation and prevent updating the Requested for field if the information is not there.
Something like this:
var description = '' + current.description;
var senderName = description.match(/^From:\s*.+\s*<([^<>]+)>$/mi);
var userEMail;
if (senderName)
userEMail = getUserByEMail(senderName[1]);
else
gs.eventQueue('sn_hr_core.inbound.e-mail_missing', current);
if (userEMail)
current.requested_for = getUserByEMail(senderName[1]);
else
gs.eventQueue('sn_hr_core.inbound.e-mail_faulty', current);
function getUserByEMail (eMail) {
var gr = new GlideRecord('sys_user');
if (gr.get('email', eMail))
return gr.getUniqueValue();
}
In addition to this BR you would need to go to System Policy -> Events -> Registry and create the two event names (inbound.e-mail_missing and inbound.e-mail_faulty):
And notifications for those:
I am assuming here that it is some kind of HR Case (or some other HR CoE) record is created from the inbound e-mail. If not, you will need to select the proper table in the proper scope, both when defining the events and when defining the notifications.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2023 09:00 AM
Note that the original code contains incorrect event names. Have updated those, so make sure you copy the latest version.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2023 01:20 PM
Hi @-O-
I have done a demo to HR using your ideal and codes and everyone was very happy. It is working out more than we anticipated. This is fantastic work and you are amazing helper. Thank you so much again for continuing to help.
For my learning proposes. What is this line means?
var senderName = description.match(/^From:\s*.+\s*<([^<>]+)>$/mi);