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 timeout settings for virtual agent chat

  After connecting to live agent User is connected to live agent and not responding to live agent chat or distracted. The below properties controls the timeout settings, the OOB settings for reminder is [180 sec] and for cancelling the chat is [360 sec]. The job is default configured to 2 min so I believe no tweaking is required here.  Property -   com.glide.cs.idle_chat_reminder_timeout                  com.glide.cs.idle_chat_cancel_timeout Scheduled job   - Idle Chat Timer Task   https://community.servicenow.com/community?id=community_article&sys_id=1453b03bdbaad0109e691ea668961929   (ServiceNow ) 

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 )){...