Skip to content

Commit c8a9f57

Browse files
authored
Improve purchasing auto-resolving of vendor (#147)
1 parent 70012e2 commit c8a9f57

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

LabPurchasing/resources/queries/labpurchasing/referenceItems.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,28 @@ var LABKEY = require("labkey");
22
var console = require("console");
33

44
function beforeInsert(row, errors){
5-
// Allow resulting vendorId from name:
6-
if (row.vendorName && !row.vendorId) {
7-
LABKEY.Query.selectRows({
8-
schemaName: 'labpurchasing',
9-
queryName: 'vendors',
10-
columns: 'rowid',
11-
filterArray: [LABKEY.Filter.create('vendorName', row.vendorName)],
12-
sort: '-rowid',
13-
maxRows: 1,
14-
scope: this,
15-
success: function(results) {
16-
if (results.rows.length) {
17-
row.vendorId = results.rows[0].rowId;
5+
// The purpose of this is to allow the user to provide a string value for
6+
// vendorId or vendorName, and attempt to resolve this against known vendors:
7+
if (!row.vendorId || isNaN(row.vendorId)) {
8+
var vendorName = row.vendorName || row.vendorId;
9+
if (vendorName) {
10+
LABKEY.Query.selectRows({
11+
schemaName: 'labpurchasing',
12+
queryName: 'vendors',
13+
columns: 'rowid',
14+
filterArray: [LABKEY.Filter.create('vendorName', vendorName)],
15+
sort: '-rowid',
16+
maxRows: 1,
17+
scope: this,
18+
success: function(results) {
19+
if (results.rows.length) {
20+
row.vendorId = results.rows[0].rowId;
21+
}
22+
else {
23+
console.error("Unable to resolve vendor: " + vendorName);
24+
}
1825
}
19-
else {
20-
console.error("Unable to resolve vendorname: " + row.vendorName);
21-
}
22-
}
23-
})
26+
})
27+
}
2428
}
2529
}

0 commit comments

Comments
 (0)