Assigning a reference field in an inbound email action

wwinter
Kilo Contributor

So, what I'm doing is extracting bits of an email to fill various fields for creating an incident from an inbound email. The case here is that someone submits a ticket to helpdesk for someone that called them. 

I'm able to set the description and classification using the below code, but any field that is a reference type seems to fail. My question is, whats the trick to assigning data to a reference field? I'm testing this with valid data that exist for the fields, so its not that wacky inputs are being rejected. I also know I'm extracting the text I want(tested it in a shell). So my issue is with setting fields like current.location, current.assigned_to, and current.assignment_group. 

Sorry if I'm not very clear about this, I'm new to servicenow & still trying to wrap my head around its nuances. 

//Extracts a substring between two parts of a string
function extractData(data, startStr, endStr)
{
subStrStart = data.indexOf(startStr) + startStr.length;
return (data.substring(subStrStart,subStrStart + data.substring(subStrStart).indexOf(endStr))).trim();
}

if((email.origemail).includes("tpcgrp.com"))
{
	current.location = extractData(finalDescription,"Location:", "Assign To:");
	current.assignment_group = extractData(finalDescription,"Assignment Group:", "Assign");
	current.assigned_to = extractData(finalDescription,"Assigned To:", "Classification:");
	current.u_classification = extractData(finalDescription,"Classification:","Message:");
	current.description= finalDescription.substring(finalDescription.indexOf("Message:")); 
}

finalDescription is just the body of the email after a boilerplate warning is truncated from the end. 

1 ACCEPTED SOLUTION

Mark Roethof
Tera Patron
Tera Patron

Hi there,

The value you are setting in the reference field, is now a string. So do a GlideRecord query, to look up that string, and retrieve the sys_id. Then simply pass the sys_id to the reference.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

View solution in original post

5 REPLIES 5

Mark Roethof
Tera Patron
Tera Patron

Hi there,

The value you are setting in the reference field, is now a string. So do a GlideRecord query, to look up that string, and retrieve the sys_id. Then simply pass the sys_id to the reference.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

wwinter
Kilo Contributor

I think you've set me in the right direction, but what i'm currently trying must not be quite right as the field still remains empty. Is using get possible here or is there a better alternative? Thanks for the help!

	var loc = extractData(finalDescription,"Location:", "Assign To:");
	var glide = new GlideRecord('sys_user');
	var returnValue = glide.get(location, loc);
	current.location=returnValue;

find_real_file.png

edit: Heres another attempt of mine, but it too fails to populate the location field(started just trying to get one field filled before bothering with all of them). 

	var glide = new GlideRecord('sys_user');
	glide.addQuery('location', loc);
	glide.query();
	if(glide.next()){
		current.location = glide.sys_id;
	}

So did you also debug if you are actually getting the correct value? For example, what does below give you:

extractData(finalDescription,"Location:", "Assign To:")

Is that indeed a valid location name? Or is het a name with spaces which will not work? Or is it something totally different?

The first script in your last post, I can't test it: location is not defined.
Your second script you are attempting:

current.location = glide.sys_id

Though glide.sys_id is the sys_id of the user, not of the location. So that can't work. You are after the sys_id of the location? So shouldn't you do a query on location table instead of user table?

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

I ended up making another post more directly asking how to properly query a glide record:   https://community.servicenow.com/community?id=community_question&sys_id=9c2feefadb8a40104819fb243996...

As it turned out I had been doing a few things wrong, like referring to the wrong table, and having the wrong location field being displayed for incidents. Heres a snippet of what I ended up using for those encountering similar difficulties. For some context, finalDescription contains the modified body of an email sent by a group manning help desk phones in off hours. They submit tickets via email, but before they had no format to follow, so we gave them one, and I parse the email for the relevant information. Those strings I extracted can't just be dumped right into reference fields, so what we have below is the work around. 

	//Sets Assignment Group field
	var asg = extractData(finalDescription,"Assignment Group:","Assigned To:");
	var glide3 = new GlideRecord('sys_user_group');
	glide3.addQuery('name', asg);
	glide3.query();
	if(glide3.next())
		current.assignment_group = glide3.getValue("sys_id");
	
	//Sets Assigned To field
	var asi =  extractData(finalDescription,"Assigned To:", "Classification:");
	var asi = asi.split(" ");
	var glide4 = new GlideRecord('sys_user');
	glide4.addQuery('first_name', asi[0]);
	glide4.addQuery('last_name', asi[1]);
	glide4.query();
	if(glide4.next())
		current.assigned_to = glide4.getValue("sys_id");