Some users may experience random login-problems to our Community. We are investigating the root cause of this. If you get an error message from CloudFlare, please send the RayID in the message to support@superoffice.com. You may also clear your browser cookies and cashe to solve it. Thanks for your understanding. 

Convert Attachment to CRM Document

lock
push_pin
done
Answered
2

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. 

22 May 2025 | 12:34 PM

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;
}
23 May 2025 | 06:08 AM
Oh, okay good to know! Thank you :)
23 May 2025 | 06:51 AM

Add reply