- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-28-2023 10:59 PM
Hi community,
I have a requirement. On the front end (portal form) there is a need to create a catalog item to transfer either hardware or consumables from one stockroom to another. Currently these are the variables on the catalog item:
model_transfer - Reference - cmdb_model table - Select the model to transfer
trnsfr_from_stockroom - Reference - alm_stockroom - Select the stockroom you are transferring from
trnsfr_to_stockroom - Reference - alm_stockroom
trnsfr_qty - String - regex: number - How many of this model are you transferring?
What I am trying to do is to create an onChange Catalog Client script targeting the model_transfer field, onChange
Whatever number is entered into this field, the client script should add parameters and call the script include where it will first determine if the model_transfer field (model selected) is model category of "consumable" or "Personal Computer"
If "Consumable, query the alm_consumable table for the model that matches the value in the model_transfer field and also matches the stockroom found in the trnsfr_from_stockroom and its state = 6 (In stock) and substatus = 'available and checks to ensure that the quantity field value is greater than or equal to the quantity placed in the trnsfr_qty field on the form. If it is greater, then return "true", if it is less than, return "false"
If the model_trnsfr field has a model whose model category != Consumable, then query the alm_hardware table for the model selected in the model_trnsfr field (returns the sys_id) where the stockroom is not empty, and the stockroom matches the value in the field trnsfr_from_stockroom and its state = 6 (In stock) and substatus = 'available', while. I know that for alm_hardware I need to use getRowCount() and if the number of rows is greater than the number placed in the trnsfr_qty variable field, then it allows the entry and results in "Good Job!" BUT if the number placed in the trnsfr_qty field is greater than the row count for the query, then to throw an error message "Not enough items in stock!" and it clears the value.
My problem right now is I have (hardware) 26 items of a model I selected in the model_transfer field.
I selected the proper from stockroom
I selected the stockroom transferring to (doesn't matter)
I typed in 2 -Good Job
I typed in 20 - Good Job
I typed in 100 - Good Job (This was not supposed to return true and throw this message)
Same result with consumables.
Here is my Client script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var stockroomId = g_form.getValue('variables.trnsfr_from_stockroom');
var modelId = g_form.getValue('variables.transfer_model');
var requestedQuantity = g_form.getValue('variables.trnsfr_qty');
var ga = new GlideAjax('InventorytransferUtils');
ga.addParam('sysparm_name', 'validateTransfer');
ga.addParam('sysparm_stockroomId', stockroomId); // Pass stockroomId as a parameter
ga.addParam('sysparm_modelId', modelId); // Pass modelId as a parameter
ga.addParam('sysparm_requestedQuantity', requestedQuantity); // Pass requestedQuantity as a parameter
ga.getXML(checkResults);
function checkResults(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'true') {
g_form.addErrorMessage('Good Job!');
this.document.getElementsByName('submit')[0].disabled = true;
} else
if (answer == 'false') {
g_form.addErrorMessage('Not enough items in stock!');
}
}
}
Here is he Script Include Function:
validateTransfer: function(ritm) {
var answer;
var stockroomId = this.getParameter('sysparm_stockroomId');
var modelId = this.getParameter('sysparm_modelId');
var requestedQuantity = this.getParameter('sysparm_requestedQuantity');
var conAsset = new GlideRecord('alm_consumable');
conAsset.addEncodedQuery('model_category=218323293743100044e0bfc8bcbe5d61^install_status=6^substatus=available^stockroom=' + stockroomId + '^model=' + modelId);
conAsset.query();
while (conAsset.next()) {
if (conAsset.model_category == '218323293743100044e0bfc8bcbe5d61' && conAsset.quantity >= requestedQuantity) {
answer = 'true'; // If the condition is met, return true.
}
}
// If the loop finishes without returning true, check alm_hardware.
var hardware = new GlideRecord('alm_hardware');
hardware.addEncodedQuery('install_status=6^substatus=available^stockroom=' + stockroomId + '^model=' + modelId);
hardware.query();
if(hardware.getRowCount() >= requestedQuantity){
answer = 'true'; // If the row count is greater, return true.
} else
answer = 'false';
// If neither condition is met, return false.
return answer;
},
Thanks in advance. I am active on here, so I will definitely give credit where due!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-30-2023 08:27 PM
Found the issue. it is not model_transfer.model_category, but is model_transfer.cmdb_model_category since it is the model that is being dot walked!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-29-2023 01:48 AM
Hi @Jimmy45 ,
Try below script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var stockroomId = g_form.getValue('variables.trnsfr_from_stockroom');
var modelId = g_form.getValue('variables.transfer_model');
var requestedQuantity = g_form.getValue('variables.trnsfr_qty');
var isConsumable = g_form.getValue('variables.model_transfer.model_category') == '218323293743100044e0bfc8bcbe5d61';
var ga = new GlideAjax('InventoryTransferUtils');
ga.addParam('sysparm_name', 'validateTransfer');
ga.addParam('sysparm_stockroomId', stockroomId);
ga.addParam('sysparm_modelId', modelId);
ga.addParam('sysparm_requestedQuantity', requestedQuantity);
ga.addParam('sysparm_isConsumable', isConsumable);
ga.getXML(checkResults);
function checkResults(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'true') {
g_form.addErrorMessage('Good Job!');
this.document.getElementsByName('submit')[0].disabled = true;
} else if (answer == 'false') {
g_form.addErrorMessage('Not enough items in stock!');
g_form.setValue('variables.trnsfr_qty', '');
}
}
}
validateTransfer: function() {
var answer;
var stockroomId = this.getParameter('sysparm_stockroomId');
var modelId = this.getParameter('sysparm_modelId');
var requestedQuantity = this.getParameter('sysparm_requestedQuantity');
var isConsumable = this.getParameter('sysparm_isConsumable') === 'true';
if (isConsumable) {
var conAsset = new GlideRecord('alm_consumable');
conAsset.addQuery('install_status', '6');
conAsset.addQuery('substatus', 'available');
conAsset.addQuery('stockroom', stockroomId);
conAsset.addQuery('model', modelId);
conAsset.query();
while (conAsset.next()) {
if (conAsset.quantity >= requestedQuantity) {
answer = 'true';
}
}
} else {
var hardware = new GlideRecord('alm_hardware');
hardware.addQuery('install_status', '6');
hardware.addQuery('substatus', 'available');
hardware.addQuery('stockroom', stockroomId);
hardware.addQuery('model', modelId);
hardware.query();
if (hardware.getRowCount() >= requestedQuantity) {
answer = 'true';
}
}
if (!answer) {
answer = 'false';
}
return answer;
},
Please mark it as solution proposed and helpful if its serves your purpose.
Thanks,
Anand
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-29-2023 09:28 AM
@Anand Kumar P - Tested the code and it throws a javascript error in the console. I am reviewing the code now. Thanks for your patience!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-29-2023 10:13 AM
@Anand Kumar P - I am no longer getting a javascript error in the console now, however, I am getting "Good Job" on any quantity I enter into the trnsfr_qty field, even if the value for trnsfr_qty is greater than the quantity of that model in that stockroom and install_status = 6 and substatus is "available." This makes me wonder if the script include is even reading the quantity field correctly from alm_consumable.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-29-2023 01:45 PM
After adding some log statements here is the updated code:
Catalog Client Script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var stockroomId = g_form.getValue('trnsfr_from_stockroom');
var modelId = g_form.getValue('model_transfer');
var requestedQuantity = g_form.getValue('trnsfr_qty');
var isConsumable = g_form.getValue('model_transfer.model_category') == '218323293743100044e0bfc8bcbe5d61';
var ga = new GlideAjax('InventorytransferUtils');
ga.addParam('sysparm_name', 'validateTransfer');
ga.addParam('sysparm_stockroomId', stockroomId);
ga.addParam('sysparm_modelId', modelId);
ga.addParam('sysparm_requestedQuantity', requestedQuantity);
ga.addParam('sysparm_isConsumable', isConsumable);
ga.getXML(checkResults);
function checkResults(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'true') {
g_form.addErrorMessage('Good Job!');
} else if (answer == 'false') {
g_form.addErrorMessage('Not enough items in stock!');
g_form.setValue('variables.trnsfr_qty', '');
}
}
// Logging for the Catalog Client Script
console.log("Requested Quantity from Catalog Client Script: " + requestedQuantity);
console.log("Is Consumable from Catalog Client Script: " + isConsumable);
}
Script include function being called:
validateTransfer: function() {
var answer = 'false'; // Default to false
var stockroomId = this.getParameter('sysparm_stockroomId');
var modelId = this.getParameter('sysparm_modelId');
var requestedQuantity = this.getParameter('sysparm_requestedQuantity');
var isConsumable = this.getParameter('sysparm_isConsumable') === 'true';
// Log parameters and values
gs.log("Parameter sysparm_stockroomId: " + stockroomId);
gs.log("Parameter sysparm_modelId: " + modelId);
gs.log("Parameter sysparm_requestedQuantity: " + requestedQuantity);
gs.log("Parameter sysparm_isConsumable: " + this.getParameter('sysparm_isConsumable'));
gs.log("Is Consumable: " + isConsumable);
if (isConsumable) {
var conAsset = new GlideRecord('alm_consumable');
conAsset.addQuery('install_status', '6');
conAsset.addQuery('substatus', 'available');
conAsset.addQuery('stockroom', stockroomId);
conAsset.addQuery('model', modelId);
conAsset.query();
var totalQuantity = 0;
while (conAsset.next()) {
totalQuantity += conAsset.quantity;
if (totalQuantity >= requestedQuantity) {
answer = 'true';
break; // Exit the loop when enough items are found
}
// Log quantity from alm_consumable
gs.log("Consumable Record - Quantity: " + conAsset.quantity);
}
} else {
var hardware = new GlideRecord('alm_hardware');
hardware.addQuery('install_status', '6');
hardware.addQuery('substatus', 'available');
hardware.addQuery('stockroom', stockroomId);
hardware.addQuery('model', modelId);
hardware.query();
if (hardware.getRowCount() >= requestedQuantity) {
answer = 'true';
}
}
// log statements
gs.log("Validation Result (answer): " + answer);
return answer;
},
The logs for the consumable I tried to trasnfer:
Is Consumable: false
Parameter sysparm_modelId: 7e754e2d1b6f2910fed48512f54bcbfd (I checked this, this is the correct sys_id for the model chosen in the model_transfer field.
Parameter sysparm_isConsumable: (This is empty, it doesn't seem to be reading the model_category)
Parameter sysparm_stockroomId: aaacc6841b82f090dffb859ce54bcb13 (This is the correct sys_id to the stockroom from trnsfr_from_stockroom field)
Validation Result (answer): false
Parameter sysparm_requestedQuantity: 10 (This consumable has 50 records as install_status = 6, and substatus = available.) It seems as it if is not reading if the model_category is consumable first, and second, it seems that is not reading the quantity correctly, which makes sense if it can't even determine if it is a consumable model or anything else.