The CatItem API provides methods that enable you to create and modify service catalog items using scripts.

This API runs in the sn_sc namespace.

CatItem - availableForUserCriteria(String action, Array criteriaIDs)

Adds the Available For user criteria to the current catalog item.

Table 1. Parameters
Name Type Description
action String Action to perform.
  • add: Adds the user criteria to the Available For list.
  • delete: Deletes the user criteria from the Available For list.
criteriaIDs Array Array of the user criteria sys_ids.
Table 2. Returns
Type Description
void

Example

This example shows how to add the specified Available For user criteria.

var item = new sn_sc.CatItem("31bea3d53790200044e0bfc8bcbe5dec");
item.availableForUserCriteria("add", ["0c441abbc6112275000025157c651c89"]);

CatItem - canViewInDomain()

Verifies whether the current catalog item is viewable in the selected domain (domain selected in the domain picker).

Catalog items in the global domain are available across all domains.

Table 3. Parameters
Name Type Description
None
Table 4. Returns
Type Description
Boolean Flag that validates whether the current catalog item is viewable in the selected domain.

Valid values:

  • true: Catalog item is viewable in the domain
  • false: Catalog item is not viewable in the domain

Example

This example shows how to verify whether a catalog item is viewable in the currently selected domain.

var catItem = new sn_sc.CatItem("060f3afa3731300054b6a3549dbe5d3e");
gs.info(catItem.canViewInDomain());

CatItem - canViewOnSearch(boolean isMobile)

Determines if the user has access to view the catalog item on global search.

Table 5. Parameters
Name Type Description
isMobile Boolean

Flag that indicates whether to perform the search for the mobile or desktop view.

Valid values:
  • true: Perform the search for the mobile view.
  • false: Perform the search for the desktop view.
Table 6. Returns
Type Description
Boolean

Flag that indicates whether the user has access to view the catalog item on global search.

Valid values:
  • true: User has access to view the catalog item on global search.
  • false: User does not have access to view the catalog item on global search.

Example

This code example shows how to check if the user has access to view the catalog item on global search in desktop view.

var catItem = new sn_sc.CatItem("04b7e94b4f7b4200086eeed18110c7fd");
var canView = catItem.canViewOnSearch('false');
gs.info("Can view on global search: " + canView);

Output:

Can view on global search: true

CatItem - create(Boolean standardUpdate)

Inserts the defined catalog item.

Table 7. Parameters
Name Type Description
standardUpdate Boolean Flag that indicates whether to enable the running of engines and workflow.
Valid values:
  • true: Enable the running of engines and workflow.
  • false: Do not enable the running of engines and workflow. Note that the created and updated system date columns on the table are not updated.
Table 8. Returns
Type Description
String Sys_id of the newly created catalog item.

Example

This example creates a new catalog item and adds a variable and variable set.

createCatalogItem('Catalog Item Name', 'Short Description', 'e0d08b13c3330100c8b837659bba8fb4', '109f0438c6112276003ae8ac13e7009d'); 

function createCatalogItem(name, short_desc, catalogId, categoryId) {
    var catalogItem = new sn_sc.CatItem();
    catalogItem.setAttributes({
        "name": name,
        "short_description": short_desc
    });
    catalogItem.setCatalogs(catalogId); // set catalog
    catalogItem.setCategories(categoryId); // set category

    var catItemSysId = catalogItem.create(); // create new catalog item
    gs.info('Catalog item created in table sc_cat_item with sys_id ' + catItemSysId);

    // add variables and variable set
    addDefaultVariables(catItemSysId);
    addDefaultVariableSet(catItemSysId);
}

// creates a new variable and adds it to the catalog item
function addDefaultVariables(catItemSysId) {
    var myVarAttrs = {
        "type": "6", // type 6 is single line text
        "cat_item": catItemSysId,
        "question_text": "First Name",
        "name": "first_name",
        'order': 50
    };
    var myVar = new sn_sc.CatalogItemVariable();
    myVar.setAttributes(myVarAttrs);
    var varRec = myVar.create(true);
    gs.info('Variable added to catalog item and record created in table item_option_new with sys_id ' + varRec);
}

// adds an existing variable set to the catalog item
function addDefaultVariableSet(catItemSysId) {
    var varset = new sn_sc.CatalogItemVariableSetM2M();
    // fields used in object are from table io_set_item 
    var attr = {
        'variable_set': 'e01cab1a4f334200086eeed18110c71f', // required
        'sc_cat_item': catItemSysId, // required
        'order': 100 // optional
    };
    varset.setAttributes(attr);
    var m2mRec = varset.create(true);
    gs.info('Variable set added to catalog item and M2M record created in table io_set_item with sys_id ' + m2mRec);
}

