Unable to set "Requested_For" from email body in RITM via inbound action

DEEPAK KUMAR SI
Tera Contributor

Hi Dev's.

I am working on a requirement were I have to capture an email from a specific email id via inbound action and then create an RITM with 1 associated TASK.

Field values will be captured from email body and both RITM & TASK fields should have same fields values.

 

Sol: I tried making an dummy catalog item and attached a workflow, (RITM & TASK creation issue solved)

Issue : I am unable to Set RITM & task "requested_for" field from email body which will be dynamic

My Inbound Action Script :

-----------------------------------------------------------------------------------------------------------

(function runAction(/*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {
 
//Implement email action here
var cartID = GlideGuid.generate(null);
var cart = new Cart(cartID);
 
var item = cart.addItem('575c738533f1b510eb275f49bd5c7bca');
 
cart.setVariable(item, 'sn_req_app', '1');
    cart.setVariable(item, 'sn_req_details', 'Sample text');
 
    var rc = cart.placeOrder();
 
var ritmRec = new GlideRecord("sc_req_item");
ritmRec.addQuery("request", rc.sys_id);
ritmRec.query();
if (ritmRec.next()) {
ritmRec.short_description = email.subject.toString();
ritmRec.comments = "received from: " + email.origemail + "\n\n" + email.body_text;
ritmRec.contact_type = "email";
 
 
//added logic for Requested For
if (email.body.name != undefined) {
var gr = new GlideRecord("sys_user");
gr.addQuery('name', email.body.name);
gr.query();
if (gr.next()) {
ritmRec.requested_for = gr.sys_id;
gs.log('abcd'+ gr.sys_id);
}
ritmRec.update();
// current.requested_for = email.body.name;
// gs.log(email.body.name);
}
else {
var stringbody = email.body_text.replace(/\n/g, "<br>");
var stringbody1 = stringbody.replace(/ /g, '');
var index = stringbody1.search("Name");
var namestring = stringbody1.substr(parseInt(index), 20).split(":");
current.requested_for = namestring[1];
}
//Update target in email table, as it won't be updated as the record was inserted through cart API
if (rc !='') {
var email_rec = new GlideRecord('sys_email');
email_rec.get(sys_email.sys_id);
email_rec.instance = rc.sys_id;
email_rec.target_table = "sc_request";
email_rec.update();
}
 
})(current, event, email, logger, classifier);
5 REPLIES 5

Vishal Birajdar
Giga Sage

Hi @DEEPAK KUMAR SI 

 

Have you checked logs..?? are you getting logs...!!

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Vishal Birajdar
Giga Sage

Hi @DEEPAK KUMAR SI 

 

Have done some changes in your script 

 

cart.placeOrder();  // will return the sys_id of request & not ritm so you need to first glide record on Request table to set requested for  & then ritm.

 

(function runAction(/*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {
 
//Implement email action here
var cartID = GlideGuid.generate(null);
var cart = new Cart(cartID);
var item = cart.addItem('575c738533f1b510eb275f49bd5c7bca');
 
cart.setVariable(item, 'sn_req_app', '1');
cart.setVariable(item, 'sn_req_details', 'Sample text');
 
var rc = cart.placeOrder();   //we will get the sys_id of request
 
var ritmRec = new GlideRecord("sc_request"); //Glide on request table
ritmRec.addQuery("sys_id", rc.sys_id); //query with sys_id
ritmRec.query();
if (ritmRec.next()) {
ritmRec.short_description = email.subject.toString();
ritmRec.comments = "received from: " + email.origemail + "\n\n" + email.body_text;
ritmRec.contact_type = "email";
 
 
//added logic for Requested For
if (email.body.name != undefined) {
var gr = new GlideRecord("sys_user");
gr.addQuery('name', email.body.name);
gr.query();
if (gr.next()) {
ritmRec.requested_for = gr.sys_id;
gs.log('abcd'+ gr.sys_id);
}
ritmRec.update();
// current.requested_for = email.body.name;
// gs.log(email.body.name);
} 
}
else {
var stringbody = email.body_text.replace(/\n/g, "<br>");
var stringbody1 = stringbody.replace(/ /g, '');
var index = stringbody1.search("Name");
var namestring = stringbody1.substr(parseInt(index), 20).split(":");
current.requested_for = namestring[1];
}
//Update target in email table, as it won't be updated as the record was inserted through cart API
if (rc !='') {
var email_rec = new GlideRecord('sys_email');
email_rec.get(sys_email.sys_id);
email_rec.instance = rc.sys_id;
email_rec.target_table = "sc_request";
email_rec.update();
}
 
})(current, event, email, logger, classifier);

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Hi Vishal,

Problem remains same.

piyushsain
Tera Guru

instead of 

if (email.body.name != undefined) {
var gr = new GlideRecord("sys_user");
gr.addQuery('name', email.body.name);
gr.query();
if (gr.next()) {
ritmRec.requested_for = gr.sys_id;
gs.log('abcd'+ gr.sys_id);
 
Use :

 

if (email.from != undefined) {
var gr = new GlideRecord("sys_user");
gr.addQuery('email', email.from);
gr.query();
if (gr.next()) {
ritmRec.requested_for = gr.sys_id;
gs.log('abcd'+ gr.sys_id);
}

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,
Piyush Sain