Create contact using inbound email action

Community Alums
Not applicable

Hi, I'm trying to use an inbound action to create a new customer contact if one does not already exist for the user submitting the case.  Here is the script I've been working on to try and accomplish this:

 

var gr = new GlideRecord('customer_contact');
	gr.addquery('email', email.body.ss_email); //check to see if contact exists
	gr.query();
	
	if(!gr.next()){ //if contact doesn't exist, then determine if the company/domain is an existing account
		var domain = email.body.ss_email.substring(email.body.ss_email.indexOf('@'));
		var ckAccount = new GlideRecord('customer_contact');
		ckAccount.addQuery('email', domain);
		ckAccount.query();
				
		if(ckAccount.next()){ //if an acccount exists for this customer, create a customer contact and asssociate with that                                   //account
			var newContact = new GlideRecord('customer_contact');
			newContact.initialize();
			newContact.user_name = email.body.ss_email;
			if(email.body.ss_fn != null){
			newContact.first_name = email.body.ss_fn;
			}
			if(email.bodd.ss_ln != null){
				newContact.last_name = email.body.ss_ln;
			}
			newContact.email = email.body.ss_email;
			newContact.account = ckAccount.account;
			newContact.insert();
		}
	}
1 ACCEPTED SOLUTION

Please go back to using addQuery and not get.

Not sure why it was changed when we want to simulate what you've been using, heh.

And as mentioned before, if that doesn't work, please use an email address in the form of 'example@example.com' and if that works and doesn't result in 245 records, then you'll know that it's not a bug and is an issue with you needing to use string.

You'd still want to use log troubleshooting entries on the "domain" once you see that this is working because you need to ensure that whatever domain is set to, is string, and is correct.

We will beat this thing! 🙂


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

12 REPLIES 12

Community Alums
Not applicable

Hi @Allen A 

I've reduced my code to the below and still getting 245 results, the input to the query is a the sender's email address pulled from the email record. 

 

gs.info("***DEBUG - This is the ss_email within email body: " + email.from);
	var gr = new GlideRecord('customer_contact');
	gr.get('email', email.from); //check to see if contact exists
	gr.query();
	
	if(gr.next()){ 
		gs.info("***DEBUG:  Inside of first If statement, gr.next() evaluated to true");
		gs.info('Rec Count= '+ gr.getRowCount());
		gs.info("***DEBUG: gr.name = " + gr.first_name + " " + gr.last_name);
		gs.info("***DEBUG: gr.user_name = " + gr.user_name);
		gs.info("***DEBUG: gr.email = " + gr.email);
        }

Please go back to using addQuery and not get.

Not sure why it was changed when we want to simulate what you've been using, heh.

And as mentioned before, if that doesn't work, please use an email address in the form of 'example@example.com' and if that works and doesn't result in 245 records, then you'll know that it's not a bug and is an issue with you needing to use string.

You'd still want to use log troubleshooting entries on the "domain" once you see that this is working because you need to ensure that whatever domain is set to, is string, and is correct.

We will beat this thing! 🙂


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Community Alums
Not applicable

HI @Allen A ,

Thanks for sticking with me, we finally got this working. I followed your advice and went back to addQuery(). After some thought I realized it was returning the entire contact table, this implied it was passing an empty string to the query.  It occurred to me that the email.from might not be a string type so I used the toString() function to convert it and voila, it returned zero results and the rest of my script began to execute.

I then uncovered one other flaw in my code. The query to determine if other contacts, with the same email domain, existed in the contacts table wasn't returning any results when I knew it should find two for this particular query. 

A quick mod to match on CONTAINS fixed this issue.  I also, out of precaution, used toString() to load all of the newContact object variables and it worked well.

Please find my complete code below with all the debugging statements I used to get there. I hope this helps others that have similar needs.

	var sender = email.from.toString();
	gs.info("***DEBUG - This is the sender: " + sender);
	var gr = new GlideRecord('customer_contact');
	gr.addQuery('email', sender); //check to see if contact exists
	gr.query();
	var domain = sender.substring(sender.indexOf('@')+1);
	gs.info("***DEBUG: domain of sender: " + domain);
	
	if(gr.next()){ //if contact doesn't exist, then determine if the company/domain is an existing account
		gs.info("Contact found: " + gr.first_name + " " + gr.last_name);
	}else{
		gs.info("***DEBUG - No existing contact found, checking to see if account with use with email domain " + domain + " exists.");
		var ckAccount = new GlideRecord('customer_contact');
		ckAccount.addQuery('email', 'CONTAINS', domain);
		ckAccount.query();
				
		if(ckAccount.next()){ //if an acccount exists for this customer, create a customer contact and asssociate with that                                   //account
			gs.info("***DEBUG: User, " + ckAccount.first_name + " " + ckAccount.last_name + " with email domain " + domain + "found." );
			gs.info('Nubmer of users with this email domain found : ' + ckAccount.getRowCount());
			gs.info("***DEBUG: Account value from ckAccount query:" + ckAccount.account);
 
			var newContact = new GlideRecord('customer_contact');
			newContact.initialize();
			newContact.user_name = sender;
			if(email.body.ss_fn != null){
			newContact.first_name = email.body.ss_fn.toString();
			}
			if(email.bodd.ss_ln != null){
				newContact.last_name = email.body.ss_ln.toString();
			}
			newContact.email = sender;
			newContact.account = ckAccount.account.toString();
			newContact.insert();
		}else{
			gs.info("No contact with email domain" + domain + "found.");
		}
	}