
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2022 01:12 PM
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();
}
}
Solved! Go to Solution.
- Labels:
-
Customer Service Management

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2022 02:06 PM
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!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2022 01:35 PM
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:
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2022 01:43 PM
Hi
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2022 05:46 PM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2022 02:44 PM
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!