A query in Inbound email actions

devservicenow k
Tera Contributor

I am Creating Inbound email action and creating case Record.

After creating if the inbound email Domain is (eg,gmail.com).

the particular Account record with the (gmail.com ) to be selected and stored in Account field in that case Record, i have created the Script but it is not getting updated in the Case record, but in the Logs it is currectly giving the output.

suggest me what changes do i need to do 

 

 

1 ACCEPTED SOLUTION

Please check the below code:-

(function runAction( /*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {
current.u_insp_email = email.from;
var domain = email.from.split('@')[1];
gs.info("Name is: " + domain);

var gr = new GlideRecord('customer_account');
gr.addEncodedQuery('u_emailidCONTAINS' + domain);

gr.query();

if (gr.next()) {
current.account = gr.sys_id;;
current.update();
gs.info("Name is2222: " + gr.name);
gs.info("Name is111: " + current.account);

if(gr.contact==''){
var update = new GlideRecord('sys_user')
update.addQuery('email',email.from)
update.query();
if(update.next())
{
gr.contact=update.sys_id;
gr.update();
}
else
{
var create = new GlideRecord('sys_user')
create.initialize();
create.email=email.from;
create.name=email.from;
create.user_name=email.from;
create.insert();
gr.contact=create.sys_id;
gr.update();
}
}
}

//current.u_insp_email = domain;
})(current, event, email, logger, classifier);

 

Please mark my answer as correct based on Impact. 

View solution in original post

14 REPLIES 14

tried with this also but it is not getting saved in the accounts

 

 

hello,

 

I can see the i beside the field which means there is a record. i beleive as you are not setting user id from the script and user id being the display field hence you see the field empty but the value is actually there click on the i icon.

 

Please mark my answer as correct based on Impact.

as your suggestion the user id is created

but I need to set the user id from the script  automatically

Hello,

 

For the  time being you can set the email id itself in the userid as that is the unique value

 

(function runAction( /*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {
current.u_insp_email = email.from;
var domain = email.from.split('@')[1];
gs.info("Name is: " + domain);

var gr = new GlideRecord('customer_account');
gr.addEncodedQuery('u_emailidCONTAINS' + domain);

gr.query();

if (gr.next()) {
current.account = gr.sys_id;;
current.update();
gs.info("Name is2222: " + gr.name);
gs.info("Name is111: " + current.account);

if(gr.contact==''){
var create = new GlideRecord('sys_user')
create.initialize();
create.insert();
create.email=email.from;
create.name=email.from;
create.user_name=email.from;
gr.contact=create.sys_id;
gr.update();
}
}

//current.u_insp_email = domain;
})(current, event, email, logger, classifier);

 

Please mark my answer as correct based on Impact 

 not setting automatically in the contact