Tuesday, 17 August 2021

AX 2012 X++ Upload product image from code

 static void Job7(Args _args)

{

 DocuRef docuRef;

DocuValue docuValue;

InventTable inventTable;

EcoResProductImage ecoResProductImage;


System.String[] fileNames;

int fileCount, i;


str fileName, trimmedFileName, fileNameWithExt;

BinData binData;


binData = new BinData();

//fileNames = System.IO.Directory::GetFiles(strFmt(@"%1", StringFilePath.valueStr()));

fileNames = System.IO.Directory::GetFiles(@"C:\hh");



fileCount = fileNames.get_Length();


for (i=0; i<fileCount; i++)

{

fileName = fileNames.GetValue(i);



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

inventTable = InventTable::find(trimmedFileName);


if (inventTable)

{

binData.loadFile(fileName);


//docuValue.FileName = trimmedFileName;

docuValue.Name = trimmedFileName;


docuValue.FileType = "jpg";

docuValue.OriginalFileName = fileNameWithExt;

docuValue.File = binData.getData();

docuValue.insert();


docuRef.ValueRecId = docuValue.RecId;

docuRef.RefTableId = tableNum(InventTable);

docuRef.RefRecId = inventTable.RecId;

//docuRef.RefRecord = inventTable.RecId;

docuRef.RefCompanyId = curext();

docuRef.Name = trimmedFileName;

docuRef.TypeId = "File";

docuRef.insert();


ecoResProductImage.clear();

ecoResProductImage.DefaultImage = true;

ecoResProductImage.FileName = fileNameWithExt;

ecoResProductImage.ImageFormat = "jpg";

ecoResProductImage.RefRecId = docuRef.RecId;

ecoResProductImage.RefRecord = docuRef.RefRecId;

ecoResProductImage.Usage = 0; // Base Enum: 0=External, 1=Internal

ecoResProductImage.insert();


}

}

}


No comments:

Post a Comment