get product image in D365 FO x++




 public str getProductImage(ItemId _itemId)

    {
        str                 imageBase64;
        InventTable         inventTable = InventTable::find(_itemId);
        EcoResProduct       product;
        Common              firstRecord;
        Common              secondRecord;

        product             = EcoResProduct::find(inventTable.Product);
        firstRecord         = inventTable;
        secondRecord        = product;

        TableId             firstTableId = DocuRef::GetRootTableID(firstRecord.TableId);
        TableId             secondTableId = DocuRef::GetRootTableID(secondRecord.TableId);
        DocuRef             docuRef;
        DocuValue           docuValue;
        EcoResProductImage  ecoResProductImage;
        
        DocuRef             docuRefRow;
        EcoResProductImage  ecoResProductImageRow;

        while select ecoResProductImage
            where ecoResProductImage.DefaultImage == NoYes::Yes
            join docuRef
                where
                    docuRef.RecId == ecoResProductImage.RefRecId
                && ((docuRef.RefTableId == firstTableId
                    && docuRef.RefRecId == firstRecord.RecId
                    && docuRef.RefCompanyId == firstRecord.DataAreaId)
                ||
                    (docuRef.RefTableId == secondTableId
                    && docuRef.RefRecId == secondRecord.RecId
                    && docuRef.RefCompanyId == secondRecord.DataAreaId)
                )
            exists join docuValue
                where
                    docuValue.RecId == docuRef.ValueRecId
        {
            docuRefRow.data(docuRef);
            ecoResProductImageRow.data(ecoResProductImage);
            if (docuRef.RefTableId == firstTableId)
            {
                // We loop until we hit the first table (in case the first table is selected as the second row).
                // If we do not hit the first table, then second table row is used.
                // This logic could also be replaced by ordering
                // (depending on the order of firstTableId and secondTableId we would order our query,
                // but it would result in copying the query code or building it dynamically, which is not worth it).
                break;
            }
        }
        
        if (docuRefRow)
        {
            imageBase64 = this.getDocumentBase64String(docuRefRow);
        }
        else
        {
            imageBase64 = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAALQAAAC0CAMAAAAKE/YAAAAAMFBMVEX6+vrR2eH///+vvczw8vTX3uXN1d60wc/n6+/19vfDzdjr7vHc4ui+ydW5xdLh5uuLqaxwAAAE2klEQVR4nO2c6ZqrIAyGcQARcbn/uz0J4NKpQltodM6T70ddgdcQAgozQvw9/VwN8IkYm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8oyXXcIpc9DOWxsrVGDWeiOw8Nt6cxjp/KH1O2IP3fh0z9Ba7H6G3qAZfOpYEZs7fQINFjQeuokF/oLW+8Ml2GzQ7VLJCegGPGiErb8lXO416nRmLAsN1QibAWsMLLm4x4xuYOJTjN5ccIh1i3WjBN6Jz4BOk4OWYGHcelfBvHxhKeWhHUIDbz+MS2YQVbQ2S+noxY0/hFppMFpg+5v8DqQbB92iC7X6BBouNdKzGt3iji9saM/jeR5aoE8LN2FrX0Ieugx6a/QXjR6Ie1C4nAxAK7jdTBgSMJ20QBQb1jM0xkODbtTKGGNDog+jR9ftNm5bEwMmDEcqnrPLH0yqzsGN/vZ1p/N/D7rcilu/v/9xdsluRM/DRKnp6Y8GTN8ZEqmxGUIMzelG0A59bnqll/8IuquzFOlJ4Ekv3ccvAVRiaCoxNJUYmkoMTSWGphJDU4mhqcTQVGJoKjE0lRiaSgxNJYamEkNTiaGpxNBU+hE/f1D/AB15LBIOLRuUAAAAAElFTkSuQmCC";
        }

        return imageBase64;
    } private str getDocumentBase64String(DocuRef docuRef) { str base64String; System.IO.MemoryStream memoryStream; ; if(docuRef) { memoryStream = new System.IO.MemoryStream(); DocumentManagement::getAttachmentStream(docuRef).CopyTo(memoryStream); base64String = System.Convert::ToBase64String(memoryStream.ToArray()); } return base64String;     }

Comments

Popular posts from this blog

step 20 Update script for service model: AOSService on machine: CR-VM-Test-1

create movement journal through code in d365 FO x++