A Script to extract the username from the short destruction
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2023 07:19 AM
Hello all, I’m trying to update the caller field from the short description of the record in Flow Designer. The flow is triggered by an inbound email, but the email is created by a service account, so the caller field is blank. When the incident is created the short description contains the username. Would I be able to extract the username from the short description and update the caller field with an update record action using scripting?
This is a script I found but did not work
var shortDescription = current.short_description; // Replace 'current.short_description' with your actual short description field
var match = pattern.exec(shortDescription);
// Check if a match is found
if (match && match.length > 1) {
var username = match[1]; // The extracted username
gs.info("Extracted username: " + username);
'username' in your script or assign it to a variable for further processing.
} else {
gs.error("No username found in short description");
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2023 08:03 AM - edited 09-27-2023 08:07 AM
Hi Wyatt,
Here's some script logic tested in Scripts Background. Hopefully useful in updating your Flow Designer script.
//var shortDescription = current.short_description; // Replace 'current.short_description' with your actual short description field
// for testing
var shortDescription = "This is a short descritption value. requested by Abraham Lincoln";
var requestedByPostition = shortDescription.indexOf('requested by ');
var userName = "";
if (requestedByPostition > 0) {
userName = shortDescription.substr(requestedByPostition+13);
}
else {
gs.error("No username found in short description");
}
if (userName.length > 0) {
gs.info("userName = " + userName);
var sur = new GlideRecord('sys_user');
sur.addQuery('name', userName);
sur.query();
if (sur.next()) {
var surSysId = sur.sys_id.toString();
gs.info("SysId = " + surSysId);
// update caller field, set equal to surSysId
current.caller_id = surSysId;
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2023 08:14 AM
Assuming you are using this as inline script in flow designer, and the incident is the trigger for the flow, you can use this script, or adjust based on your use case:
var pattern = ' requested by ';
var shortDescription = fd_data.trigger.current.short_description
var match = shortDescription.search(pattern);
gs.info(match)
// Check if a match is found
if (match != -1) {
var username = shortDescription.substring(match+pattern.length); // The extracted username
return username
} else {
gs.error("No username found in short description");
return '';
}
Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2023 09:54 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2023 11:18 AM - edited 09-27-2023 01:51 PM
You could use a business rule, that runs on Insert, and "When" is 'Before'. Add a condition: "Caller", "Is empty". Using Peter's "match" logic and script:
(function executeRule(current, previous /*null when async*/) {
// Add your code here
if (gs.isInteractive())
return; // don't run if session is interactive.
var pattern = ' requested by ';
var shortDescription = current.short_description;
var match = shortDescription.search(pattern);
gs.info("position = " + match);
// Check if a match is found
if (match != -1) {
var username = shortDescription.substring(match+pattern.length); // The extracted username
gs.info("username = " + username);
var sur = new GlideRecord('sys_user');
sur.addQuery('name', username);
sur.query();
if (sur.next()) {
var surSysId = sur.sys_id.toString();
// update caller field, set equal to surSysId
current.caller_id = surSysId;
}
} else {
gs.addErrorMessage("No username found in short description");
}
})(current, previous);
The caller_id field is a reference field to the sys_user table, so a sys_id value is needed there. Maybe Peter can adjust the Flow Logic, and you can avoid using a Business Rule.