This is the code I use for quickly attaching something from BPM land. I used a Func becuse I was doing multiple attachments, but maybe someday we’ll have global functions we can just call on like our own widgets… achem @Bart_Elia hint hint
Mine pulls from a UNC directory for it’s reference, but if you want to write standard Csharp to move the file or instead use the BOs and the attachment type for that you can do that as well.
Func<string, Guid, string, string, int> AttachFile = (iFilename, iRelatedGuid, iRelatedTable, iKey1) =>
{
int outAttchNum = 0;
using (var txscope = IceDataContext.CreateDefaultTransactionScope())
{
// Create the system file reference
XFileRef xref = new XFileRef();
Db.XFileRef.Insert(xref);
xref.Company = callContextClient.CurrentCompany;
xref.XFileName = $@"\\server\path\{iFilename}";
xref.XFileDesc = iFilename;
xref.DocTypeID = "CUSTOGPO";
Db.Validate(xref);
// Attach the file to the record
XFileAttch xfile = new XFileAttch();
Db.XFileAttch.Insert(xfile);
xfile.Company = callContextClient.CurrentCompany;
xfile.RelatedToSchemaName = "Erp";
xfile.RelatedToFile = iRelatedTable;
xfile.Key1 = iKey1.ToString();
xfile.ForeignSysRowID = iRelatedGuid;
xfile.XFileRefNum = xref.XFileRefNum;
Db.Validate(xfile);
// Store the new attachment number for a rainy day
outAttchNum = xfile.AttachNum;
txscope.Complete();
}
return outAttchNum;
};
int attchnum = AttachFile(ud11.Key2, std.OrderHed[0].SysRowID, "OrderHed", std.OrderHed[0].OrderNum.ToString());