Thursday, 5 December 2013

Upload Images to Inventory items : Programmatically.

The below code helps to upload the images and attach them to the inventory items via the docuRef programmatically.


static void Job1(Args _args)
{
    DocuRef     docuRef;
    DocuValue   docuValue;
    InventTable inventTable;
   
    System.String[] fileNames;
    int fileCount, i;
   
    str fileName, trimmedFileName;
    BinData binData;
   
    ;
   
    binData = new BinData();
    fileNames = System.IO.Directory::GetFiles(@"C:\Images");
   
    fileCount = fileNames.get_Length();
   
    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));
       
        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.File = binData.getData();
            docuValue.insert();
           
            docuRef.ValueRecId = docuValue.RecId;
            docuRef.RefTableId = tableNum(InventTable);
            docuRef.RefRecId = inventTable.RecId;
            docuRef.RefCompanyId = curext();
            docuRef.TypeId = "Image";
           
            docuRef.insert();
        }
    }
}

Cheers,
Nikhil

No comments:

Post a Comment