Skip to main content

ServiceNow Submit a Request Using Cart API - 2 Approaches

ServiceNow Submit a Request Using Cart API - 2 Approaches

1) cart_api object

2) using table api


--

var instName = gs.getProperty('instance_name');

if (instName == 'xxdev' || instName == 'xxdev2') {

    submitRequest(); //--CART API

    //submitRequest_tableapi(instName); //--TABLE API - useful to target a remote instance

}

 

function submitRequest() {

    var sRequestedFor = gs.getUserID();

    var sApplicationName = 'test_app';

    var sEnvironment = 'DEV';

    var sMetaData = '';

    var sOwnerEmails = 'test@test.com';

    var sTenantID = xxxxx-4718-4590-a921-xxxxxxxxx;

    var sReplyURL = 'www.test123.com';

    var sManager = 'bfdb471e1b3931500b2c2f876e4xxxxx';  

    var sAppDescription = 'test-1234';

    var sURL_1 = 'www.test456.com'

 

    var cart = new sn_sc.CartJS();

    var item = {

        'sysparm_id': 'f18a7b2b93681650aba13249583xxxxx',

        'sysparm_quantity': '1',

        'variables': {

            'requested_for': sRequestedFor,

            'requesting_user': sRequestedFor,

            'requested_for_manager': sManager,

            'application_name': sApplicationName,

            'environment': sEnvironment,

            'spm_name': sApplicationName,

            'where_to_federate': 'azure',

            'app_description': sAppDescription,

            'federation_type': 'SAML',

            'app_it_owner': sManager,

            'sso_type': 'SAML',

            'metadata': sMetaData,

            'primary_id': 'KAID',

            'owner_emails': sOwnerEmails,

            'tenant_id': sTenantID,

            'reply_url': sReplyURL,

            'logout_url': sURL_1,

            'application_documentation_url': sURL_1,

            'lifespandays': '360'

        }

    };

    //var cartDetails = cart.addToCart(item);

    var cartDetails = cart.orderNow(item);

    //-- in case of errors, this will throw the error:

    gs.print(JSON.stringify(cartDetails));

 

    gs.print(cartDetails.number);

 

}

 

function submitRequest_tableapi(inst) {

 

    var sRequestedFor = gs.getUserID();

    var request = new sn_ws.RESTMessageV2();

    var sn_instance = 'https://' + inst + '.service-now.com';

    var cat_item_sysid = 'f18a7b2b93681650aba13249583xxxxx'; //--

 

    var catalogapi_url = '/api/sn_sc/servicecatalog/items/' + cat_item_sysid + '/order_now';

    var sEndPoint = sn_instance + catalogapi_url;

    //gs.print('sEndPoint='+sEndPoint);

 

    request.setEndpoint(sEndPoint);

    request.setHttpMethod('POST');

 

    var sApplicationName = 'test_app';

    var sEnvironment = 'DEV';

    var sMetaData = '';

    var sOwnerEmails = test@test.com';

    var sTenantID = 'xxxx-4718-4590-a921-f17exxxxx';

    var sReplyURL = 'www.test123.com';

    var sManager = 'bfdb471e1b3931500b2c2f876e4xxxxx';  

 

    //--OPTION 1:

    //var sUser = ‘xuser’;

    //var password = gs.getProperty('testuser.pwd');

    //request.setBasicAuth(user, password);

 

    //--OPTION 2:

    var authentication_type = 'basic';

    var profile_name = '923c8486fb305ad42478f4ff4exxxxxx'; //--sys_id of sys_auth_profile_basic record

    request.setAuthenticationProfile(authentication_type, profile_name);

 

    request.setRequestHeader("Accept", "application/json");

    //--data:

    var JSON_cat_item = "{";

    JSON_cat_item += "sysparm_quantity: 1";

    JSON_cat_item += ",";

    JSON_cat_item += "variables: {";

    JSON_cat_item += "requested_for: '" + sRequestedFor + "',"; //--MUST contain the ' ' or throws an error

 

    JSON_cat_item += "requesting_user: '" + sRequestedFor + "',"; //--MUST contain the ' ' or throws an error

    JSON_cat_item += "requested_for_manager: '" + sManager + "',"; //--MUST contain the ' ' or throws an error

 

    JSON_cat_item += "application_name: '" + sApplicationName + "',";

    JSON_cat_item += "environment: '" + sEnvironment + "',";

    JSON_cat_item += "spm_name: '" + sApplicationName + "',";

    JSON_cat_item += "where_to_federate: '" + 'azure' + "',";

    JSON_cat_item += "app_description: '" + 'test124' + "',";

 

    JSON_cat_item += "federation_type: '" + 'SAML' + "',";

    JSON_cat_item += "app_it_owner: '" + sManager + "',";

    JSON_cat_item += "sso_type: '" + 'SAML' + "',";

    JSON_cat_item += "metadata: '" + sMetaData + "',";

    JSON_cat_item += "primary_id: '" + 'KAID' + "',";

    JSON_cat_item += "tenant_id: '" + sTenantID + "',";

 

    JSON_cat_item += "owner_emails: '" + sOwnerEmails + "',";

    JSON_cat_item += "reply_url: '" + sReplyURL + "',";

    JSON_cat_item += "logout_url: '" + sReplyURL + "',";

    JSON_cat_item += "application_documentation_url: '" + sReplyURL + "',";

    JSON_cat_item += "lifespandays: '" + '360' + "'";

 

    JSON_cat_item += "}";

    JSON_cat_item += "}";

 

    request.setRequestBody(JSON_cat_item);

 

    var response = request.execute();

    //gs.log('RESP=' + response.getBody());

 

    gs.print('httpstatus=' + response.getStatusCode());

 

    var json_string = JSON.parse(response.getBody());

    gs.print('REQ RESP=' + JSON.stringify(json_string));

    var sReqNum = json_string.result.number;

    gs.print('REQUEST NUMBER=' + sReqNum);

 

}