Output:

Catalog item created in table sc_cat_item with sys_id be5c771e876370103a730f2d0ebb3556
Variable added to catalog item and record created in table item_option_new with sys_id b65cb71e876370103a730f2d0ebb3535
Variable set added to catalog item and M2M record created in table io_set_item with sys_id 8b5cb71e876370103a730f2d0ebb354b

CatItem - deleteRecord(Boolean standardUpdate)

Deletes a catalog item.

Table 9. Parameters
Name Type Description
standardUpdate Boolean Flag that indicates whether to enable the running of engines and workflow.
Valid values:
  • true: Enable the running of engines and workflow.
  • false: Do not enable the running of engines and workflow.
Table 10. Returns
Type Description
void

Example

This example deletes all inactive catalog items.

var catalogItem = new GlideRecord('sc_cat_item');
catalogItem.addQuery('active', 'false'); // get all inactive catalog items
catalogItem.query();
while (catalogItem.next()) {

    // before deleting a catalog item, delete its associated variable set M2M records
    var variableSetM2M = new GlideRecord('io_set_item'); // M2M table linking variable set and catalog item
    variableSetM2M.addQuery('sc_cat_item', catalogItem.getUniqueValue()); // get M2M records for the catalog item
    variableSetM2M.query();
    while (variableSetM2M.next()) {
        var varset = new sn_sc.CatalogItemVariableSetM2M(variableSetM2M.getUniqueValue()); // M2M record sys_id
        varset.deleteRecord(true); // delete M2M record
    }

    // then delete the catalog item
    var item = new sn_sc.CatItem(catalogItem.getUniqueValue()); 
    item.deleteRecord(true);
}

CatItem - getFirstAccessibleCategoryForSearch(String catalogId)

Returns the first category that the user can view in a catalog.

Table 11. Parameters
Name Type Description
catalogId String Sys_id of the catalog.
Table 12. Returns
Type Description
String Sys_id of the first category that the user can view in a catalog.

Example

Example:

var CatItem=new sn_sc.CatItem("04b7e94b4f7b4200086eeed18110c7fd");	
	console.log(CatItem.getFirstAccessibleCategoryForSearch("e0d08b13c3330100c8b837659bba8fb4"));

Output:

d258b953c611227a0146101fb1be7c31

CatItem - getInvalidDelegatedUsers(Array requestForUsers)

Returns an array of users for whom the associated item cannot be delegated (requested on behalf of).

The method verifies each of the users passed in the array.

Table 13. Parameters
Name Type Description
requestForUsers Object Array of user sys_ids to check whether the associated user can acquire the current item and that the item can be requested on behalf of them.

Table: User [sys_user]

Table 14. Returns
Type Description
Array List of user names (Name column from Users [sys_user] table) for whom the item can't be requested for by a delegate.

Example

This example shows how to obtain a list of user names for whom the item can't be requested for by a delegate.

function getInvalidDelegatedUsers(itemId, userIds) {
var catItem = new sn_sc.CatItem(itemId);
var invalidUsers = catItem.getInvalidDelegatedUsers(userIds);
return invalidUsers;
}

Output:

[
  "Joe Smith",
  "Jenny Brown",
  "Fred Bennet",
  "Alice Jones"
]

CatItem - getRecordClass()

Returns the class name for the current catalog item record.

Table 15. Parameters
Name Type Description
None
Table 16. Returns
Type Description
String Class name for the current catalog item record.

Example

Example:

var CatItem=new sn_sc.CatItem("04b7e94b4f7b4200086eeed18110c7fd");	
	console.log(CatItem.getRecordClass());

Output:

sc_cat_item

CatItem - isDelegationAllowed(String delegatedUser)

Verifies whether the specified delegated user has acquisition rights to the current service catalog item.

Table 17. Parameters
Name Type Description
delegatedUser String Optional. Sys_id of the user to request the service catalog item for (delegate). The method verifies whether the user has acquisition rights to the item.

Default: Checks whether the calling user has acquisition rights to the item.

Table 18. Returns
Type Description
Boolean Flag that indicates whether the user has acquisition rights to the current service catalog item.
Valid values:
  • true: User has acquisition rights to the item.
  • false: User does not have acquisition rights to the item.

Example

This code example shows how to determine if delegation is allowed for the catalog item.

function canRequestFor(itemId, user) {
  var catItem = new sn_sc.CatItem(itemId);
  var result = catItem.isDelegationAllowed(user);
  return result;
}

Output: true

CatItem - isVisibleServicePortal()

