Creating Batch jobs using RunBaseBatch when importing data from a CSV file.

First create a new class and extend it from RunBaseBatch. i.e. class DemoBatchJob extends RunbaseBatch{}


In the class declaration add the following.

class DemoBatchJob extends RunbaseBatch
 { 
  DialogRunbase    dlg;
  DialogField       _file;

  FilenameOpen   file;
 #define.CurrentVersion(1)
 #localmacro.CurrentList
 file              //file variable is to keep in track the CSV file path for the batch job
 #endmacro
}


Override the pack method. 

 public container pack()
 {
   return [#CurrentVersion, #CurrentList];

Override the unpack method.

 public boolean unpack(container _packedClass)
 {
   Integer version = RunBase::getVersion(_packedClass);
   ;
   switch (version)
   {
       case #CurrentVersion:
           [version, #CurrentList] = _packedClass;
           break;
       default:
           return false;
   }
   return true;
}

Override the dialog method.

 protected Object dialog() 
{
  dlg = super();
    dlg.addText('Please select csv file:');
    _file = dlg.addField(ExtendedTypeStr(FilenameOpen));
    return dlg;

getFromDialog :

 public boolean getFromDialog()
   {
        boolean ret;
       
         ret = super();
        
         file = _file.value();
          return ret;
    }

Override validate method:

 public boolean validate(Object calledFrom = null)
 {
   boolean ret;
    
    ret = super(calledFrom);
   
      if (_file.value() == '')
      {
        warning('File path must be filled in.');
        ret = false;
       }
          return ret;
 }
Here validate method will validate that the file is selected or not if not then it will show the warning.

 Override construct:

 public static DemoBatchJob construct()
 {
  return new DemoBatchJob();
 }
   

Override the run method.

 public void run()
{
       if (strLwr(subStr(file,strLen(file)-2,3)) == 'csv')    //in this case it would be to import a csv file.
       {
         permission = new FileIOPermission(file,#io_read);
        
        permission.assert();
        
         commaTextIo = new CommaTextIO(file,'R');
         containFromRead = commaTextIo.read();
   //logic goes here i.e. as per requirement

commaTextIo = null;
info("Records inserted"));
      
}
   else
   {
   throw error('Not a valid csv file.');
   }
}
   
}

Create the static main method.

 public static void main(Args _args)
 {
    DemoBatchJob demoBatchJob =DemoBatchJob::construct();

       if (DemoBatchJob.prompt())
       {
          DemoBatchJob.run();
        }

Comments

  1. This comment has been removed by a blog administrator.

    ReplyDelete

Post a Comment

Popular posts from this blog

D365: SSRS Report Development/ Customization

D365: X++ code to add custom lookup on worker to show specific workers team workers only

Error message when you log on to a Microsoft Dynamics AX 4.0 client: "You are not a recognized user of Microsoft Dynamics AX"