Converting Incident Description text to Catalog Item Variables

Aaron25
Tera Guru

Hi Everyone,

 

Had a quick question to see if it is possible to convert an incident description text to specific variables on a catalog item assuming the ticket always comes in the same format.

 

Incident 

Description Text:

Full Name : John Smith

Preferred Name : John

Start Date: 09/12/2024

 

Catalog Item:

John Smith to  variable full_name

John to variable preferred_name

09/12/2024 to variable_date

 

Specifically looking for the dynamic text that will come after the full name, preferred name and start date and map them to the corresponding fields using an UI Action.

 

I have the convert to the request down, its just a matter of what to put in the script to convert the text to a variable.

 

Even if it shows as Preferred Name: John Smith that is fine also.

 

Thank You in advance

 

Aaron 

 

 

 

 

2 ACCEPTED SOLUTIONS

Aaron25
Tera Guru

Thanks Everyone for the help. Was able to do this by doing the below. I added exceptions to the array as our name formats are very dynamic for some reason that we get from our onboarding team. Code might be a little messy but was able to get around 50 different test cases with it.

 

if (typeof window == 'undefined') {
    CreateRequestNow();
}

function CreateRequestNow() {
    var message = '';
    var sysidOfItem = 'SYSID'; // SysID of the catalog item
    var cartId = GlideGuid.generate(null);
    var cart = new Cart(cartId);
    var item = cart.addItem(sysidOfItem);

      var description = current.description;
 
var fullNameMatch = description.match(/Full Name:\s*([^\n]*(?:\([^\)]*\))?)(?=\s*Preferred First Name|$)/);
var fullName = fullNameMatch ? fullNameMatch[1].trim() : '';


// Extract Preferred First Name from the incident description(?!.*Full Name)
var prefNameMatch = description.match(/Preferred First Name\s*([A-Za-z]+)/);
var prefName = prefNameMatch ? prefNameMatch[1].trim() : '';

// Extract Start Date from the incident description
var startDateMatch = description.match(/Start Date:\s*((\d{1,2}\/\d{1,2}\/\d{4})|(\w+\s\d{1,2}(?:st|nd|rd|th)?,?\s\d{4}))/);
var startDate = startDateMatch ? startDateMatch[1].trim() : '';

// Extract End Date from the incident description
var endDateMatch = description.match(/End Date \(if applicable\):\s*(\d{4}\s\d{2}\s\d{2}|\[not available\])/);
var endDate = endDateMatch ? endDateMatch[1].trim() : '';
 
 
 
var titleMatch = description.match(/Title:\s*([\w\s,&-]+)(?=\s*Employment Type:)/);
var title = titleMatch ? titleMatch[1].trim() : '';

// Extract Employment Type from the incident description
var emptypeMatch = description.match(/Employment Type:\s*([\w\s\-\(\)\/]+)(?=\s*Work Location:)/);
var employeeType = emptypeMatch ? emptypeMatch[1].trim() : '';

// Extract Work Location from the incident description
var workLocationMatch = description.match(/Work Location:\s*([\w\s\-\&]+)(?=\s*Manager Name:)/);
var workLocation = workLocationMatch ? workLocationMatch[1].trim() : '';

// Extract Manager Name from the incident description
var managerMatch = description.match(/Manager Name:\s*([\w\s]+)(?=\s*Cost Center:)/);
var manager_name = managerMatch ? managerMatch[1].trim() : '';

// Extract Cost Center from the incident description
var costCenterMatch = description.match(/Cost Center:\s*(\d+)/);
var costCenter = costCenterMatch ? costCenterMatch[1].trim() : '';


// Set variables for the catalog item
cart.setVariable(item, 'full_name', fullName);
cart.setVariable(item, 'pref_name', prefName);
cart.setVariable(item, 'start_date', startDate);
cart.setVariable(item, 'title', title);
cart.setVariable(item, 'employment_type', employeeType);
cart.setVariable(item, 'work_location', workLocation);
cart.setVariable(item, 'manager', manager_name);
cart.setVariable(item, 'cost_center', costCenter);
cart.setVariable(item, 'description', description);
 
//End dates are usually blank
if (endDate && endDate !== '[not available]') {
    cart.setVariable(item, 'end_date', endDate);
}

View solution in original post

@Aaron25  please check my logic and If it works for you ..please mark it as accepted as solution

View solution in original post

4 REPLIES 4

Brad Bowman
Kilo Patron
Kilo Patron

To isolate each variable value you can split the incident description on ':', then you'll have an array of six members that you can trim and assign 1,3, and 5 to the respective variables in the Cart API or whatever you're using to create the Request.

var incDesArr = current.description.split(':');
var fname = incDesArr[1].trim();
var pname = incDesArr[3].trim();
var sdate = incDesArr[5].trim();

 

Mani A
Tera Guru

parseIncidentAndUpdateCatalogItem('incident_sys_id', 'catalog_item_sys_id');


