Add an attachment to a Document (e.g. a Delivery)

Paolo Manfrin

In this sample I would like to show a piece of code how to add an attachment to a Delivery via DI



// attach the PDF to the Delivery document
string exportPathForSAP = Path.GetTempPath().Substring(0, Path.GetTempPath().Length - 1);
SAPbobsCOM.Documents oDelivery = (SAPbobsCOM.Documents)SwissAddonFramework.B1Connector.GetB1Connector().Company.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oDeliveryNotes);
oDelivery.GetByKey(Convert.ToInt32(DocEntry));
SAPbobsCOM.Attachments2 oAttachment = (SAPbobsCOM.Attachments2)SwissAddonFramework.B1Connector.GetB1Connector().Company.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oAttachments2);
if (oAttachment.GetByKey(oDelivery.AttachmentEntry))
{
// the document has already attachments
oAttachment.Lines.Add();
oAttachment.Lines.SetCurrentLine(oAttachment.Lines.Count-1);
oAttachment.Lines.FileName = "PDF_ " + this.MessageReference + ".pdf";
oAttachment.Lines.SourcePath = exportPathForSAP;
oAttachment.Lines.Override = SAPbobsCOM.BoYesNoEnum.tYES;
if (oAttachment.Update()!=0)
{
string errMsg = SwissAddonFramework.B1Connector.GetB1Connector().Company.GetLastErrorDescription();
SwissAddonFramework.Messaging.StatusBar.WriteError(errMsg);
}
System.Diagnostics.Process.Start(exportPath);
}
else
{ // the document has no attachments assigned so far
oAttachment.Lines.FileName = "PDF_ " + this.MessageReference + ".pdf";
oAttachment.Lines.SourcePath = exportPathForSAP;
if (oAttachment.Add() != 0)
{
string errMsg = SwissAddonFramework.B1Connector.GetB1Connector().Company.GetLastErrorDescription();
SwissAddonFramework.Messaging.StatusBar.WriteError(errMsg);
}
string objKey = SwissAddonFramework.B1Connector.GetB1Connector().Company.GetNewObjectKey();
oAttachment.GetByKey(Convert.ToInt32(objKey));
int absEntry = oAttachment.AbsoluteEntry;
oDelivery.AttachmentEntry = oAttachment.AbsoluteEntry;
if (oDelivery.Update() != 0)
{
// error
string errMsg = SwissAddonFramework.B1Connector.GetB1Connector().Company.GetLastErrorDescription();
SwissAddonFramework.Messaging.StatusBar.WriteError(errMsg);
return;
}
SwissAddonFramework.Messaging.StatusBar.WriteSucess("Attachment added succesful to Delivery " + DocNum);
System.Diagnostics.Process.Start(exportPath);
}

Enjoy!



Jan_de_Lange

Hello Paolo,


 


Thats just what I was looking for. How/where do Iimplement this code in Designer?


 


 


 Kind regards


 


Jan de Lange



Paolo Manfrin

Hi Jan, 


you do not implement this in designer but rather in customize.


You can link it to an optimizer rule or a function button rule for example.


cheers


paolo



Jan_de_Lange

Thanks, I have edited a customise rule but there is a compiler error as the result of that.


 


if (oAttachment.Update()! = 0) gives various errors:


 


What did i do wrong? or is there something missing?


Paolo Manfrin

Hi Jan,


please post some screenshots and the errors you get. 


Thank You


paolo



0

Comments

0 comments

Please sign in to leave a comment.

Didn't find what you were looking for?

New post