Submitting a catalog item from an Inbound Action

Bernard Angeles
Giga Contributor

Hi all,

I have been tasked with triggering the submission of a catalog item when an email is received.

I don't have much of a coding background so I'm sure the problem is somewhere or something to do with my code...

Here is my current inbound action below:

Target table: sc_req_item

Action type: Record Action

Script:

createRequest();

function createRequest() {
	var cartId = GlideGuid.generate(null);
	var cart = new Cart(cartId);
	
	var item = cart.addItem('65fcbfee75642400af0e5269def2e168');
	
	cart.setVariable(item, 'sn_req_app', '1');
	cart.setVariable(item, 'sn_req_details', 'Sample text');
	
	var rc = cart.placeOrder();
}

A few other points:

  • There are no field actions, just the above script
  • I have ensured that in my email submission tests, the condition to trigger the inbound action is correct
  • The sys_id is pointing to an active test catalog item with only 2 variables both of which are non-mandatory
  • Global scope

 

What I have found is, when the email is received, I get the following mesage in the email log: "Information - Skipping 'inbound_action_name', did not create or update sc_req_item"

In testing I have also noticed that if I place a field action along with the script (for example, Short Description - From email - Subject) it will generate a blank RITM record with no Item attached.

 

Any help on this would be much appreciated!

Thank you,

Bernard  

 

1 ACCEPTED SOLUTION

That is all correct. Target table should be Request.

Change your script to look like this:

(function runAction( /*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {

        var cartId = GlideGuid.generate(null);
        var cart = new Cart(cartId);

        var item = cart.addItem('65fcbfee75642400af0e5269def2e168');

        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";
            ritmRec.update();
        }
        //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();
        }

View solution in original post

15 REPLIES 15

Bob20
Tera Contributor

I gave that a try thinking that the standard fields (not vars) would be set the same way on the form. Yet when I look at the fields in the REQ & RITM, both are empty. Was thinking that perhaps there might be something clearing them after setting, but can't see anything like that. Going to reach out to our local SN Support Rep.

Thanks for your guidance.

B.

Hi Michael, 

hope you've been well!

I've used this script before I'm sure but something this time is not working.  I've narrowed it down to just your script.  Here's my script and a screenshot. In my logs the script is not reaching line 8...seems to be hanging up on the "var rc = cart.placeOrder()".  I have no user criteria on the catalog item.  For now I've stripped out all variables, and none of the variables on the cat item are mandatory.  There is an associated workflow for the cat item and it has been published. 

See anything I'm missing?  thanks!

 

var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem('397c59971bc2a0504e73eb12b24bcbef');
gs.log('#### Create RITM from Oracle inbound action is running; line 6');
var rc = cart.placeOrder();
gs.log('#### Create RITM from Oracle inbound action is running; line 8');
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";
ritmRec.update();
}
//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.upd

find_real_file.png

Hi Michael,

yes very strange...I now have the script stripped down to just the bare bones from the docs...and am not able to get it to work in my personal instance.  Is there something about the emails I'm sending in?  They are being received by the instance, but again it's not reaching the 2nd log entry...it is logging the first log entry, so I know the script is running...which makes me think the "var rc = cart.placeOrder();" isn't working.  

 

(function runAction(/*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {

var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem('3c7fee85eb3323008f833c4fea887e45');//test cat item
gs.log('#### Create RITM from Oracle LFG_CMO@lfg.com inbound action is running; line 6');
var rc = cart.placeOrder();
gs.log('#### Create RITM from Oracle LFG_CMO@lfg.com inbound action is running; line 8');
gs.addInfoMessage(rc.number);

})(current, event, email, logger, classifier);

find_real_file.png

Found the answer!  Thanks to Cody's post: 

https://community.servicenow.com/community?id=community_question&sys_id=d0f905041b735c106531ea89bd4bcbb7

 

Starting with Paris, a catalog item needs to have User Criteria (OOB "Any User" for example) for the cart.placeOrder() to work if you want to allow a Guest user (an unauthenticated user) to use the Cart API to create a catalog request.

I added the "Any User" User Criteria to my catalog item and now it works.