- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2025 01:23 AM
Hi All,
Need a help in validating the CSV file so I need to check whether the mandatory columns & rows are filled. I have written the code and it is fetching the mandatory column header but if 2 column headers are missing, it is showing only 1. Also, how can I validate all the row values so that it contains data?
Below is the script:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2025 06:03 AM
try this
var AttachmentSize = Class.create();
AttachmentSize.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkAttachment: function() {
var login = this.getParameter('sysparm_cart_item_id');
var table_name = "sc_cart_item";
gs.info('SS7' + table_name);
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_name', 'sc_cart_item');
gr.addQuery('sys_created_by', login);
gr.orderByDesc('sys_created_on');
gr.setLimit(1);
gr.query();
if (gr.next()) {
if (gr.getValue("size_bytes") > 10000) {
return "File size must be less than 10kb."; // Too big
} else if (gr.getValue("size_bytes") < 10000) {
if (table_name == "sc_cart_item") {
var a = [];
var table_sys_id = gr.table_sys_id;
gs.info('SS28' + table_sys_id + gr.size_bytes + gr.sys_created_on);
var gsa = new GlideSysAttachment();
var bytesInFile = gsa.getBytes(table_name, table_sys_id);
gs.info('SS33' + bytesInFile);
var originalContentsInFile = Packages.java.lang.String(bytesInFile);
gs.info('SS35' + originalContentsInFile);
originalContentsInFile = String(originalContentsInFile);
var fileData = originalContentsInFile.split('\n'); //split for new line
gs.info("SS38:" + " " + fileData);
var csvHeaders = fileData[0];
gs.info('SS40 Headers: ' + csvHeaders);
a = csvHeaders.split(',');
gs.info("length" + a.length);
for (i = 0; i < a.length; i++) {
gs.info("Array element: " + i + ", value: " + a[i]);
}
var fileLength = fileData.length;
gs.info('SS57' + fileLength);
// Check for missing headers
var mandatoryHeaders = ["TOWER", "ENTERED_HOSTNAME", "SWITCH_IP", "NETMASK", "MARSHA_CODE", "MODEL", "SNMP_CONTACT", "SNMP_LOCATION", "ISE_LOCATION"];
var missingHeaders = [];
for (var i = 0; i < mandatoryHeaders.length; i++) {
var currentHeader = mandatoryHeaders[i];
if (csvHeaders.indexOf(currentHeader) === -1) {
missingHeaders.push(currentHeader);
}
}
if (missingHeaders.length > 0) {
return "Missing headers: " + missingHeaders.join(', ');
}
// Validate each row
for (i = 1; i < fileData.length; i++) {
var rowDetails = fileData[i].trim();
if (rowDetails) { // Check if the row is not empty
var rowValues = rowDetails.split(',');
gs.info("Row: " + i + " : " + rowDetails);
gs.info('Row 1' + rowValues);
gs.info('Row 2' + rowValues.length);
if (rowValues.length !== mandatoryHeaders.length || rowValues.includes('')) {
return "Row " + (i + 1) + " is incomplete or missing data.";
}
}
}
return "All mandatory columns are filled and all rows contain data.";
}
}
}
},
});
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2025 10:41 PM
to enhance further as per your new requirement use this
var AttachmentSize = Class.create();
AttachmentSize.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkAttachment: function() {
var login = this.getParameter('sysparm_cart_item_id');
var table_name = "sc_cart_item";
gs.info('SS7' + table_name);
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_name', 'sc_cart_item');
gr.addQuery('sys_created_by', login);
gr.orderByDesc('sys_created_on');
gr.setLimit(1);
gr.query();
if (gr.next()) {
if (gr.getValue("size_bytes") > 10000) {
return "File size must be less than 10kb."; // Too big
} else if (gr.getValue("size_bytes") < 10000) {
if (table_name == "sc_cart_item") {
var a = [];
var table_sys_id = gr.table_sys_id;
gs.info('SS28' + table_sys_id + gr.size_bytes + gr.sys_created_on);
var gsa = new GlideSysAttachment();
var bytesInFile = gsa.getBytes(table_name, table_sys_id);
gs.info('SS33' + bytesInFile);
var originalContentsInFile = Packages.java.lang.String(bytesInFile);
gs.info('SS35' + originalContentsInFile);
originalContentsInFile = String(originalContentsInFile);
var fileData = originalContentsInFile.split('\n'); //split for new line
gs.info("SS38:" + " " + fileData);
// Check if the file is empty
if (fileData.length === 0 || (fileData.length === 1 && fileData[0].trim() === '')) {
return "The file is empty.";
}
var csvHeaders = fileData[0];
gs.info('SS40 Headers: ' + csvHeaders);
a = csvHeaders.split(',');
gs.info("length" + a.length);
for (i = 0; i < a.length; i++) {
gs.info("Array element: " + i + ", value: " + a[i]);
}
var fileLength = fileData.length;
gs.info('SS57' + fileLength);
// Check for missing headers
var mandatoryHeaders = ["TOWER", "ENTERED_HOSTNAME", "SWITCH_IP", "NETMASK", "MARSHA_CODE", "MODEL", "SNMP_CONTACT", "SNMP_LOCATION", "ISE_LOCATION"];
var missingHeaders = [];
for (var i = 0; i < mandatoryHeaders.length; i++) {
var currentHeader = mandatoryHeaders[i];
if (csvHeaders.indexOf(currentHeader) === -1) {
missingHeaders.push(currentHeader);
}
}
if (missingHeaders.length > 0) {
return "Missing headers: " + missingHeaders.join(', ');
}
// Validate each row and accumulate errors
var rowErrors = [];
for (i = 1; i < fileData.length; i++) {
var rowDetails = fileData[i].trim();
if (rowDetails) { // Check if the row is not empty
var rowValues = rowDetails.split(',');
gs.info("Row: " + i + " : " + rowDetails);
gs.info('Row 1' + rowValues);
gs.info('Row 2' + rowValues.length);
if (rowValues.length !== mandatoryHeaders.length || rowValues.includes('')) {
rowErrors.push("Row " + (i + 1) + " is incomplete or missing data.");
}
}
}
if (rowErrors.length > 0) {
return rowErrors.join('\n');
}
return "All mandatory columns are filled and all rows contain data.";
}
}
}
},
});
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2025 06:03 AM
try this
var AttachmentSize = Class.create();
AttachmentSize.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkAttachment: function() {
var login = this.getParameter('sysparm_cart_item_id');
var table_name = "sc_cart_item";
gs.info('SS7' + table_name);
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_name', 'sc_cart_item');
gr.addQuery('sys_created_by', login);
gr.orderByDesc('sys_created_on');
gr.setLimit(1);
gr.query();
if (gr.next()) {
if (gr.getValue("size_bytes") > 10000) {
return "File size must be less than 10kb."; // Too big
} else if (gr.getValue("size_bytes") < 10000) {
if (table_name == "sc_cart_item") {
var a = [];
var table_sys_id = gr.table_sys_id;
gs.info('SS28' + table_sys_id + gr.size_bytes + gr.sys_created_on);
var gsa = new GlideSysAttachment();
var bytesInFile = gsa.getBytes(table_name, table_sys_id);
gs.info('SS33' + bytesInFile);
var originalContentsInFile = Packages.java.lang.String(bytesInFile);
gs.info('SS35' + originalContentsInFile);
originalContentsInFile = String(originalContentsInFile);
var fileData = originalContentsInFile.split('\n'); //split for new line
gs.info("SS38:" + " " + fileData);
var csvHeaders = fileData[0];
gs.info('SS40 Headers: ' + csvHeaders);
a = csvHeaders.split(',');
gs.info("length" + a.length);
for (i = 0; i < a.length; i++) {
gs.info("Array element: " + i + ", value: " + a[i]);
}
var fileLength = fileData.length;
gs.info('SS57' + fileLength);
// Check for missing headers
var mandatoryHeaders = ["TOWER", "ENTERED_HOSTNAME", "SWITCH_IP", "NETMASK", "MARSHA_CODE", "MODEL", "SNMP_CONTACT", "SNMP_LOCATION", "ISE_LOCATION"];
var missingHeaders = [];
for (var i = 0; i < mandatoryHeaders.length; i++) {
var currentHeader = mandatoryHeaders[i];
if (csvHeaders.indexOf(currentHeader) === -1) {
missingHeaders.push(currentHeader);
}
}
if (missingHeaders.length > 0) {
return "Missing headers: " + missingHeaders.join(', ');
}
// Validate each row
for (i = 1; i < fileData.length; i++) {
var rowDetails = fileData[i].trim();
if (rowDetails) { // Check if the row is not empty
var rowValues = rowDetails.split(',');
gs.info("Row: " + i + " : " + rowDetails);
gs.info('Row 1' + rowValues);
gs.info('Row 2' + rowValues.length);
if (rowValues.length !== mandatoryHeaders.length || rowValues.includes('')) {
return "Row " + (i + 1) + " is incomplete or missing data.";
}
}
}
return "All mandatory columns are filled and all rows contain data.";
}
}
}
},
});
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2025 09:17 PM
Hope you are doing good.
Did my reply answer your question?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-22-2025 12:57 AM
Hi Ankur,
This is working fine. Just a small help needed in the code. Currently if my CSV has 1 data missing, the above code is printing that particular row. Suppose 2 values are missing from different rows, what change should we do in the code?
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2025 10:17 PM
Hi @Ankur Bawiskar , any idea how to check 2 missing rows or how to check for an empty file?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2025 10:33 PM
I believe I have answered your original question and subsequent question.
Please mark my appropriate responses as correct and now you can mark more than 1 response as correct.
The discussion can still continue on the thread.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader