X++ code for Product image upload in AX

Hi Folks,

In my previous post I shared test code to upload bulk images in AX through X++ code. Here is another set of code to achieve the same.

Create a new class with below methods.

 class ImageImport extends RunBase 
 { 
   // Packed variables 
   FilePath            filePath; 
   NoYesId             setDefault; 
   ItemIdEANExternalItemIdRadio  itemIdEANExternalItemIdRadio; 
   VendAccount           vendAccount; 
   // Dialog fields 
   DialogField               dlgfilePath; 
   DialogField               dlgsetDefault; 
   DialogField               dlgitemIdEANExternalItemIdRadio; 
   DialogField               dlgVendAccount; 
   #define.CurrentVersion(1) 
   #define.Version1(1) 
   #localmacro.CurrentList 
     filePath 
     ,setDefault 
     ,DMItemIdEANExternalItemIdRadio 
     ,vendAccount 
   #endmacro 
 } 
 public Object dialog() 
 { 
   DialogRunbase    dialog = super(); 
   dlgfilePath = dialog.addFieldValue(extendedTypeStr(FilePath),filePath); 
   dlgsetDefault = dialog.addFieldValue(extendedTypeStr(NoYesId),setDefault); 
   dlgsetDefault.label(@SYS132677); 
   dlgitemIdEANExternalItemIdRadio = dialog.addFieldValue(extendedTypeStr(itemIdEANExternalItemIdRadio),itemIdEANExternalItemIdRadio); 
   dlgvendAccount = dialog.addFieldValue(extendedTypeStr(VendAccount),vendAccount); 
   dlgvendAccount.enabled(NoYes::No); 
   return dialog; 
 } 
 public void dialogPostRun(DialogRunbase tmpdialog) 
 { 
 //this method is needed to make the vendor field editable if externalitem is choosen. 
 super(tmpdialog); 
 tmpdialog.dialogForm().formRun().controlMethodOverload(true); 
 tmpdialog.dialogForm().formRun().controlMethodOverloadObject(this); 
 } 
 public boolean fld3_1_modified() 
 { 
      if(dlgitemIdEANExternalItemIdRadio.value() == DMItemIdEANExternalItemId::ExternalItemId) 
      { 
             dlgVendAccount.enabled(NoYes::Yes); 
      } 
      else 
      { 
             dlgVendAccount.enabled(NoYes::No); 
      } 
      return NoYes::Yes; 
 } 
 public boolean getFromDialog() 
 { 
 ; 
   filePath  = dlgfilePath.value(); 
   setDefault  = dlgsetDefault.value(); 
   itemIdEANExternalItemIdRadio = dlgitemIdEANExternalItemIdRadio.value(); 
   vendAccount  = dlgvendAccount.value(); 
   return super(); 
 } 
 private ItemId getItemIdFromEAN(ItemBarCode _itemBarcode) 
 { 
   InventItemBarcode  inventItemBarcode; 
   select firstonly ItemId from inventItemBarcode 
   where inventItemBarcode.itemBarCode == _itemBarcode; 
   return inventItemBarcode.itemId; 
 } 
 private ItemId getItemIdFromExternalItemId(ExternalItemId _externalItemId, VendAccount _vendAccount) 
 { 
   CustVendExternal     custVendExternalItem; 
   Select firstonly ItemId from custVendExternalItem 
      where custVendExternalItem.ExternalItemId == _externalItemId  
      && custVendExternalItem.CustVendRelation == _vendAccount 
      && custVendExternalItem.ModuleType == ModuleInventPurchSalesVendCustGroup::Purch; 
   return custVendExternalItem.ItemId; 
 } 
 void insertImage(ItemId _itemId, Filename _fileName, NoYes _defaultImage = NoYes::No) 
 { 
   InventTable           inventTable = inventTable::find(_itemid); 
   DocuRef             docuRef; 
   EcoResProductImageManagement  ecoResProductImageManagement; 
   DocuType            docuType = DocuType::find('ItemImage'); //TODO: set the documenttype you provided for images 
   DocuActionArchive        docuActionArchive = new docuActionArchive(); 
   EcoResProductImage       ecoResProductImage; 
   try 
   { 
   //create docuref 
   docuRef.clear(); 
   docuRef.initValue(); 
   docuRef.TypeId      = docuType.TypeId; 
   docuRef.RefCompanyId   = curExt(); 
   docuRef.RefTableId    = inventTable.TableId; 
   docuRef.RefRecId     = inventTable.RecId; 
   docuRef.Name       = _fileName; 
   docuRef.Restriction   = DocuRestriction::External; 
   if(docuRef.validateWrite()) 
   { 
     docuRef.insert(); 
     if(setDefault == NoYes::Yes) 
     { 
       //making sure all other images are NOT standard, as only one can be standard image. 
        update_recordSet ecoResProductImage 
       setting DefaultImage = NoYes::No 
       where ecoResProductImage.RefRecord == docuRef.RefRecId; 
     } 
   } 
   ttsBegin; 
   docuActionArchive.add(docuRef,_fileName); 
   ttsCommit; 
     // For Image management purpose. - Start 
     ecoResProductImageManagement = EcoResProductImageManagement::construct(); 
     ecoResProductImageManagement.setEcoResImageValues(docuRef, EcoResProductImageUsage::External, setDefault); 
     //Delete imported file 
     new InteropPermission(InteropKind::ClrInterop).assert(); 
       WinAPI::deleteFile(_fileName); 
     CodeAccessPermission::revertAssert(); 
   } 
   catch 
   { 
     throw error(strFmt('Error with Image of item: %1', _itemId)); //TODO: make a label out of this string 
   } 
 } 
 public container pack() 
 { 
   return [#CurrentVersion,#CurrentList]; 
 } 
 public void run() 
 { 
 FileSystemAdapter    fileSystemAdapter; 
 str           imageFileName; 
 ItemId         itemId; 
 ; 
 fileSystemAdapter  = FileSystemAdapter::construct(filePath); 
 while (fileSystemAdapter.nextFile()) 
 { 
   itemId = ''; 
   switch(itemIdEANExternalItemIdRadio) 
   { 
     case itemIdEANExternalItemId::ItemId : 
       itemId = fileSystemAdapter.fileNameWithoutExtension(); 
       break; 
     case itemIdEANExternalItemId::EAN : 
       itemId = this.getItemIdFromEAN(fileSystemAdapter.fileNameWithoutExtension()); 
       break; 
     case itemIdEANExternalItemId::ExternalItemId : 
       if(vendAccount != '') 
       { 
         itemId = this.getItemIdFromExternalItemId(fileSystemAdapter.fileNameWithoutExtension(),vendAccount); 
         break; 
       } 
     default : itemId = ''; 
   } 
   imageFileName = fileSystemAdapter.fullFileName(); 
   if(itemId != '' && imageFileName != '' && InventTable::exist(itemId)) 
   { 
     this.insertImage(itemId,imageFileName, setDefault); 
   } 
 } 
 } 
 public boolean unpack(container packedClass) 
 { 
   Version version = RunBase::getVersion(packedClass); 
 ; 
   switch (version) 
   { 
     case #CurrentVersion: 
       [version,#CurrentList] = packedClass; 
       break; 
     default: 
       return false; 
   } 
   return true; 
 } 
 server static ImageImport construct() 
 { 
   return new ImageImport(); 
 } 
 static void main(Args args) 
 { 
   ImageImport  ImageImport = ImageImport::construct(); 
   if (ImageImport.prompt()) 
     ImageImport.run(); 
 }


2 comments:

  1. HI sir,
    Can you Please change the background color of this post.
    can't see...

    Thanks!

    ReplyDelete
    Replies
    1. Done, Thanks for your valuable feedback.

      Delete

Thanks