Is there any way to simply get an attachment from the attachment db table and convert it to a CRM document that can be added to a sale, contact etc etc? Like, in a imaginary scenario I wish for a like createCRMDocumentEntityFromAttachmentId(1) method or function? Or just any other way to solve it without having to parse the document from the attachment body to a CRM Document.
I am trying to add all attachments from a request to the customer card.
All Replies (2)
A simple function for it doesn't exist I'm afraid.
You'll have to loop through all attachments, get the base64 encoded string, create a document using the NS classes and set the stream from the document.
You can create a function for it, though - something like this:
NSDocumentAgent docAgent;
Integer createCRMDocumentFromAttachment(Integer documentTypeId, Integer attachmentId, Integer contactId, Integer personId) {
Integer documentId = -1;
try {
Attachment att;
att.load(attachmentId);
NSStream att_stream = decodeBase64AsStream(att.getBase64());
NSDocumentEntity docEntity = docAgent.CreateDefaultDocumentEntity();
// Set document attributes
docEntity = docAgent.SaveDocumentEntity(docEntity);
docAgent.SetDocumentStream(docEntity, att_stream, true);
documentId = docEntity.GetDocumentId();
}
catch {
String errorMessage = "Exception caught: " + error + "...at " + errorLocation;
// Do error handling
}
return documentId;
}