function parseIncidentAndUpdateCatalogItem(incidentSysId, catalogItemSysId) {
// Initialize variables
var fullName = '';
var preferredName = '';
var startDate = '';


var grIncident = new GlideRecord('incident');
if (grIncident.get(incidentSysId)) {

var description = grIncident.getValue('description');

// Extract values using regex
var nameMatch = description.match(/Full Name\s*:\s*(.*)/);
var preferredNameMatch = description.match(/Preferred Name\s*:\s*(.*)/);
var startDateMatch = description.match(/Start Date\s*:\s*(.*)/);

if (nameMatch) {
fullName = nameMatch[1].trim();
}
if (preferredNameMatch) {
preferredName = preferredNameMatch[1].trim();
}
if (startDateMatch) {
startDate = startDateMatch[1].trim();
}

/
gs.info('Full Name: ' + fullName);
gs.info('Preferred Name: ' + preferredName);
gs.info('Start Date: ' + startDate);


var grCatalogItem = new GlideRecord('sc_req_item');
if (grCatalogItem.get(catalogItemSysId)) {
grCatalogItem.setValue('u_full_name', fullName); // Assuming custom field 'u_full_name' exists
grCatalogItem.setValue('u_preferred_name', preferredName); // Assuming custom field 'u_preferred_name' exists
grCatalogItem.setValue('u_date', startDate); // Assuming custom field 'u_date' exists
grCatalogItem.update();
} else {
gs.error('Catalog item not found: ' + catalogItemSysId);
}
} else {
gs.error('Incident not found: ' + incidentSysId);
}
}

 

@Aaron25  please check my logic and If it works for you ..please mark it as accepted as solution

Aaron25
Tera Guru

Thanks Everyone for the help. Was able to do this by doing the below. I added exceptions to the array as our name formats are very dynamic for some reason that we get from our onboarding team. Code might be a little messy but was able to get around 50 different test cases with it.

 

if (typeof window == 'undefined') {
    CreateRequestNow();
}

function CreateRequestNow() {
    var message = '';
    var sysidOfItem = 'SYSID'; // SysID of the catalog item
    var cartId = GlideGuid.generate(null);
    var cart = new Cart(cartId);
    var item = cart.addItem(sysidOfItem);

      var description = current.description;
 
var fullNameMatch = description.match(/Full Name:\s*([^\n]*(?:\([^\)]*\))?)(?=\s*Preferred First Name|$)/);
var fullName = fullNameMatch ? fullNameMatch[1].trim() : '';


// Extract Preferred First Name from the incident description(?!.*Full Name)
var prefNameMatch = description.match(/Preferred First Name\s*([A-Za-z]+)/);
var prefName = prefNameMatch ? prefNameMatch[1].trim() : '';

// Extract Start Date from the incident description
var startDateMatch = description.match(/Start Date:\s*((\d{1,2}\/\d{1,2}\/\d{4})|(\w+\s\d{1,2}(?:st|nd|rd|th)?,?\s\d{4}))/);
var startDate = startDateMatch ? startDateMatch[1].trim() : '';

// Extract End Date from the incident description
var endDateMatch = description.match(/End Date \(if applicable\):\s*(\d{4}\s\d{2}\s\d{2}|\[not available\])/);
var endDate = endDateMatch ? endDateMatch[1].trim() : '';
 
 
 
var titleMatch = description.match(/Title:\s*([\w\s,&-]+)(?=\s*Employment Type:)/);
var title = titleMatch ? titleMatch[1].trim() : '';

// Extract Employment Type from the incident description
var emptypeMatch = description.match(/Employment Type:\s*([\w\s\-\(\)\/]+)(?=\s*Work Location:)/);
var employeeType = emptypeMatch ? emptypeMatch[1].trim() : '';

// Extract Work Location from the incident description
var workLocationMatch = description.match(/Work Location:\s*([\w\s\-\&]+)(?=\s*Manager Name:)/);
var workLocation = workLocationMatch ? workLocationMatch[1].trim() : '';

// Extract Manager Name from the incident description
var managerMatch = description.match(/Manager Name:\s*([\w\s]+)(?=\s*Cost Center:)/);
var manager_name = managerMatch ? managerMatch[1].trim() : '';

// Extract Cost Center from the incident description
var costCenterMatch = description.match(/Cost Center:\s*(\d+)/);
var costCenter = costCenterMatch ? costCenterMatch[1].trim() : '';


// Set variables for the catalog item
cart.setVariable(item, 'full_name', fullName);
cart.setVariable(item, 'pref_name', prefName);
cart.setVariable(item, 'start_date', startDate);
cart.setVariable(item, 'title', title);
cart.setVariable(item, 'employment_type', employeeType);
cart.setVariable(item, 'work_location', workLocation);
cart.setVariable(item, 'manager', manager_name);
cart.setVariable(item, 'cost_center', costCenter);
cart.setVariable(item, 'description', description);
 
//End dates are usually blank
if (endDate && endDate !== '[not available]') {
    cart.setVariable(item, 'end_date', endDate);
}