try
{
int iOrderNum = System.Convert.ToInt32(this.orderNum);
int iOrderLine = System.Convert.ToInt32(this.orderLine);
var oHedGuid = new System.Guid();
string strfolder = @“C:\INPUT_FOLDER”;
string strProcessedFolder = @“C:\PROCESS_FOLDER”;
string strINSFileName = this.partNum + “.jpg”;
string strINSOrigFileName = this.partNum + “.jpg”;
string strFullFilePath = strfolder + strINSFileName;
bool bFileExists = false;
string strXfileName = “”;
int iAttachNum = 0;
int iXFileRefNum = 0;
//find the guid for the row we want to add the attachment to
var orderHedRow = Db.OrderHed.FirstOrDefault(odtl=> odtl.Company == Session.CompanyID && odtl.OrderNum == iOrderNum);
if (orderHedRow != null)
{
oHedGuid = orderHedRow.SysRowID;
}
//make sure the file exists
string inspFileExists = System.IO.File.Exists(strFullFilePath).ToString();
if (inspFileExists == “True”)
{
//covert the file to byte array
var bytes = System.IO.File.ReadAllBytes(strFullFilePath);
//initiate the attachments service
this.CallService<Ice.Contracts.AttachmentSvcContract>(attch=>
{
//check to see if the file exists
attch.DocStarFileExistsForTableRow("DOOR_IMG", "OrderHed",strINSFileName,oHedGuid, out strXfileName, out iAttachNum);
//if the file exists it will return an attachment number
if (iAttachNum > 0)
{
bFileExists = true;
//find xFileRefNum
//I'm not sure if this can be found from the method or if DB lookup is the only way
var XFileAttch = Db.XFileAttch.FirstOrDefault(x=> x.Company == Session.CompanyID && x.AttachNum == iAttachNum);
iXFileRefNum = XFileAttch.XFileRefNum;
}
System.Collections.Generic.Dictionary<string, string> mylist = new System.Collections.Generic.Dictionary<string, string>();
//The file exists to we want to upload a new ECM Version
if (bFileExists)
{
var attachDataSetVer = attch.DocStarUploadFileAsVersion(iXFileRefNum, strINSFileName, bytes, "DOOR_IMG","OrderHed",mylist);
// strINSFileName = attachDataSetVer.ToString();
}
else
{
//File does not already exists so create new ECM Attachment (note that this creates the ECM record but not the ERP Record)
var attachDataSet = attch.DocStarUploadFile(strINSFileName, bytes, "DOOR_IMG","OrderHed",mylist );
//return the ECM File name
strINSFileName = attachDataSet;
}
//initiate the sales order entry service
this.CallService<Erp.Contracts.SalesOrderSvcContract>(
od=>
{
var odDs = od.GetByID(iOrderNum);
//od.GetNewOrderDtlAttch(ref odDs, iOrderNum, iOrderLine);
od.GetNewOrderHedAttch(ref odDs, iOrderNum);
if (odDs != null && bFileExists == false)
{
//loop not required in this instance but could be for other attachment types
var dsAtt = odDs.OrderHedAttch.Where(row=> row.RowMod == "A");
foreach (var dsAttRow in dsAtt)
{
//The file name is the ECM File name
dsAttRow.FileName = strINSFileName;
dsAttRow.DrawDesc = "Door Image - " + this.partNum;
dsAttRow.DocTypeID = "DOOR_IMG";
}
od.Update(ref odDs);
}
//od end
});
//attch end
});
//tidy up the files that we just processed
if (System.IO.File.Exists(strProcessedFolder + strINSOrigFileName))
{
System.IO.File.Delete(strProcessedFolder + strINSOrigFileName);
System.IO.File.Move(strfolder + strINSOrigFileName,strProcessedFolder + strINSOrigFileName);
}
else
{
System.IO.File.Move(strfolder + strINSOrigFileName,strProcessedFolder + strINSOrigFileName);
}
}
}
catch (Exception ex)
{
//throw; //silent fail
Ice.Diagnostics.Log.WriteEntry(String.Format("Dev Environment - Order attachment process ERROR: " + ex.Message)); //write to Event Log
string strErrorEmailSubject = "Dev Environnment - Order attachment process ERROR: " + this.orderNum ;
var mailer = this.GetMailer(async: true);
var message = new Ice.Mail.SmtpMail();
message.SetTo("support teams email address");
message.SetSubject(strErrorEmailSubject);
message.SetBody("There was an error attaching the image: " + ex.Message);
mailer.Send(message);
}