
- 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 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 07:28 AM - edited 09-03-2024 07:29 AM
I see that you defined the rightSupplierParts variable, but it's not used in your code:
var rightSupplierParts = RightSupplier.split(/[\s-_]+/);
Is it expected?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-03-2024 10:25 AM
Yes, this part is not used in the code now. Earlier I had it used within this part of the second set of validation.
// 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);
}
});
}