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

Thank you both for your suggestions.  I was able to make some progress; I found that email.body.ss_email was not retuning the sender's address so I substituted in email.from, which works.  Now, I have run into a new issue I'm having trouble getting around: 

After I query the customer_contact table to see if there is an existing contact it does not appear to return any.  I determined this by using gs.info(...gr.first_name...) to see if it was actually finding a match but it outputs 0, a falsey value, indicating no match, or so I think.

However, the condition in my first if block (!gr.next) evaluates to false, preventing the rest of my script from executing.

Have you seen a situation like this before? What do you recommend as a next step to troubleshoot?

gs.info("***DEBUG - This is the email.from: " + email.from);
	var gr = new GlideRecord('customer_contact');
	gr.addquery('email', email.from); //check to see if contact exists
	gr.query();
	gs.info("***DEBUG: gr.name = " + gr.first_name + " " + gr.last_name);

		if(!gr.next()){ //if contact doesn't exist, then determine if the company/domain is an existing account
		gs.info("***DEBUG:  Inside of first If statement, !gr.next() evaluated to true");
		var domain = email.from.substring(email.from.indexOf('@')+1);
		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');
			gs.info("***DEBUG: inside the ckAccount.next() If Statement");
			newContact.initialize();
			newContact.user_name = email.from;
			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();
		}
	}else{
              gs.info("***DEBUG:  Else, !gr.next() evaluated to false");
        }

Hello,

I'm glad my replies above have been helpful as well as the included log examples.

It's not very clear what you're meaning when you say:

"After I query the customer_contact table to see if there is an existing contact it does not appear to return any.  I determined this by using gs.info(...gr.first_name...) to see if it was actually finding a match but it outputs 0, a falsey value, indicating no match, or so I think.

However, the condition in my first if block (!gr.next) evaluates to false, preventing the rest of my script from executing."

You would only be able to check if there's a record or not, by using gr.next(), then if so, you have access to the record. So your log statement:

gs.info("***DEBUG: gr.name = " + gr.first_name + " " + gr.last_name);

		if(!gr.next()){ 

Which is before the gr.next()...is not the correct place for that and wouldn't return an appropriate data.

So if it's not entering your false if block, indicated by your (!gr.next()), then it means there is a user with that email in your instance.

Example:

var gr = new GlideRecord('sys_user');
gr.addQuery('email', 'itil@example.com');
gr.query();
gs.info("Name is: " + gr.name); //this simulates where you had your incorrect log statement
if (gr.next()) {
gs.info("Name is: " + gr.name); //this simulates after you've used gr.next()
}

Result?

find_real_file.png

You can easily double-check by querying this and seeing yourself on the table?

This is where it's nice to have you troubleshoot things on your end, through these log statements, and then you can go and look within your instance.

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


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

Hi,

Thanks for marking replies as Helpful.

Hopefully my latest reply above, makes sense.

Let me know if you're able to understand what I'm talking about!

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 ,

 

Thank you for the additional guidance. I believe I am encountering a bug. I doubled checked my table (customer_contact) to ensure a contact with this email address did not already exist, it did not. I added gs.info('Rec Count= '+ gr.getRowCount()); and found that 245 results were being returned from my query. I then set up a loop to output the user name and email address of the results to the log and they are all records with no correlation to the query parameters at all. At this point, I believe my next step is to submit a HI ticket.

Hello,

There isn't a bug, it's most likely an issue with this:

var domain = email.body.ss_email.substring(email.body.ss_email.indexOf('@'));

Have you done proper troubleshooting (as we've discussed a few times in this chain) and seen what that value is? And is the value in string form?

You had already had the above, so I assumed that was properly setup.

But in GlideRecord, if a query is invalid, it will ignore it, which seems to be the case here.

Please do a very simple test and replace "domain" in your GlideRecord query with a literal email address like 'example@example.com' and you'll see the issue not happening with 245 results coming back.

The overall goal for me here was to help point you to troubleshooting and how to do log entries, etc. Please use them and check things (like "domain", etc.) when you see problems.

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

 


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