Kinetic Function to delete attachments from a job

I am working in the classic UI.

I am working on functions to be triggered when a user clicks the Documents button on the MES > Work Queue > Job Details panel.

When called, the function will delete any current attachments from JobHead that have a DoctypeID of “PRODMOCK” and will then add PRODMOCK attachments from the Sales Order Line to Job before the Documents form opens.

I’ve got the copying part working as expected. I never expected the deleting part to be the problem.

I’ve run the trace multiple times in Job Entry to try to identify the correct steps, but the trace has me stumped. In the UI, I right-click on the attachment in the tree and use the remove option from the context menu. The trace only shows 2 BO methods called. Ice.Proxy.BO.AttachmentImpl > GetPathReferences and Erp.Proxy.BO.JobEntryImpl > Update.

According to the trace, this removal is not being accomplished with a RowMod setting. The the attachment node for the removed attachment is simply removed from the parameter dataset. However, when I remove an attachment in the dataset and call the Update method, I get an error message “Update not allowed, Engineered and Prevent Changes.”

Any suggestions?

I have a BPM I wrote a while ago that deletes attachments from parts when copied, so it’s similar. The AttachmentSvc BO and Ice.XFileAttch table are what you’ll need.

I wrote this without testing, adjusting to work in a function and with jobs so YMMV. The basic outline is there on how you should be able to do it. I can’t remember if this actually deletes the underlying file or just “disappears” it from the UI, but the attachment BO also has a DeleteFile method that might do something interesting.

var jobNum = "test";
var xFileRefNums = Db.XFileAttch
  .Where((r) =>
    r.Company == Session.CompanyID
    && r.RelatedToSchemaName == "Erp"
    && r.RelatedToFile == "JobHead"
    && r.Key1 == jobNum)
  .Select((r) => r.XFileRefNum)
  .ToList();

CallService<AttachmentSvcContract>((svc) =>
{
  foreach (var xFileRefNum in xFileRefNums)
  {
    svc.DeleteByID(xFileRefNum);
  }
});
3 Likes