Get email address from body of inbound email and set as Requested For?

na93
Mega Expert

Hi All,

I have an inbound request action, and what I am trying to do is get the the inbound action to check the inbound email for an Email Address within the body of the email itself. Then use this email address to set it as the 'Requested for" of the Request.

Now excuse me as I am completely new to development and Javascript so I'm probably a mile off but this is what I have so far, I'm not sure how I go about using the variable 'emails' to essentially check the user table and set this as the requester

createRequest();

function createRequest() {

//Create Request

var grRequest = new GlideRecord ("sc_request");

grRequest.initialize();

var content = email.body_text;  

var emails = content.match(/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/ig);  

gs.log('Email is ' + emails); NOTE: This prints the email address from the body of the email fine

var userObject = gs.getUser().getRecord().getValue('emails');

grRequest.requested_for.setDisplayValue(emails);

grRequest.short_description = email.subject.toString();

grRequest.description = "received from: " + email.origemail + "\n\n" + email.body_text;

var requestSysId = grRequest.insert();

//Create Request Item, substitute your requested for

//current.requested_for = '52aa1965db4ef2006de0f3731d9619e1';

current.requested_for.setDisplayValue('emails');

current.short_description = email.subject.toString();

current.description = "received from: " + email.origemail + "\n\n" + email.body_text;

//substitute your cat item

current.cat_item = '73a67d42db248300aefef3731d961917';

current.parent = requestSysId;

current.request = requestSysId;

current.insert();

//Workflow Trigger

var w = new Workflow();

wfSysId = w.getWorkflowFromName("Test");

w.startFlow(wfSysId, current, current.operation());

}

Any points or help would be much appreciated.

1 ACCEPTED SOLUTION

renaud3
Giga Contributor

it would be best to call a function doing the job.


Something like:



grRequest.requested_for = getRequestedForID(emails);




and create the new function after or inside your "createRequest" function:



function getRequestedForID(retrievedEmail){


var grUser = new GlideRecord("sys_user");


grUser.addQuery("email", retrievedEmail);


grUser.query();


if(grUser.next()){


return grUser.sys_id.toString();


}


else {


gs.log("Couldn't find user based on email: " + retrievedEmail + " in Inbound Email Action Create Request","Inbound Email log");


}


}


View solution in original post

19 REPLIES 19

Jaspal Singh
Mega Patron
Mega Patron

Hi Nabeel,



Could you kindly include below snippet to the inbound code. What we do is we look for the header   in the mail logs received as there we can find all received, sent mails & mail IDs.



var mail_addr = email.headers;


var srch = mail_addr.search('Reply-To:');


gs.log('Mail is ',srch);


var srch_to = mail_addr.search('To:');


var sub_str = mail_addr.substring(srch+9,srch_to).trim();




var sysID_user;


var gr = new GlideRecord('sys_user');


gr.addActiveQuery();


gr.addQuery('email', sub_str);


gr.query();


if(gr.next()){


sysID_user = gr.sys_id;


}



& wherever you want to set Requested for inbound replace it with sysID_user.



For instance grRequest.requested_for.setDisplayValue(emails); would become grRequest.requested_for=sysID_user;


Thanks Jaspal, will this pick up the email address even though it is in the body of the email itself?



Cheers,
Nabeel


Yes...as we are looking for address in the mail logs header section.



Thanks,



Jaspal Singh


+919022506309


ServiceNow SA
Kilo Guru

Use below code in inbound action :



var usr = '';   // You can use this line to set a default  


var mg = new GlideRecord('sys_user');  


if (mg.get('name', email.body.email)) {  


usr = mg.sys_id;  


}



And then set it accordingly



I would prefer   using Cart API for request creation..


I've tried both your solution Sri and Jaspal's and no joy i'm afraid, it is coming back as blank within the logs.



I think it's time for me to give up on this one as I've spent a lot of time trying things with no joy



The inbound email I was trying to handle is exactly this layout (Incase anyone can think of something else)



Please unblock www.google.com


Guardian user is AD\UID


[The following information is not authenticated by Guardian] Additional information:Please can you make this website available as I need this for my job


Request from John Doe john.doe@example.com



Note I have no way of changing anything within that comes inbound