A Script to extract the username from the short destruction

Wyatt Fudal1
Tera Guru

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");

}

 

4 REPLIES 4

Bert_c1
Kilo Patron

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;
    }
}

 

 

Peter Bodelier
Giga Sage

Hi @Wyatt Fudal1 

 

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.

If the script doesn't work, what method would you try? I applied the vanilla first and tried with some changes and sadly the record does not update the caller field. 

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.