Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to set the target field in Email logs with Request/RITM number,when request is created through inbound email action

robinpaul
Kilo Contributor

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

8 REPLIES 8

tylerjones
Tera Contributor

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();

 
}

 

kuba4
Kilo Expert

Thanks!

I'll test under my instance.

 

 

Community Alums
Not applicable

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";

 

============================

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();

}
}