How to set the target field in Email logs with Request/RITM number,when request is created through inbound email action
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2017 04:29 AM
Hello All,
I have created an inbound email action to Create a Request and Requested Item in Catalog when an email is received into servicenow. I have set Target Table as: Shopping Cart(sc_cart)
The Script work well and it creates a Request and Request Item. But my When i see the email logs from System Logs-->Emails and open the received email. Here the Target field is populating as "Shopping Cart: DEFAULT"
My Question is, Is there a way that i can populate the target field with RITM Number in the email log.
inbound email action script :
createRequest();
function createRequest() {
var cart = new Cart(); //calling the cart API
var item = cart.addItem('10a2f5dfc6112276018db58138c7a1e0'); //sys_id of the catalog item I want to fire
cart.setVariable(item, 'request_short_description', email.subject.toString());
cart.setVariable(item, 'request_description', email.body_html);
cart.setVariable(item, 'short_title', email.subject.toString());
cart.setVariable(item, 'screen_wipes','1');
cart.setVariable(item, 'copier_reams','1' );
// set the requested for
var gr = new GlideRecord("sys_user");
gr.addQuery("email", email.from);
gr.query();
if (gr.next()) {
var cartcartGR = cart.getCart();
cartGR.requested_for = gr.sys_id;
cartGR.update();
}
var rc = cart.placeOrder(); //this launches the catalog item, and creates a request object. rc = the request object
updateRITM(rc.sys_id); //call a function immediately to update the ritm. This must be a nested function, otherwise inbound actions get weird.
//also, we're passing the sys_id of the request so we know what RITM to grab.
}
function updateRITM(req){
var ritm = new GlideRecord('sc_req_item');
ritm.addQuery('request', req); //req is what we passed from the previous function. the sys_id of the request.
ritm.query();
while (ritm.next()){
ritm.u_customer = gs.getUserID(); //my ritm table separately tracks its own customer, since I don't use Request
ritm.description = email.body_text; //we're still in the inbound action so why not exploit?
ritm.priority = email.body.priority; //good example of how to exploit email body variables
ritm.short_description = email.subject;
ritm.update();
}
}
// current.setAbortAction(true);
event.state="stop_processing";
Any Pointers would be helpful ! Thanks in advance for any assistance.
Message was edited by: Robin paul
- Labels:
-
Best Practices
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2019 07:50 AM
Hey Kuba,
I was able to. do this via glide record update.
var emailGR = new GlideRecord("sys_email");
emailGR.addQuery("sys_id", sys_email.sys_id);
emailGR.query();
if(emailGR.next()){
emailGR.instance = incSysID;
emailGR.target_table = "incident";
emailGR.update();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2019 01:50 AM
Thanks!
I'll test under my instance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2019 02:09 AM
please use below code; Refer the highlighted
createRequest();
function createRequest() {
var cart = new Cart(); //calling the cart API
var item = cart.addItem('10a2f5dfc6112276018db58138c7a1e0'); //sys_id of the catalog item I want to fire
cart.setVariable(item, 'request_short_description', email.subject.toString());
cart.setVariable(item, 'request_description', email.body_html);
cart.setVariable(item, 'short_title', email.subject.toString());
cart.setVariable(item, 'screen_wipes','1');
cart.setVariable(item, 'copier_reams','1' );
// set the requested for
var gr = new GlideRecord("sys_user");
gr.addQuery("email", email.from);
gr.query();
if (gr.next()) {
var cartcartGR = cart.getCart();
cartGR.requested_for = gr.sys_id;
cartGR.update();
}
var rc = cart.placeOrder(); //this launches the catalog item, and creates a request object. rc = the request object
updateRITM(rc.sys_id); //call a function immediately to update the ritm. This must be a nested function, otherwise inbound actions get weird.
//also, we're passing the sys_id of the request so we know what RITM to grab.
//Add target to email
if(rc != undefined){
var gr = new GlideRecord("sc_req_item");
gr.addQuery("request", rc.sys_id);
gr.query();
if(gr.next()){
sys_email.target_table = "sc_req_item";
sys_email.instance = gr.sys_id;
sys_email.update();
}
}
}
function updateRITM(req){
var ritm = new GlideRecord('sc_req_item');
ritm.addQuery('request', req); //req is what we passed from the previous function. the sys_id of the request.
ritm.query();
while (ritm.next()){
ritm.u_customer = gs.getUserID(); //my ritm table separately tracks its own customer, since I don't use Request
ritm.description = email.body_text; //we're still in the inbound action so why not exploit?
ritm.priority = email.body.priority; //good example of how to exploit email body variables
ritm.short_description = email.subject;
ritm.update();
}
}
event.state="stop_processing";
============================
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2024 12:10 PM
This solved the issue. Thanks, @Community Alums .
Note that I made the following changes so that it's the REQ that's the target record and not the RITM.
if(rc != undefined){
var gr = new GlideRecord("sc_request");
gr.addQuery("sys_id", rc.sys_id);
gr.query();
if(gr.next()){
sys_email.target_table = "sc_request";
sys_email.instance = gr.sys_id;
sys_email.update();
}
}