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

Allen Andreas
Administrator
Administrator

Hello,

What does your troubleshooting log statements say? I don't see any here and unsure if you're just coding in the dark. What values are you getting from the email? What if statements are being entered or not...?

What errors are you seeing?

What ends up happening?

Unfortunately, you didn't provide any of this helpful information and instead pasted your code...

With that said, please also use the appropriate forum feature: "Insert/Edit code sample" so that your code is easier to read and organized:

find_real_file.png

Please mark reply as Helpful/Correct, if applicable. Thanks!


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

Community Alums
Not applicable

Hi @Allen A , 

Pardon my ignorance, it's my first time posting here.  I'm receiving the following error when I look in the Emails log:

Create New Customer Contact : did not create or update customer_contact using current

So from what I can tell, the condition is being met for the inbound action to execute but the script is unable to create a new contact record.

I tried using gs.log() to debug my code but can't determine what log these are being written to.  I've only been using SN for a few months and new to JavaScript as well. Any insight is greatly appreciated.

 

Hello,

It's always a good idea to state what you've mentioned in your original post. As I mentioned before, more information is better for us to help you. How would we know you're new to ServiceNow? New to JavaScript, etc.?

With that said, I'll give a small example and you can take it from there:

gs.info("***DEBUG - This is the ss_email within email body: " + email.body.ss_email);
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
gs.info("***DEBUG - No customer contact found as we're now inside this if statement");

You would then be able to navigate to: System Logs > System Log > All and see your debug entries showing up with a source of: *** Script

This should help you review things more fully and the main advice is to review all of your supposed email body variables you think you're retrieving and see what their values are.

System log documentation: https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0721331

Please mark reply as Helpful/Correct, if applicable. Thanks! 


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

favian1
Kilo Sage

Hello Mike,

You can find the log under System Logs > System Log > All. 

Your code looks pretty solid, but it's difficult to determine where it's failing. See if you can add some log statements, and find them in the log. If you don't figure it out yourself, I will be glad to help out.

Thanks!