Use scripts - background to create catalog items

psnow123
Giga Guru

I'm trying to use the scripts - background to test out the code for an inbound email action, that creates a catalog item, but the compiler throws an error. 

 

I understand the error, but is there a way to use these objects to create catalog items, or is there another way to create catalog items in scripts - background?

 

Evaluator.evaluateString() problem: java.lang.SecurityException: GlideGuid is not allowed in scoped applications: 	com.glide.script.fencing.GlidePackageScopeHandler.found(GlidePackageScopeHandler.java:28)

 Code:

 // Implement email action here
    var cartID = GlideGuid.generate(null);
    var cart = new Cart(cartID);
    var item = cart.addItem('526542fe9787611051ba39000153af84');

    var legalNameParsed = "LEGAL NAME FROM EMAIL BODY";
    var countryParsed = "USA";

    cart.setVariable(item, 'what_is_the_full_legal_name_of_the_company', legalNameParsed);
    cart.setVariable(item, 'what_is_the_country', countryParsed);
    cart.setVariable(item, 'email_body', email.body_html);

    if (email.subject.toString() == 'New B') {
        cart.setVariable(item, 'what_kind_of_request_is_this', 'new_b');
    } else if (email.subject.toString() == 'New Company') {
        cart.setVariable(item, 'what_kind_of_request_is_this', 'new_company');
    } else if (email.subject.toString() == 'New Company I') {
        cart.setVariable(item, 'what_kind_of_request_is_this', 'new_company_i');
    }


    var ritm = new GlideRecord('sc_req_item');
    ritm.addQuery('request', rc.sys_id);
    ritm.query();
    if (ritm.next()) {
        ritm.short_description = email.subject.toString();
        ritm.contact_type = "email";
        ritm.update();
    }

 

3 REPLIES 3

Riya Verma
Kilo Sage
Kilo Sage

Hi @psnow123 ,

 

Hope you are doing great.

 

In order to create the catalog item via script, we need to create cartGR Glide record object after setting the variables. Use cart.getCart() and the update the created RITM variables if you want to update any. Once done, we need to use cart.placeOrder() to generate the RITM.

 

Please try using below script:

 // Implement email action here
    var cartID = GlideGuid.generate(null);
    var cart = new Cart(cartID);
    var item = cart.addItem('526542fe9787611051ba39000153af84');

    var legalNameParsed = "LEGAL NAME FROM EMAIL BODY";
    var countryParsed = "USA";

    cart.setVariable(item, 'what_is_the_full_legal_name_of_the_company', legalNameParsed);
    cart.setVariable(item, 'what_is_the_country', countryParsed);
    cart.setVariable(item, 'email_body', email.body_html);

    if (email.subject.toString() == 'New B') {
        cart.setVariable(item, 'what_kind_of_request_is_this', 'new_b');
    } else if (email.subject.toString() == 'New Company') {
        cart.setVariable(item, 'what_kind_of_request_is_this', 'new_company');
    } else if (email.subject.toString() == 'New Company I') {
        cart.setVariable(item, 'what_kind_of_request_is_this', 'new_company_i');
    }

var rc = cart.placeOrder();
// rc will be storin the generated RITM object which can be useed further based on requirement 
Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma

Saurav11
Kilo Patron
Kilo Patron

Hello,

 

Please use generateGUID().generate(null) should solve the issue.

 

Please mark my answer as correct based on Impact.

Sai Shravan
Mega Sage

Hi @psnow123 ,

The error message suggests that the use of 'GlideGuid' is not allowed in scoped applications. Scoped applications have restricted access to certain ServiceNow objects and APIs for security reasons.

 

To create catalog items in a script background or inbound email action, you can use the sc_cat_item and sc_cart tables directly instead of relying on the Cart and GlideGuid objects.

var cart = new GlideRecord('sc_cart');
cart.initialize();
cart.insert();

var item = new GlideRecord('sc_cat_item');
if (item.get('<catalog item sys_id>')) {
  var cartItem = new GlideRecord('sc_cart_item');
  cartItem.initialize();
  cartItem.cart = cart.sys_id;
  cartItem.item = item.sys_id;
  cartItem.insert();

  var legalNameParsed = 'LEGAL NAME FROM EMAIL BODY';
  var countryParsed = 'USA';

  cartItem.setValue('variables.what_is_the_full_legal_name_of_the_company', legalNameParsed);
  cartItem.setValue('variables.what_is_the_country', countryParsed);
  cartItem.setValue('variables.email_body', email.body_html);

  if (email.subject.toString() == 'New B') {
    cartItem.setValue('variables.what_kind_of_request_is_this', 'new_b');
  } else if (email.subject.toString() == 'New Company') {
    cartItem.setValue('variables.what_kind_of_request_is_this', 'new_company');
  } else if (email.subject.toString() == 'New Company I') {
    cartItem.setValue('variables.what_kind_of_request_is_this', 'new_company_i');
  }

  cartItem.insert();

  var ritm = new GlideRecord('sc_req_item');
  ritm.addQuery('request', rc.sys_id);
  ritm.query();
  if (ritm.next()) {
    ritm.short_description = email.subject.toString();
    ritm.contact_type = 'email';
    ritm.update();
  }
}

 

Regards,

Shravan

Please mark this as helpful and correct answer, if this helps you

Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you