Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

Update Number of Attachments Field

Avatar

Level 9


My form has a button for users to attach a file. Clicking the button opens a browse window allowing the user to select the file and attach it to the form. Each time this is done the NumericField1 increases by one. This shows users how many attachments there are.

I am trying to find script that wil update the NumericField1 if the user deletes one of the attachments. I can get it to work with a button but cannot get the script to run during the layout:ready event. How can I get this to run if the user deletes an attachment using the trash can icon in the attachments pane? Here's my button script:


var oObj = event.target;
var fileAttachments = oObj.dataObjects;
if(fileAttachments != null)
{
Attachments.NumericField1.rawValue = fileAttachments.length;
}
else
{
Attachments.NumericField1.rawValue = "0";
}


14 Replies

Avatar

Level 7

The code is working for me, however I put it on a click event on a button to test. I am guessing that the problem is that the event is not triggering at the correct time. What event do you have it on? I tried it on Initialize, Form Ready and Layout Ready. My guess is that the action of adding/removing an attachment is not triggering an event.

Avatar

Level 9

Yes - it works when clicking the button but not on any of the events I've tried (Form Ready, Layout Ready, Doc Ready) I agree - it appears deleting an attachment from the attachment pane does not seem to trigger an event. I was using a ListBox to display the file names and when deleting the user selected the name from the list and clicked a button. That has worked great except this new form connects to a database. For some reason when adding an attachment, saving and closing the form, then reopening the form and letting it connect to the database, the ListBox clears (blank). I have been trying for days to figure this out!

Avatar

Level 9

Additional Note: the ListBox was not connected to the database.

Avatar

Level 7

So if you are adding the names to a drop down list, when the user deletes an attachment it updates the drop down? if so you could count the entries in the drop down instead of the attachments. Now the question is why is the list box clearing? What event are you using to populate the list box?

Avatar

Level 9

Sorry for the late reply. I did not receive an e-mail telling me you posted again.

I have a button (click event) to populate the list box. I have used this script for years...

var d = new Date();

var myDoc = event.target;

var sFile = "myFile" + Math.floor(d/1000); 

  myDoc.importDataObject({cName: sFile});

var myDataObject = myDoc.getDataObject(sFile);

var sFileName = myDataObject.path;

  ListBox1.addItem(sFileName,sFile);

  NumericField1.rawValue = NumericField1.rawValue + 1;



When I open the form, I receive two pop-up messages - first one connects to Table1 (if I answer Yes) and the second connects to Table2 (if I answer Yes). If I add an attachment, save the form, close and reopen - answer yes to connect to both database tables - the attachment listbox is blank. If I answer No not to connect to Table 1 and Yes to connect to Table 2 - the listbox still has the attachment listed and works correctly.

Avatar

Level 7

Which Event is your script to populate the list on? I am suspecting that when DB connection is made the event is being overridden. I would look at other options and see if that solves the problem

Avatar

Level 9

The script that populates the ListBox one file at a time is the click event of the button.

When a user wants to attach a file, they click the "Attach a File" button. The "Open" browse window appears, the selected file is attached, the NumericField1.rawValue increase by 1 and the ListBox display the file name.

Avatar

Level 7

So if you open the file, add some attachments, but do not connect to the DB if you close and open the file the list is still good? If that is correct then the assumption is that the DB connection is resetting that list box. I have run into this in the past. I had to change my Run-time options. Try it with Automatic or Manual to see if that works. Another option is to store your list items in a field and re-import them into the list after you connect to the DB assuming that also does not get reset

Avatar

Level 9

I appreciate your help.

I tried switching from Automatic to Manual but the problem still exists.


Can you help me with the script to save the list items in a field and re-import them?

Avatar

Level 7

I would be happy to try. Can you send me your file?

Avatar

Level 9

Additional information:

  1. I have many drop-down list objects on this same form. None of them clear when connecting to the database the way the listbox clears.
  2. If I add a list item using LiveCycle Designer, that list item does not clear when connecting to the database. If I open the form and add an attachment - the attachment file name clears when connecting to the database but not the list item I added using Designer.
  3. When opening the form, if i only connect to  database table 2 (my form table data) and do not connect to table 1 (my form other fields) the listbox does not clear. 
  4. I tried removing all the database bindings to table 1 except the FileName but the problem still exists.

Avatar

Level 9

I added a TestField (not connected to the database) and script to sed the same data that goes to the listbox now goes to the TextField. The TextField only displays the file path not the display text (file name). When saving, reopening and connecting to the database - the TextField is still populated.

Avatar

Level 7

you have to use formattedValue instead of rawValue to get the file name.