Skip to content

Instantly share code, notes, and snippets.

@jmbauguess
Created August 26, 2015 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmbauguess/74867e79615b7a02cda1 to your computer and use it in GitHub Desktop.
Save jmbauguess/74867e79615b7a02cda1 to your computer and use it in GitHub Desktop.
Creates Requested Items that can be approved and worked for testing purposes
/**
* @description Creates Requested Items that can be approved and worked for testing purposes
* @namespace
* @type {Class}
*/
var RequestTester = Class.create();
RequestTester.prototype = {
/**
* @description Creates a Requested Item given a name of a catalog item and variables to enter
* @param {String} name The name of a catalog item
* @param {Object} cartVariables An object containing name/value pairs of variables
* @return {GlideRecord} The requested item record
* @example
* var rt = new RequestTester();
* var myVariables = [{'name' : 'Var1', 'value' : 'Val1'}, {'name' : 'Var2', 'value' : 'Val2'}];
* var req = rt.createRequestByName('MyRequest', myVariables);
*/
createRequestByName: function(name, cartVariables) {
var cartId = GlideGuid.generate(null),
cart = new Cart(cartId),
item = cart.addItem(this.getItemByName(name).sys_id),
rc,
vars;
for (vars in cartVariables) {
if (cartVariables.hasOwnProperty(vars)) {
cart.setVariable(item, cartVariables[vars].name,
cartVariables[vars].value);
}
}
rc = cart.placeOrder();
return rc;
},
/**
* @description Gets a requested item based on its parent request
* @param {GlideRecord} request A Request record
* @return {GlideRecord} A requested item record
* @example
* var rt = new RequestTester();
* var myVariables = [{'name' : 'Var1', 'value' : 'Val1'}, {'name' : 'Var2', 'value' : 'Val2'}];
* var req = rt.createRequestByName('MyRequest', myVariables);
* var ritm = rt.getRequestedItemByRequest(req);
*/
getRequestedItemByRequest: function(request) {
var ritm = new GlideRecord('sc_req_item');
if (ritm.get('request', request.sys_id)) {
return ritm;
}
},
/**
* @description Gets a catalog item by name
* @param {String} name The name of a catalog item
* @return {GlideRecord} The glide record related to the catalog item name
*/
getItemByName: function(name) {
var catalogItem = new GlideRecord('sc_cat_item');
catalogItem.get('name', name)
return catalogItem;
},
/**
* @description Gets all current approvals that are requested for the ritm
* @param {GlideRecord} ritm A requested item
* @return {GlideRecord} A record of approvals; or a blank object if there aren't any approvals
*/
getApprovals: function(ritm) {
var approvals = new GlideRecord('sysapproval_approver');
approvals.addQuery('sysapproval', ritm.sys_id);
approvals.addQuery('state', 'requested');
approvals.query();
if (approvals.hasNext()) {
gs.log("Found approvals for " + ritm.number);
return approvals;
}
gs.log("No approvals for " + ritm.number);
return null;
},
/**
* @description Sets all approvals to approved for a given ritm; Recursive, as approvals happen in phases at times
* @param {GlideRecord} ritm A requested item
* @example
* var rt = new RequestTester();
* var req = rt.createRequestByName('MyRequest', myVariables);
* var ritm = rt.getRequestedItemByRequest(req);
* rt.makeApprovals(ritm);
*/
makeApprovals: function(ritm) {
var approval = this.getApprovals(ritm);
if (approval == null) {
return;
} else {
if (approval.next()) {
approval.state = 'approved';
approval.update();
}
this.makeApprovals(ritm);
}
},
/**
* @description Sets an approval to rejected for a given ritm
* @param {GlideRecord} ritm A requested item
* @return {String} The sys_id of the updated approval record
* @example
* var rt = new RequestTester();
* var req = rt.createRequestByName('MyRequest', myVariables);
* var ritm = rt.getRequestedItemByRequest(req);
* rt.rejectApprovals(ritm);
*/
rejectApprovals: function(ritm) {
var approval = this.getApprovals(ritm);
if (approval == null) {
return '';
} else {
if (approval.next()) {
approval.state = 'rejected';
return approval.update();
}
}
},
/**
* @description Sets an approval to rework for a given ritm
* @param {GlideRecord} ritm A requested item
* @return {String} The sys_id of the updated approval record
* @example
* var rt = new RequestTester();
* var req = rt.createRequestByName('MyRequest', myVariables);
* var ritm = rt.getRequestedItemByRequest(req);
* rt.reworkApprovals(ritm);
*/
reworkApprovals: function(ritm) {
var approval = this.getApprovals(ritm);
if (approval == null) {
return '';
} else {
if (approval.next()) {
approval.state = 'rework';
return approval.update();
}
}
},
/**
* @description Updates a ritm so it moves from rework to requested
* @param {GlideRecord} ritm A requested item
* @return {GlideRecord} The updated requested item
* @example
* var rt = new RequestTester();
* var req = rt.createRequestByName('MyRequest', myVariables);
* var ritm = rt.getRequestedItemByRequest(req);
* rt.reworkApprovals(ritm);
* rt.updateRitmForRework(ritm);
* rt.makeApprovals(ritm);
*/
updateRitmForRework: function(ritm) {
ritm.comments = "Updating for reworking!";
ritm.update();
return ritm;
},
/**
* @description Gets catalog tasks associated with a ritm
* @param {GlideRecord} ritm A requested item
* @return {GlideRecord} A collection of catalog tasks; or an empty object if no tasks exist
*/
getCatalogTasks: function(ritm) {
var tasks = new GlideRecord('sc_task');
tasks.addQuery('request_item', ritm.sys_id);
tasks.query();
if (tasks.hasNext()) {
gs.log("Found tasks for " + ritm.number);
return tasks;
}
gs.log("No tasks for " + ritm.number);
return null;
},
/**
* @description Closes catalog tasks related to a requested item; Recursive, as tasks generate in stages sometimes
* @param {GlideRecord} ritm A requested item
* @example
* var rt = new RequestTester();
* var req = rt.createRequestByName('MyRequest', myVariables);
* var ritm = rt.getRequestedItemByRequest(req);
* rt.makeApprovals(ritm);
* rt.closeCatalogTasks(ritm);
*/
closeCatalogTasks: function(ritm) {
var tasks = this.getCatalogTasks(ritm);
if (tasks == null) {
return;
} else {
while (tasks.next()) {
tasks.state = 3;
tasks.update();
}
this.closeCatalogTasks(ritm);
}
},
'type' : 'RequestTester'
};
@jmbauguess
Copy link
Author

Unfortunately, the log statements in some functions are necessary to slow down the code long enough for some records to be visible. Still trying to find a way around this.

@johnbouchard
Copy link

Good Articles, have enjoyed your stuff, you approach things in an organized manner and well thought out. For the issues with writing logs to slow down records to be visible, have you tried promises?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment