How to link an image to an item in Item Master in AX 2012?
1) Copy the images in the required path.2) Create a new job to link images to items.
static void uploadProductImages(Args _args)
{
DocuRef docuRef;
DocuValue docuValue;
InventTable inventTable;
EcoResProductImage ecoResProductImage;
System.String[] fileNames;
int fileCount, i;
str fileName, trimmedFileName, fileNameWithExt;
BinData binData;
str filePath;
System.IO.FileInfo fileInfo;
EcoResProduct ecoResProduct;
EcoResProductImageManagement productImageManagement;
EcoResProductImageThumbnail ecoResProductImageThumbnail;
filePath = 'Specify the path where your images are saved here.';
// File path should be provided as : C:\\Images\\Car.jpg
binData = new BinData();
fileNames = System.IO.Directory::GetFiles(filePath);
fileCount = fileNames.get_Length();
ttsBegin;
for (i=0; i<fileCount; i++)
{
fileName = fileNames.GetValue(i);
// Get only the file name. If value returned is C:\Images\Car.jpg, get only Car.jpg
trimmedFileName = substr(fileName, strscan(fileName, '\\', strlen(fileName),
-strlen(fileName))+ 1, strlen(fileName));
fileNameWithExt = trimmedFileName;
if (trimmedFileName)
{
// Assuming file extension is always .jpg, removing it
trimmedFileName = strreplace(trimmedFileName, ".jpg", "");
}
// assuming image name matches item name in AX
ecoResProduct = EcoResProduct::findByDisplayProductNumber(trimmedFileName);
inventTable = InventTable::findByProduct(ecoResProduct.RecId);
if (inventTable)
{
binData.loadFile(fileName);
docuValue.FileName = trimmedFileName;
docuValue.FileType = "jpg";
docuValue.OriginalFileName = fileNameWithExt;
docuValue.File = binData.getData();
docuValue.insert();
docuRef.ValueRecId = docuValue.RecId;
docuRef.RefTableId = tableNum(InventTable);
docuRef.Name = trimmedFileName;
docuRef.RefRecId = inventTable.RecId;
docuRef.RefCompanyId = inventTable.dataAreaId;
docuRef.TypeId = "Image";
docuRef.insert();
ecoResProductImage.clear();
ecoResProductImage.DefaultImage = true;
ecoResProductImage.FileName = fileNameWithExt;
ecoResProductImage.ImageFormat = "jpg";
ecoResProductImage.RefRecId = docuRef.RecId;
ecoResProductImage.RefRecord = docuRef.RefRecId;
ecoResProductImage.Usage = EcoResProductImageUsage::External;
//0; // Base Enum: 0=External, 1=Internal
// To make the thumbnail size
ecoResProductImageThumbnail = new EcoResProductImageThumbnail(false);
ecoResProductImage.MediumSize = ecoResProductImageThumbnail.generateThumbnail(204,204,docuRef);
ecoResProductImage.ThumbnailSize = ecoResProductImageThumbnail.generateThumbnail(48,48,docuRef);
ecoResProductImage.insert();
info(strFmt("%1 - %2", docuValue.RecId, docuRef.RecId));
}
}
ttsCommit;
}
3) Make sure you have a document type of type "Image"(as we are linking images to items) created in
Organization Administration > Setup > Document Management > Document Types.
Thanks & Regards
Sindhu