Comments

Popular posts from this blog

ServiceNow check for null or nil or empty (or not)

Haven't tested these all recently within global/local scopes, so feel free to have a play! option 1 use an encoded query embedded in the GlideRecord , e.g.  var grProf = new GlideRecord ( 'x_cls_clear_skye_i_profile' ); grProf . addQuery ( 'status=1^ owner=NULL ' ); grProf . query (); even better use the glideRecord  addNotNullQuery or addNullQuery option 2 JSUtil.nil / notNil (this might be the most powerful. See this link ) example: if ( current . operation () == 'insert' && JSUtil . notNil ( current . parent ) && ! current . work_effort . nil ())  option 3 there might be times when you need to get inside the GlideRecord and perform the check there, for example if the code goes down 2 optional routes depending on null / not null can use gs.nil : var grAppr = new GlideRecord ( 'sysapproval_approver' ); var grUser = new GlideRecord ( 'sys_user' ); if ( grUser . get ( 'sys_id' , current . approver )){

Service Catalog: variable advanced reference qualifiers

Call a script include to apply a reference qualifier on a catalog item variable: - variable reference qualifier dependent on another variable selection, in this case a variable referencing sys_user (requested_for) On the catalog item form. variable name to apply ref qual filter : retail_equipment variable reference qualifier (on cmdb table ): javascript : new  refqual_functions (). lostStolen_getAssignedCIs (); client-callable script include ( refqual_functions)  function : lostStolen_getAssignedCIs : function (){         //--called from variable set client script, for lost/stolen request (service catalog)     gs . log ( current . variables . requested_for , 'retail_lostStolen_getAssignedCIs' );         return ( 'install_statusNOT IN8,7^owned_by=' + current . variables . requested_for );             //owned_by=1269b79937f1060041c5616043990e41^install_statusNOT IN8,7            },

Get URL Parameter - server side script (portal or classic UI)

Classic UI : var sURL_editparam = gs . action . getGlideURI (). getMap (). get ( ' sysparm_aparameter ' ); if ( sURL_editparam == 'true' ) { gs . addInfoMessage ( 'parameter passed ); } Portal : var sURL_editparam = $sp . getParameter ( " sysparm_aparameter " ); if ( sURL_editparam == 'true' ) { gs . addInfoMessage ( 'parameter passed ); }