Checking for attachments

Samuel Macedo
Giga Contributor

Greetings everyone,

I have the following problem.

I got one type of record that needs to be filled with specific attachments. So I made a section in the form with "File Attachment" fields because i need to keep track of which file was attached and which wasn't.

Something like this.find_real_file.png

I managed to make a Business Rule that checks if there are attachments missing, if so, it shows a message at the top of the screen saying "The following files are missing: " and it shows which ones are missing.

find_real_file.png

The problem is, if I attach a file, update the record and then remove the file, the form won't recognize that a file is missing anymore.

I don't understand why it does that.

Can someone help me? Tell me if you need more info.

1 ACCEPTED SOLUTION

Samuel Macedo
Giga Contributor

So... what I found out was that the File Attachment Field is a mess.

When the field is empty for the first time, it is ACTUALLY empty, so you can use the business rules condition like "IF FIELD IS EMPTY" and it'll work.

The problem is, when you upload a file and then delete it, the field doesn't become empty again. Instead it deletes the file from sys_attachment table (as expected) but the field itselft stays with the value of the sys_id of the last file (don't ask me why).

So I had to make a Insert/Update business rule with a script that searches for the sys_id of the file on the sys_attachment table, like this:

 var att = new GlideRecord('sys_attachment');
    att.addQuery('sys_id',current.u_file_attachment_1);
    att.query();
    if(att.getRowCount()>0){
        current.setValue('u_choice_1','true');
    }else{
        current.setValue('u_choice_1','false');
    }
 
Then I had to make an onDisplay business rule to see if the 'u_choice_1' is true or false. If it's false, it shows a message saying that the file is missing.
 
Thanks for the help everyone.

View solution in original post

9 REPLIES 9

Allen Andreas
Administrator
Administrator

Well, we have no information about your business rule...other than you have one. So a screenshot of the settings and/or script and all that would be a start.

Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Sorry about that! I should've done that haha

I made a OnDisplay Business Rule with the following script:

(function executeRule(current, previous /*null when async*/) {

// Add your code here
if (current == null) // to prevent null pointer exceptions.
return;

var PE = current.u_file_attachment_7;
var NF = current.u_file_attachment_4;
var RA = current.u_file_attachment_2;
var RC = current.u_file_attachment_8;

if(PE=="" || NF=="" || RA=="" || RC==""){
gs.addErrorMessage("Missing the following files: ");

if(PE==""){
gs.addErrorMessage(PE.getLabel());
}

if(RC==""){
gs.addErrorMessage(RC.getLabel());
}

if(RA==""){
gs.addErrorMessage(RA.getLabel());
}

if(NF==""){
gs.addErrorMessage(NF.getLabel());
}
}

AshishKM
Kilo Patron
Kilo Patron

Hi Samuel, how you are managing attachment type via business rule, business rule can check the file attached/or not attached for any records. Please share more information for your custom file type and business rule code.

 

 


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

I made a OnDisplay Business Rule that checks if each field is empty or not.

I don't know if there's a better way to do that.

This what I wrote:

(function executeRule(current, previous /*null when async*/) {


// Add your code here
if (current == null) // to prevent null pointer exceptions.
return;

var PE = current.u_file_attachment_7;
var NF = current.u_file_attachment_4;
var RA = current.u_file_attachment_2;
var RC = current.u_file_attachment_8;

if(PE=="" || NF=="" || RA=="" || RC==""){
gs.addErrorMessage("Missing the following files: ");

if(PE==""){
gs.addErrorMessage(PE.getLabel());
}

if(RC==""){
gs.addErrorMessage(RC.getLabel());
}

if(RA==""){
gs.addErrorMessage(RA.getLabel());
}

if(NF==""){
gs.addErrorMessage(NF.getLabel());
}
}