Insert Record In Custom Table - Inbound E-Mail Action

-Andrew-
Kilo Sage

Hi there,

Is there a way of creating a record in a custom table when the recipient(s) to the inbound email are "****@******.com"?

I have been reviewing the ServiceNow Docs and community but can't find a way to do this.

I have created a new inbound email action called "Inbound Email Lookup - DPU" but this still generates an Incident and not a record in the chosen table.

Thanks for your help.

Andrew

1 ACCEPTED SOLUTION

Kieran Anson
Kilo Patron

Hi Andrew,

You're on the right tracks with an inbound action however it sounds like you've not set the order of your new action and therefore the OOB create incident action is being used.

Set the execution order lower than 100 (e.g 70) so it processed your action prior to the incident one. Also ensure you have checked 'stop processing'

If my reply helped with your issue please mark helpful 👍 and correct if your issue is now resolved.
By doing so you help other community members find resolved questions which may relate to an issue they're having.

View solution in original post

16 REPLIES 16

Hi Kieran, just reviewing the email logs now and can see the below;

 

"Inbound Email Lookup - DPU : did not create or update u_dpu_requests using current"

"Email is classified as new for triggers execution"

"Stop processing detected after executing script: Inbound Email Lookup - DPU"

find_real_file.pngI attach this screenshot for reference?

 

Any ideas please?

Hey Andrew,

This usually occurs when incorrect syntax is used. Could you post the script portion of your inbound action?

Sure thing:

 

processInboundEmailLookup();

function processInboundEmailLookup(){
	//Query for the inbound email log record
	var inboundEmail = new GlideRecord('sys_email');
	inboundEmail.addQuery('uid', email.uid);
	inboundEmail.query();
	if(inboundEmail.next()){
		var foundOne = false;
		var iel = new GlideRecord('u_inbound_email_lookup');
		iel.addQuery('u_active', true);
		iel.orderBy('u_order');
		//Match for lookup records with a source email value first
		iel.orderByDesc('u_email');
		iel.query();
		while(iel.next() && !foundOne){
			//Check for matching lookup records
			if(GlideFilter.checkRecord(inboundEmail, iel.u_conditions)){
				//If source email is found on lookup record
				if(!iel.u_email.nil()){
					//Check for matching source email address
					if(iel.u_email == email.origemail || iel.u_email == email.from){
						processLookup(iel, inboundEmail);
						foundOne = true;
						break;
					}
				}
				else{
					processLookup(iel, inboundEmail);
					foundOne = true;
					break;
				}
			}
			//Continue processing lookup records if match not found
		}
	}
}

//Process the inbound email lookup rule
function processLookup(rule, inboundEmail){
	var newRec;
	//If request item, order via cart
	if (rule.u_table == 'sc_req_item'){
		var cart = new Cart();
		cart.addItem(rule.u_catalog_item);
		newRec = cart.placeOrder();
		gs.sleep(3000); //Wait to make sure the request item is created
		//Query for the item and set as created record
		var recSysId = newRec.sys_id.toString();
		newRec = new GlideRecord("sc_req_item");
		newRec.addQuery("request", recSysId);
		newRec.query();

		
		//Query to see if user exists and add email to watch list if not
		var usr = new GlideRecord('sys_user');
		usr.addQuery('email', email.origemail);
		usr.query();
		if(usr.getRowCount() == 0){
			newRec.watch_list = email.origemail;
		}
	}
	//Standard record initialization for other task types
	else{
		newRec = new GlideRecord(rule.u_table);
		newRec.initialize();
	}
	
	//Apply any template to created record
	if (!rule.u_template.nil()){
		newRec.applyTemplate(rule.u_template.name);
	}
	
	var finalBody = email.body_text;
	
	//gs.info('NEW BODY ORIGINAL MESSAGE: ' + finalBody.split(/[\r\n]+-----Original Message-----/)[0]);
	gs.info('NEW BODY FROM: ' + finalBody.split(/[\r\n]+From:/)[0]);
	//gs.info('NEW BODY FROM COMPLEX: ' + finalBody.split(/[\r\n]+  _____  [\r\n]+From:/)[0]);
	var body = finalBody.split(/[\r\n]+From:/)[0];
	//Eval to apply email body and short description to the appropriate fields
	//eval("newRec." + rule.u_email_body_mapping + " = 'Received from: ' + email.origemail + ':  ' + email.body_text;");
	eval("newRec." + rule.u_email_body_mapping + " = 'Received from: ' + email.origemail + ':  ' + body;");
	eval("newRec." + rule.u_email_subject_mapping + " = email.subject;");
	
	//Apply other table-specific settings
	if ((rule.u_table == 'u_dpu_requests') || (rule.u_table == 'u_facilities') || (rule.u_table == 'u_hr')){
		newRec.caller_id = gs.getUserID();
		newRec.contact_type = 'email';
		if (email.body.assign != undefined)
			newRec.assigned_to = email.body.assign;
		
		if(email.importance != undefined)
			if (email.importance == 'High')
			newRec.impact = 1;
		
		//Query to see if user exists and add email to watch list if not
		usr.addQuery('email', email.origemail);
		usr.query();
		if(usr.getRowCount() == 0){
			newRec.watch_list = email.origemail;
		}
	}
	
	//Apply any script to created record
	if (!rule.u_script.nil()){
		try{
			eval(rule.u_script);
		}
		catch(e){}
	}
	
	//Finish record insert
	newRec.update();
	inboundEmail.instance = newRec.sys_id;
	inboundEmail.target_table = rule.u_table;
	inboundEmail.update();
	//Stop processing lookup rules
	event.state= 'stop_processing';
	
	//Copy attachments from email to created record
	GlideSysAttachment.copy('sys_email', inboundEmail.sys_id, newRec.getTableName(), newRec.sys_id.toString());
}

Thanks Andrew,

This is a rather lengthy script, before I do anything what are you wanting to achieve with the email coming in? Just create a record on a table?

The script attached performs various lookups and conditions for a request based process (creates a cart, adds items and orders). If you're only looking to create a simple table record similar to how you would for an incident, I can provide some simpler logic.