Solved

UploadFileMaintenance GetFile always returns null

  • 20 June 2023
  • 3 replies
  • 181 views

Userlevel 4
Badge +1

Hello, 

I’m trying to get file info from the UploadFileMaintenance graph using the code below.
Unfortunately, the GetFile and GetFileWithNoData methods always returns null for me and I cannot understand why, and I’m out of ideas of try.
I’ve added comments to the code to explain what I believe is happening. Can anyone explain what I’m doing wrong please?

//create the CABatchEntry graph, find the batch I'm interested in and set the graphs 'current' record to the record I've found.
CABatchEntry graph = PXGraph.CreateInstance<CABatchEntry>();
PXResultset<CABatch> pXResults = SelectFrom<CABatch>.Where<CABatch.batchNbr.IsEqual<@P.AsString>>.View.Select(new PXGraph(), batchID);
graph.Document.Current = (CABatch)pXResults.TopFirst;

//create the UploadFileMaintenance graph
var fileUploadGraph = PXGraph.CreateInstance<UploadFileMaintenance>();
//get a list of all FileIDs for the current record
var savedFilesIDs = PXNoteAttribute.GetFileNotes(graph.Document.Cache, graph.Document.Current);

//things appear to be working well at this point I have a 35 guids to loop through
foreach (Guid fileID in savedFilesIDs)
{
//the following 2 lines always returns null
var savedFileInfo = fileUploadGraph.GetFileWithNoData(fileID);
var savedFileInfo1 = fileUploadGraph.GetFile(fileID);
if (savedFileInfo != null)
{
fileUploadGraph.Files.AllowDelete = true;
fileUploadGraph.Files.Delete(fileUploadGraph.Files.Current);
fileUploadGraph.Persist();
}

}

Thanks

icon

Best answer by davidnavasardyan09 28 June 2023, 05:37

View original

3 replies

Userlevel 4
Badge +1

 I’ve been looking at this one further and attempted to debug using dnSpy. 

The screen shot above shows my code. Which gets the list of files attached to the document, then loops through the list of FileIDs and attempts to use the UploadFileMaintenance.GetFile to get the file details. 
The ‘Locals’ list shows the fileID variable is set to {f39f5b98-fd4a-4daa-9e96-0488d4f9bf7c} and I can see that this id exists in the database.

I press F11 to step into the code and appear here:
 

It looks like the fileID has not been set to the value sent to the method. This would explain why the method always returns null. But why isn’t the fileID variable being set properly?

Thanks
Steve

Badge +11

I can’t really see anything wrong with your code. I’ve used similar code before:

var files = PXNoteAttribute.GetFileNotes(cache, row);
var upload = PXGraph.CreateInstance<UploadFileMaintenance>();

foreach (Guid fileID in files)
{
FileInfo fileInfo = upload.GetFile(fileID);
if (fileInfo != null && !string.IsNullOrEmpty(fileInfo.Name))
{
using (MemoryStream stream = new MemoryStream(fileInfo.BinData))
{
// do stuff
}
break;
}
}

 

Userlevel 5
Badge +1

Hi @stephenward03 

This is quite unusual. The GetFile and GetFileWithNoData methods are designed to retrieve the file info based on the provided fileID. If the fileID exists in the database, as you've mentioned, these methods should not return null.

One possible issue could be that these methods are running in a different context and may not have access to the same database session where the fileID is valid. However, this would be quite uncommon and it's more likely that there's another issue at hand.

Here are a few things to check:

  1. Data Availability: Double-check that the fileID is in fact present in the UploadFileRevision table in the database.

  2. Graph Initialisation: The UploadFileMaintenance graph is typically not used programmatically. Instead, the PX.SM.FileInfo object and PXNoteAttribute are used to handle file-related operations. You could use the static method PXNoteAttribute.GetFileNotes to fetch file notes, and then retrieve file info using PX.SM.FileInfo. It's not clear from your example why you need UploadFileMaintenance graph, so consider if it's possible to use these more common approaches instead.

  3. Access Rights: Ensure that the user under which the code is running has the necessary access rights to fetch file info.

  4. Refresh the FileUploadGraph instance: You may want to try to refresh the UploadFileMaintenance graph instance for each iteration, although it's not an ideal solution performance-wise.

If none of the above works, I suggest you open a support case with Acumatica. It seems that you've encountered a somewhat peculiar issue that might require more in-depth troubleshooting. It would be helpful if you could provide any additional details about the context in which this code is running, such as if it's part of a customization project, running in a scheduled process, etc.

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved