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
Post a Comment