Understanding the RunBase Framework in D365 F&O: Basic Requirements and Implementation

Rumman Ansari   Software Engineer   2024-09-22 04:03:04   572  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

RunBase framework in D365 F&O - Basic Requirement


class CountryPopulationBatchJob extends RunBaseBatch
{
    public static void main(Args _args){
         CountryPopulationBatchJob countryPopulationBatchJob = new CountryPopulationBatchJob();
        
         if(countryPopulationBatchJob.prompt()){
                countryPopulationBatchJob.run();
        }
    }

    static client server ClassDescription description()
    {
        return "Country Population";
    }

    public void run(){
       super();
       Info("Rumman is running");  
    }

    public container pack()  {
        return ["a", "b"];
    }

    public boolean unpack(container _packedClass)
    {     
      return true;
    }

}

Output:


Rumman is running

Solve below assignment:

You have to create a table (CountryPopulation) which is having below fields.
1. Name
2. Age
3. Gender
4. Address
5. Occupation
Now,
Create a batch job using RunBaseBatch framework which can insert data from a CSV file inside the table(CountryPopulation)

See my project structure:

project structure
Figure:

Create a table like below:

table for runbase
Figure:

Now see table output

runbase example
Figure:

Create a CSV file like below:

csv file
Figure:

write a CountryPopulationBatchJob class like below:


  class CountryPopulationBatchJob extends RunBaseBatch
{
    DialogRunbase                               dialog;
    DialogField                                 dialogDefinitionGroupUpdate;
    DMFDefinitionGroupName                      definitionGroupUpdate;
    Filename                                    filename;
    System.IO.Stream                            fileStream;
    SharedServiceUnitFileID                     fileID;
    DMFDefinitionGroupName                      definitionGroupName;

    #define.definitionGroupName("Country Population Import")
    #define.CurrentVersion(3)
    #localmacro.CurrentList
        definitionGroupUpdate,
        filename, fileID
    #endmacro

    private const str OkButtonName = 'OkButton';
    private const str FileUploadName = 'FileUpload';

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

        public static void main(Args _args){
         CountryPopulationBatchJob countryPopulationBatchJob = new CountryPopulationBatchJob();
        
         if(countryPopulationBatchJob.prompt()){
                countryPopulationBatchJob.run();
        }
    }

    static client server ClassDescription description()
    {
        return "Country Population";
    }

    protected FormControl getFormControl(DialogRunbase _dialog, str _controlName)
    {
        return _dialog.formRun().control(_dialog.formRun().controlId( _controlName));
    }

    public void run(){
       //super();
       //Info("Rumman is running");   

       FileUpload fileUploadControl = this.getFormControl(dialog, FileUploadName);
        FileUploadTemporaryStorageResult fileUploadResult = fileUploadControl.getFileUploadResult();

        if (fileUploadResult != null && fileUploadResult.getUploadStatus())
        {
            setPrefix(CountryPopulationBatchJob::description());
            info(strFmt("@SYS90632",DateTimeUtil::applyTimeZoneOffset(DateTimeUtil::utcNow(), DateTimeUtil::getUserPreferredTimeZone())));
            info(strfmt("@SYS79623", filename));
      
            using(System.IO.Stream stream = fileUploadResult.openResult())
            {
                if(stream)
                {
                    try
                    {
                        if(this.importFromCSV(stream))
                        {
                            info("@SYS70405");
                        }
                        else
                        {
                            error("@USV:FileImportError");
                        }
                    }
                    catch
                    {
                        error("@USV:FileImportError");
                    }
                }
                else
                {
                    error("@USV:FileImportError");
                }
            }
        }
    }

    private boolean importFromCSV(System.IO.Stream _stream){
        //Info("import");  
        System.IO.TextReader textReader = new System.IO.StreamReader(_stream);
        str line;
        container dataContainer;
        CountryPopulation countryPopulation;
        ttsBegin;

        //line = textReader.ReadLine();
        // Read each line of the CSV file and insert data into the table
        line = textReader.ReadLine();
        while( line != "" ){
            line = textReader.ReadLine();

         if(line == ''){
                 continue;}
        
            dataContainer = list2Con(strSplit(line, ','));
            // Ensure that the line has the correct number of fields
            if (conLen(dataContainer) == 5)
            {
                // CountryPopulation countryPopulation = new CountryPopulation();
                countryPopulation.Name = conPeek(dataContainer, 1);
                countryPopulation.Age = conPeek(dataContainer, 2);
                countryPopulation.Gender = conPeek(dataContainer, 3);
                countryPopulation.Address = conPeek(dataContainer, 4);
                countryPopulation.Occupation = conPeek(dataContainer, 5);
            
                countryPopulation.insert();
            }
        }

        ttsCommit;

        return true;
    }

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

    public boolean unpack(container _packedClass)
    {
        Integer version = conPeek(_packedClass, 1);

        switch (version)
        {
            case #CurrentVersion:
                [version, #CurrentList] = _packedClass;
                break;
            default:
                return false;
        }

        return true;
    }

    public Object dialog()
    { 
        DialogGroup      dialogGroup;
        FormBuildControl formBuildControl;
        FileUploadBuild  dialogFileUpload;
       

        dialog = new DialogRunbase(CountryPopulationBatchJob::description(), this);

        //dialogDefinitionGroupUpdate = dialog.addFieldValue(extendedTypeStr(DMFDefinitionGroupName), definitionGroupUpdate);
        //dialogDefinitionGroupUpdate.registerOverrideMethod(methodStr(FormStringControl, lookup), methodStr(CountryPopulationBatchJob, lookUpDefinitionGroup), this);
       // dialogDefinitionGroupUpdate.value(#definitionGroupName);

        dialogGroup = dialog.addGroup(CountryPopulationBatchJob::description());
        formBuildControl = dialog.formBuildDesign().control(dialogGroup.name());

        dialogFileUpload = formBuildControl.addControlEx(classstr(FileUpload), FileUploadName);
        dialogFileUpload.style(FileUploadStyle::MinimalWithFilename);
        dialogFileUpload.fileTypesAccepted('.xlsx,.xls,.xlsm,.csv');
        dialogFileUpload.baseFileUploadStrategyClassName(classstr(FileUploadTemporaryStorageStrategy));
        dialogFileUpload.fileNameLabel("@SYS308842");

        return dialog;
    }

}

Create a menu item and update it with the above class and make it as a startup object:

menu item
Figure:

Run the project and select a excel file like below

runbase example
Figure:

Now you can see below result:

runbase example
Figure:

See output in the table

after execution see result
Figure: