Tuesday 21 May 2019

Get file from table field which is a container type

Last time I had a task to modify an image which is displayed on collection letter.
Just simple change.
The issue was, that nobody at company had original image.

Image was stored in one of Dynamics AX tables, so I had to take it out from there

Below you can find a simple code how to get this image and save it to some disk location

static void GetFile_PKI(Args _args)
{

    CustCollectionLetterLine    collectionLetterLine;
    container                   image;    
    Bindata                     bin = new bindata();    
    str                         tmpImageHolder;    


    //I knew what RecId of that record is, so it's just simple select statement
    select collectionLetterLine 
        where collectionLetterLine.recId == 5637144577;

    //This is a table field where I have my image. Field is container type
    image           = collectionLetterLine.NotesLogo_PKI;
    
    bin.setData(image);
    
    // This will give me a string encoded as base64
    tmpImageHolder  = bin.base64Encode();

    // I've chosen .jpg extension but in fact it does not matter
    //You have to have a look on file header (for example in notepad) to get
    //information what type of file it is.
    AifUtil::saveBase64ToFile(@"C:\temp\test.jpg", tmpImageHolder);

}