PO attachment from Requisition attachment

We use Docstar for all Epicor attachments. Is it possible to make it so that when a PO is created from a requisition, the attachment in the Req automatically gets added to the PO? It is not clear to me if that is something we can enable or if it is just a feature that is missing in Epicor 10.2.700.

You should be able to do it with a BPM… though I’m not familiar with Docstar nuances.

I’ve got (non-docstar) attachments copying from Operations to POs.


It’s a little tricky as it handles multiple attachments from the same operation, triggered by the PO line, but adds them to the PO Header. And makes sure no duplicates.

I was trying to solve this exact problem this week but couldn’t find any information. I managed to get this working by downloading the file from DocStar off of the requisition, then reuploading it as a PO header attachment instead. This does create a copy of the original file but that’s fine for my use case. The code below is run inside of a standard data directive on the PO header.

Action<string, string, string, Guid> AttachFile = (filePath, fileName, poNumber, poGuid) =>
{
  using (var txScope = IceDataContext.CreateDefaultTransactionScope())
  {
    var xFileRef = new XFileRef();
    Db.XFileRef.Insert(xFileRef);
    xFileRef.Company = Session.CompanyID;
    xFileRef.XFileName = filePath;
    xFileRef.XFileDesc = fileName;
    xFileRef.DocTypeID = "EP_PO";
    Db.Validate(xFileRef);
    
    var xFileAttch = new XFileAttch();
    Db.XFileAttch.Insert(xFileAttch);
    xFileAttch.Company = Session.CompanyID;
    xFileAttch.RelatedToSchemaName = "Erp";
    xFileAttch.RelatedToFile = "POHeader";
    xFileAttch.Key1 = poNumber;
    xFileAttch.ForeignSysRowID = poGuid;
    xFileAttch.XFileRefNum = xFileRef.XFileRefNum;
    Db.Validate(xFileAttch);
    
    txScope.Complete();
  }
};

var attchSvc = Ice.Assemblies.ServiceRenderer.GetService<Ice.Contracts.AttachmentSvcContract>(Db);
var headers = this.ttPOHeader.Where((x) =>
  x.Company == Session.CompanyID
  && string.Equals(x.RowMod, IceRow.ROWSTATE_ADDED, StringComparison.OrdinalIgnoreCase));

foreach (var header in headers)
{
  var reqNums = this.Db.ReqDetail
    .Where((x) =>
      x.Company == header.Company
      && x.PONUM == header.PONum)
    .Select((x) => x.ReqNum)
    .Distinct();
    
  foreach (var reqNum in reqNums)
  {
    var xFileRefs = this.Db.XFileRef
      .Where((x) =>
        x.Company == Session.CompanyID
        && this.Db.XFileAttch.Any((y) =>
          y.Company == Session.CompanyID
          && y.XFileRefNum == x.XFileRefNum
          && y.RelatedToSchemaName == "Erp"
          && y.RelatedToFile == "ReqHead"
          && y.Key1 == reqNum.ToString()))
      .Select((x) => new
      {
        x.XFileRefNum,
        x.XFileDesc
      });
    
    var metadata = new Dictionary<string, string>();
    foreach (var xFileRef in xFileRefs)
    {
      var fileData = attchSvc.DocStarDownloadFile(xFileRef.XFileRefNum, ref metadata);
      
      metadata.Clear();
      metadata.Add("_TableName", "POHeader");
      metadata.Add("PONum", header.PONum.ToString());
      metadata.Add("Keywords", header.PONum.ToString());
      metadata.Add("OrderDate", header.OrderDate.ToString());
      
      var docPath = attchSvc.DocStarUploadFile(
        xFileRef.XFileDesc,
        fileData,
        "EP_PO",
        "POHeader",
        metadata);
      
      AttachFile(
        docPath,
        xFileRef.XFileDesc,
        header.PONum.ToString(),
        header.SysRowID);
    }
  }
}