Skip to main content
Answer

Files - How to access ADD LINK button from Code Level

  • September 2, 2024
  • 2 replies
  • 73 views

Forum|alt.badge.img

Hello Everyone,

We have requirement to add the Files in the FILES section using the ADD LINK button. Can you please help me how to add the Files using ADD LINK button from Code?

 

 

Best answer by taras

Hi, Add Link button creates link between File and some Entity. To create this link from code you need to insert into NoteDoc NoteID of an Entity and FileID of a File. You can use code of Add Link button: 
 

    protected IEnumerable addLink(PXAdapter a)
{
try
{
PXFilter<FileDialog> fileDialog = FileDialog;
if (fileDialog != null && (fileDialog.Current?.Entity).HasValue && Files.Current != null)
{
PXDatabase.Insert<NoteDoc>(new PXDataFieldAssign[2]
{
new PXDataFieldAssign("NoteID", PXDbType.UniqueIdentifier, FileDialog?.Current?.Entity),
new PXDataFieldAssign("FileID", PXDbType.UniqueIdentifier, Files.Current.FileID)
});
}

return a.Get();
}
catch (PXDatabaseException ex)
{
if (ex.ErrorCode != PXDbExceptions.PrimaryKeyConstraintViolation)
{
throw ex;
}

throw new Exception("Can't insert link more than once", ex);
}
}

 

2 replies

Forum|alt.badge.img+1
  • Jr Varsity I
  • Answer
  • September 3, 2024

Hi, Add Link button creates link between File and some Entity. To create this link from code you need to insert into NoteDoc NoteID of an Entity and FileID of a File. You can use code of Add Link button: 
 

    protected IEnumerable addLink(PXAdapter a)
{
try
{
PXFilter<FileDialog> fileDialog = FileDialog;
if (fileDialog != null && (fileDialog.Current?.Entity).HasValue && Files.Current != null)
{
PXDatabase.Insert<NoteDoc>(new PXDataFieldAssign[2]
{
new PXDataFieldAssign("NoteID", PXDbType.UniqueIdentifier, FileDialog?.Current?.Entity),
new PXDataFieldAssign("FileID", PXDbType.UniqueIdentifier, Files.Current.FileID)
});
}

return a.Get();
}
catch (PXDatabaseException ex)
{
if (ex.ErrorCode != PXDbExceptions.PrimaryKeyConstraintViolation)
{
throw ex;
}

throw new Exception("Can't insert link more than once", ex);
}
}

 


Naveen Boga
Captain II
Forum|alt.badge.img+19
  • Captain II
  • September 4, 2024

@nsmith51  Have you tried with the SetFileNotes?