- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-27-2018 12:04 PM
Hello, everyone, I am working on a script that automatically consumes. It works fine when I use the Script - Background module, but when I use it in a workflow, I get a different result. Basically, in the workflow, it takes the consumable and puts it in both Consumed and In Use state with an empty Display Name and Model.
Before running the workflow, the top row was 60. I entered a quantity of 2 and it got sent to the two top rows.
I just want it to show up as the actual item I selected. I am open to another way to automatically consume. I just need it to work with the workflow.
Here is the script:
var this_consumable = current.variables.alm_consumable;
//The this_consumable variable is a Reference variable used to select the consumable from the available stockrooms.
var this_qty = current.quantity;
var this_user = current.request.requested_for;
current.variables.alm_consumable_pull_from_stock = (" Qty "+ this_qty + "|" + this_consumable.getDisplayValue() );
//this line prints the value to a single line text variable on the Task form.
var new_sys_id = new Consumables().splitForeground(this_consumable, this_qty, '10', '', '', '', '', this_user);
//this is the script include that actually consumes and assigns to the user
Thank you,
CJB
Solved! Go to Solution.
- Labels:
-
Enterprise Asset Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-31-2018 08:42 AM
With this script, consumableAsset must be the SysID of the consumable not the display value:
new Consumables().splitForeground(consumableAsset, qty, '10', '', '', '', '', requestedFor);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-27-2018 12:08 PM
Can you post your script include as well? Is it on the same scope?
Also try doing this at the start of the script
gs.include("Consumables");
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-29-2018 06:42 AM
Hi, Sanjiv, was that the right script include?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-27-2018 12:25 PM
Hello, Sanjiv! I will include gs.include at start of my script. Yes, it is the same scope. Here is the script include for Consumables:
var Consumables = Class.create();
Consumables.prototype = {
initialize : function() {
},
splitForeground : function(sys_id, qty, status, substatus, asset, stockroom, location, assigned_to) {
this.split(sys_id, qty, status, substatus, asset, stockroom, location, assigned_to);
if (asset != '') {
var assetRecord = new GlideRecord('alm_asset');
if (assetRecord.get(asset)) {
if (qty == 1)
gs.addInfoMessage(gs.getMessage('One item has been consumed and attached to {0}', assetRecord.getDisplayValue()));
else if (qty > 1)
gs.addInfoMessage(gs.getMessage('{0} items have been consumed and attached to {1}', [qty + '', assetRecord.getDisplayValue()]));
}
}
if (assigned_to != '') {
var userRecord = new GlideRecord('sys_user');
if (userRecord.get(assigned_to)) {
if (qty == 1)
gs.addInfoMessage(gs.getMessage('One item has been consumed and attached to {0}', userRecord.getDisplayValue()));
else if (qty > 1)
gs.addInfoMessage(gs.getMessage('{0} items have been consumed and attached to {1}', [qty + '', userRecord.getDisplayValue()]));
}
}
},
split : function(sys_id, qty, status, substatus, asset, stockroom, location, assigned_to) {
var consumable = this._getConsumable(sys_id);
if (parseInt(qty,10) > parseInt(consumable.quantity,10)) {
gs.addErrorMessage(gs.getMessage(
'Trying to split {0} when only {1} exist', [ qty + '',
consumable.quantity + '' ]));
return consumable;
}
var split = (parseInt(consumable.quantity,10) != parseInt(qty,10));
if (split) {
cost = consumable.cost / consumable.quantity;
consumable.quantity = consumable.quantity - qty;
consumable.update();
consumable.quantity = qty;
consumable.cost = cost * qty;
}
consumable.install_status = status;
consumable.substatus = substatus;
if (asset != '') {
// Asset is being split and associated with real asset.
consumable.parent = asset;
consumable.assigned_to = assigned_to;
} else {
// Only setting these fields when the split consumable is
// not associated with a real asset. For real-assets,
// downstream BRs manage these assignments.
consumable.stockroom = stockroom;
consumable.location = location;
consumable.assigned_to = assigned_to;
}
if (split)
return consumable.insert();
else
return consumable.update();
},
getMaxInState : function(model, stockroom, status, substatus, asset) {
var qty = 0;
var gr = new GlideRecord('alm_asset');
gr.addQuery('stockroom', stockroom);
gr.addQuery('install_status', status);
gr.addQuery('substatus', substatus);
gr.addQuery('model', model);
if (asset != '')
gr.addQuery('sys_id', asset);
gr.query();
if (gr.next())
qty = gr.quantity;
return qty;
},
_getConsumable : function(sys_id) {
var consumable = new GlideRecord('alm_consumable');
consumable.addQuery('sys_id', sys_id);
consumable.query();
consumable.next();
return consumable;
},
mergeConsumable : function(current) {
var doRedirect = false;
if ((!current.nil()) && (!current.doRedirect.nil()))
doRedirect = current.doRedirect;
var gr = new GlideRecord('alm_consumable');
gr.addQuery('model', current.model);
gr.addQuery('location', current.location);
gr.addQuery('model_category', current.model_category);
gr.addQuery('stockroom', current.stockroom);
gr.addQuery('install_status', current.install_status);
gr.addQuery('substatus', current.substatus);
gr.addQuery('parent', current.parent);
gr.addQuery('assigned_to', current.assigned_to);
if (SNC.AssetMgmtUtil.isPluginRegistered('com.snc.procurement')) {
if (current.install_status == 2)
gr.addQuery('purchase_line', current.purchase_line);
}
if (current.active_to == true)
gr.addQuery('active_to', true);
else
gr.addQuery('active_to', false).addOrCondition('active_to', null);
gr.query();
while (gr.next()) {
if (gr.sys_id != current.sys_id) {
gs.addInfoMessage(gs.getMessage('Merged updated record with previous record with similar attributes'));
gr.quantity = parseInt(gr.quantity,10) + parseInt(current.quantity,10);
gr.cost = parseFloat(gr.cost) + parseFloat(current.cost);
gr.update();
current.deleteRecord();
current = gr;
if (doRedirect == true)
action.setRedirectURL(gr);
break;
}
}
},
type : 'Consumables'
};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-29-2018 07:01 AM
CJB, I see you are using the out of the box code behind the Consume UI Action and associated UI page which is what I was going to suggest prior to digging a little.
What type of catalog item are you using for this process? It is a request or a record producer? Trying to replicate this in my instance.