Determines if the current catalog item is available in service portal.

Table 19. Parameters
Name Type Description
None
Table 20. Returns
Type Description
Boolean

Flag that indicates whether the catalog item is available in the Service Portal.

Valid values:
  • true: Available on Service Portal.
  • false: Not available on Service Portal.

Example

Example:

var catItem = new sn_sc.CatItem("04b7e94b4f7b4200086eeed18110c7fd");
var catItemAvail = catItem.isVisibleServicePortal();
gs.info("Is item available on Service Portal: " + catItemAvail);

Output:

Is item available on Service Portal: true

CatItem - notAvailableForUserCriteria(String action, Array criteriaIDs)

Adds the Not Available For user criteria to a catalog item.

Table 21. Parameters
Name Type Description
action String Action to perform.
  • add: Adds the user criteria to the Not Available For list.
  • delete: Deletes the user criteria from the Not Available For list.
Table 22. Returns
Type Description
void

Example

This example shows how to add the specified Not Available For user criteria.

var item = new sn_sc.CatItem("31bea3d53790200044e0bfc8bcbe5dec");
item. notAvailableForUserCriteria("add", ["0c441abbc6112275000025157c651c89"]);

CatItem - read(Object columns, Boolean standardUpdate)

Returns a mapping of catalog item attribute values.

Table 23. Parameters
Name Type Description
columns Object Name-value pairs of columns for which to return values.
standardUpdate Boolean Flag that indicates whether to enable the running of engines and workflow.
Valid values:
  • true: Enable the running of engines and workflow.
  • false: Do not enable the running of engines and workflow.
Table 24. Returns
Type Description
Object Mapping of column names to values.

Example

This example reads the name and price fields of a catalog item.

var catItem = new sn_sc.CatItem("a96277509f300200b407b89a442e704e");
var values = catItem.read({"name" : "", "price" : ""}, true);
gs.info("Catalog item name: " + values.name);
gs.info("Catalog item price: " + values.price);
Output:
Catalog item name: Standard Laptop
Catalog item price: 1100

CatItem - setAttributes(Object attributes)

Sets attributes for a catalog item.

Table 25. Parameters
Name Type Description
attributes Object Name-value pairs of the columns for which to set.
Table 26. Returns
Type Description
void

Example

This example sets attributes for a new catalog item.

createCatalogItem('Name of your Catalog Item', 'Short Description of your Catalog Item', 'e0d08b13c3330100c8b837659bba8fb4', '109f0438c6112276003ae8ac13e7009d'); 

function createCatalogItem(name, short_desc, catalogId, categoryId) {
    var catalogItem = new sn_sc.CatItem();
    // catalog item column values
    var attributes ={
        "name": name,
        "short_description": short_desc,
	 "description": "<p>This is a test catalog item.</p>",
	 "workflow":"0287f2c64a36232700820846b1f8bdce" // sys_id of workflow
    };
    catalogItem.setAttributes(attributes); // set catalog item attributes
    catalogItem.setCatalogs(catalogId); // set catalog
    catalogItem.setCategories(categoryId); // set category

    var catItemSysId = catalogItem.create(); // create new catalog item
    gs.info('Catalog item created in table sc_cat_item with sys_id ' + catItemSysId);
}

Output:

Catalog item created in table sc_cat_item with sys_id 706948a287e370103a730f2d0ebb351e

CatItem - setCatalogs(String catalogs)

Defines the catalogs that this catalog item is associated with.

Table 27. Parameters
Name Type Description
catalogs String Comma-separated list of sys_ids of the catalogs to associate with the item the item.
Table 28. Returns
Type Description
void

Example

This example associates a catalog item with two catalogs.

var short_desc = 'This is a short description of my catalog item.';
var catalogItem = new sn_sc.CatItem();
catalogItem.setAttributes({
    "name": 'My Catalog Item',
    "short_description": short_desc
});
catalogItem.setCatalogs('e0d08b13c3330100c8b837659bba8fb4,742ce428d7211100f2d224837e61036d'); // set Service Catalog and Technical Catalog

var catItemSysId = catalogItem.create(); // create new catalog item
gs.info('Catalog item created in table sc_cat_item with sys_id ' + catItemSysId);
Output:
Catalog item created in table sc_cat_item with sys_id d0c5dcaa872770103a730f2d0ebb35cb

CatItem - setCategories(String categories)

Defines the categories that this catalog item is associated with.

Table 29. Parameters
Name Type Description
categories String Comma-separated list of sys_ids of the categories to associate with the item the item.
Table 30. Returns
Type Description
void

Example

This example shows how to associate two categories to an item.

var catItem = new sn_sc.CatItem("060f3afa3731300054b6a3549dbe5d3e");
catItem.setCatagories("af443cfa5f130100a9ad2572f2b47747", "d467125fd7500200d74460affd6103a1");

CatItem - setImage(String dbImageSysId, String type)

Adds an image to a catalog item.

Table 31. Parameters
Name Type Description
dbImageSysId String Sys_id of the image from the Images table [db_image].
type String Type of image.
Valid values:
  • picture
  • icon
Table 32. Returns
Type Description
void

Example

This example adds an image to a catalog item.

var catItem = new sn_sc.CatItem("1fc0bd968777301093d5ec6d0ebb3548"); // sys_id of catalog item to update
// update fields if needed
var attr = {
    'name': 'New Name of Catalog Item - 123',
    'description': '<p>This is an updated description.</p><p> My description new line 1</p><p>My description new line 2</p>',
    'short_description': 'New Short Description of Catalog Item' 
};
catItem.update(attr, true);
catItem.setImage('1b443cfa5f130100a9ad2572f2b47746', 'picture'); // sys_id of image from table db_image

CatItem - setTableName(String tableName)

Defines the table name for this catalog item.

Table 33. Parameters
Name Type Description
tableName String Name of the table that extends Catalog Item [sc_cat_item].
Table 34. Returns
Type Description
void

Example

This example shows how to set the name of an extended table.

var catItem = new sn_sc.CatItem();
catItem.setTableName("New_catalog_table");

CatItem - submitProducer(Object values)

Creates a record using a specified Service Catalog record producer.

Table 35. Parameters
Name Type Description
values Object Contains the field values and record producer to use when creating the record.
{
  "variables": {Object},
  "sysparm_id": "String"
}
values.variables Object The field values to set for the new record.
{
   "field": "String",
   "field": "String"
}
values.sysparm_id String Sys_id of the record producer to use from the Record Producer [sc_cat_item_producer] table.

Example

This example creates an incident using the Create Incident record producer.

var catalogItem = new sn_sc.CatItem('3f1dd0320a0a0b99000a53f7604a2ef9');  //sys_id of the Create Incident record producer 
var values = {
   "variables": {"urgency" : "2", "comments" : "Laptop is in a brick state"},
   "sysparm_id": "3f1dd0320a0a0b99000a53f7604a2ef9"
}
var targetRecordDetails = catalogItem.submitProducer(values);
gs.info(JSON.stringify(targetRecordDetails, null, 3));

Output:

{
   "sys_id": "133b2ee1875f30103a730f2d0ebb35f9",
   "number": "INC0010002",
   "parent_id": null,
   "record": "api/now/table/incident/133b2ee1875f30103a730f2d0ebb35f9",
   "redirect_portal_url": "",
   "parent_table": "task",
   "redirect_url": "incident.do?sys_id=133b2ee1875f30103a730f2d0ebb35f9&sysparm_view=ess",
   "table": "incident",
   "redirect_to": "generated_record"
}

CatItem - update(Object columnValues, Boolean standardUpdate)

Updates the values for specified fields of a catalog item.

Table 37. Parameters
Name Type Description
columnValues Object Name-value pairs of the fields to update and their associated values.
standardUpdate Boolean Flag that indicates whether to enable the running of engines and workflow.
Valid values:
  • true: Enable the running of engines and workflow.
  • false: Do not enable the running of engines and workflow.
Table 38. Returns
Type Description
void

Example

This example updates the name, short description, and price of an existing catalog item.

var catItem = new sn_sc.CatItem("1fc0bd968777301093d5ec6d0ebb3548"); // sys_id of catalog item to update
printCatalogItem(); // print current catalog item values
// object containing fields to update
var attr = {
    "name": "New Name of Catalog Item",
    "short_description": "New Short Description of Catalog Item", 
    "price": "450"
};
catItem.update(attr, true);
printCatalogItem(); // print new catalog item values

// prints the name, short description, and price of the catalog item
function printCatalogItem(){
    // object containing fields to read
    var readValues = {
        "name" : "", 
        "short_description" : "", 
        "price" : ""
    };
    var values = catItem.read(readValues, true); // read the field values
    gs.info("Catalog item name: " + values.name);
    gs.info("Catalog item short description: " + values.short_description);
    gs.info("Catalog item price: " + values.price);
}
Output:
Catalog item name: Example Catalog Item
Catalog item short description: Example Short Description
Catalog item price: 300
Catalog item name: New Name of Catalog Item
Catalog item short description: New Short Description of Catalog Item
Catalog item price: 450