
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-02-2024 11:52 PM
Hi All,
I have written the below BR OnAfter upon insert of attachment in sys_attachment table to validate if the file name has any supplier among the list of suppliers that I have and if it does then does it matches with the supplier name of the record to which the file is being attached.
There will be instances when a file name could contain partial name of suppliers. like for example instead of "Capgemini US LLC" it could just contain "capgemini validation tracker" etc. Even in such scenarios it should work to validate and allow if its the same supplier or not allow if the record to which the file is being attached is for another supplier.
This script you can run in background script. replacing the supplier_table name
When I execute this script ideally I should get the message "File name - tcs india ltd.pdf has incorrect supplier. please attach the correct file" however I see it is being attached
another thing I am noticing is I am seeing for the first 3 times this message which says 'not found' . What am I doing wrong?
Appreciate your assistance.
wrongSupplier: not found
var fileName = "tcs india ltd.pdf".toLowerCase();
var RightSupplier = "Angel Falls Pty Ltd".toLowerCase();
gs.addInfoMessage("Right Supplier: " + RightSupplier);
gs.addInfoMessage("File Name: " + fileName);
var wrongSupplier = "not found";
var supplierInFileName = false;
var rightSupplierInFileName = false;
var suplist = new GlideRecord("supplier_table");
suplist.query();
while (suplist.next()) {
var supplierName = suplist.supplier.toString().toLowerCase();
var fileNameParts = fileName.split('.')[0].split(/[\s-_]+/);
var rightSupplierParts = RightSupplier.split(/[\s-_]+/);
// Direct check for full supplier name
if (fileName.indexOf(supplierName) > -1) {
supplierInFileName = true;
gs.addErrorMessage('FileName 1: ' + supplierInFileName);
} else {
// Check if any part of the file name matches the supplier name
fileNameParts.forEach(function(part) {
if (part.length > 2 && supplierName.indexOf(part) > -1) {
supplierInFileName = true;
gs.addErrorMessage('FileName 2: ' + supplierInFileName);
}
});
}
// Direct check for RightSupplier
if (fileName.indexOf(RightSupplier) > -1) {
rightSupplierInFileName = true;
gs.addInfoMessage("FileName 3: " + rightSupplierInFileName);
} else {
// Check if any part of the file name matches the RightSupplier
fileNameParts.forEach(function(part) {
if (part.length > 2 && RightSupplier.indexOf(part) > -1) {
rightSupplierInFileName = true;
gs.addInfoMessage("FileName 4 - part : " + rightSupplierInFileName);
}
});
}
// Validate the supplier name against the record's supplier value
if (supplierInFileName) {
if (RightSupplier.includes(supplierName) && rightSupplierInFileName) {
wrongSupplier = false;
gs.addErrorMessage('Here: if');
break;
} else {
wrongSupplier = true;
gs.addErrorMessage('Here: ELSE ' + wrongSupplier);
}
}
gs.addErrorMessage("During loop - wrongSupplier: " + wrongSupplier + " Supplier: " + supplierName);
}
gs.addErrorMessagenfo("Final wrongSupplier: " + wrongSupplier);
if (wrongSupplier === true) {
gs.addErrorMessage("File Name " + fileName + " has incorrect Supplier. Please attach the correct file=2");
// var attachment = new GlideSysAttachment();
// var attachmentSysID = current.sys_id;
// attachment.deleteAttachment(attachmentSysID);
} else if (wrongSupplier === false || wrongSupplier === "not found" || !RightSupplier) {
gs.addInfoMessage('File Name: - ' + fileName + ' is attached to this record=1');
} else if ((fileName.indexOf(RightSupplier) == -1 && wrongSupplier === true)) {
gs.addErrorMessage("File Name " + fileName + " is Incorrect. Please attach the correct file=3");
// var attachment = new GlideSysAttachment();
// var attachmentSysID = current.sys_id;
// attachment.deleteAttachment(attachmentSysID);
} else if ((fileName.indexOf(RightSupplier) > -1 && wrongSupplier === false)) {
gs.addInfoMessage('File Name: - ' + fileName + ' is attached to this record=4');
}
Suggestions and hints to make it work and perform better is much appreciated.
Regards,
Imran
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-03-2024 11:48 AM
Got it. The next thing I'm seeing is that you are initializing wrongSupplier as a string "not found". Later you are sometimes setting the value to boolean true or false. With the number of records and loops you are doing this could get confusing, so I would change these to strings "true" and "false" throughout.
When I'm running this Fix Script now, using the table core_company and the Name field. When I have a Company record named "Angel Falls Pty Ltd", I'm getting this when it hits that record in the loop
Refusing message: FileName 2: true
Refusing message: FileName 4 - part : true
Refusing message: Here: if
GlideSession message was modified by sanitization. [message=File Name: - tcs india ltd.pdf is attached to this record=1][sanitized=File Name: - tcs india ltd.pdf is attached to this record=1]
Refusing message: File Name: - tcs india ltd.pdf is attached to this record
Then if I delete the Company record, the final logs are
Refusing message: FileName 4 - part : true
Refusing message: Here: ELSE true
Refusing message: During loop - wrongSupplier: true Supplier: total benefits
GlideSession message was modified by sanitization. [message=File Name tcs india ltd.pdf has incorrect Supplier. Please attach the correct file=2][sanitized=File Name tcs india ltd.pdf has incorrect Supplier. Please attach the correct file=2]
Refusing message: File Name tcs india ltd.pdf has incorrect Supplier. Please attach the correct file=2
Is this what you are expecting to happen? What are you seeing when you do and do not have a similar Supplier record?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-03-2024 04:03 AM
Are you certain on your test scenario? The fileName contains a partial match to the RightSupplier ("ltd"), so it should attach, correct?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-03-2024 04:52 AM - edited 09-03-2024 04:53 AM
Yes, Brad, I have around 1K + suppliers and some of them would have Ltd in their name likewise few would have US, LLC, Inc, etc.
But RightSupplier will always have complete name of the supplier and even the SupplierName will have complete name of the suppliers as I am not parting here.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-03-2024 06:08 AM
fileNameParts.forEach(function(part) {
if (part.length > 2 && RightSupplier.indexOf(part) > -1) {
rightSupplierInFileName = true;
gs.addInfoMessage("FileName 4 - part : " + rightSupplierInFileName);
}
});
As your logs show, "ltd" from part of the fileName is found within RightSupplier, so rightSupplierInFileName is true.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-03-2024 10:20 AM
RightSupplier won't validate to true as I have the complete name of the supplier under RightSupplier likewise in supplierName too I will have the complete name of the supplier.
The thinking behind it is to ascertain if the file name consist of any supplier from the supplier table and if it is same as the RightSupplier than allow it and if it is not then abort the uploading of attachment in the record. Hope I am able to convey the requirement.