Skip to main content

Notifications

Dynamics 365 Community / Forums / Finance forum / RunBaseBatch pack unpa...
Finance forum
Answered

RunBaseBatch pack unpack variables coming from a form

Posted on by 250
Hello,
I have implemented a runbasebatch class which takes values from a form.
In the Main method logic, I have initialized the variables from the form args.
It works perfectly fine in non-batch mode.
However, the variables are set to null when the class is run in batch mode.
I have tried to pack and unpack the variables in a list and version.
However, I am having a weird issue where the variables are being initialized from the 1st run values.
If ever a variable value has been initialized with a new value, it is still having the value which was set during the 1st run when going through the unpack logic.
  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 227,979 Super User 2024 Season 2 on at
    RunBaseBatch pack unpack variables coming from a form
    Serialization (pack/unpack) and saving last values are two different things. Serialization is necessary, e.g. to serialize the data before saving for batch processing. Saving last dialog values and showing them in the dialog next is optional and can be disabled.
  • Jason0810M Profile Picture
    Jason0810M 250 on at
    RunBaseBatch pack unpack variables coming from a form
    The issue is as follows now:
    I select Value1 for the variable from the caller formrun for the 1st time run.
    The value gets packed and if I run the class a second time with Value2 selected from the caller formrun for the variable, Value1 is still being used as it was packed previously.
    Is it possible to call the method this.saveLast() in the method the initFromArgs() -> method which is retrieving the variable values?
    This could save the values that are being retrieved from the caller formrun.
  • Martin Dráb Profile Picture
    Martin Dráb 227,979 Super User 2024 Season 2 on at
    RunBaseBatch pack unpack variables coming from a form
    It's not how I would do it. You're doing at a different logical place than the functionality you want to change (loading last values vs. data deserialization), which will make it harder to discover and may even lead to unwanted behavior.
     
    I would ask whether we ever want to save values and load them the next time. If not, I would simply disable the saving.
     
    If you want to conditionally disable loading, setting getLastCalled variable to true may be a good option.
  • Jason0810M Profile Picture
    Jason0810M 250 on at
    RunBaseBatch pack unpack variables coming from a form
    Hello Martin,
    I have tried a possible solution and it seems to work.
    I have added a boolean variable calledFromForm and set it to true only if the user has called the functionality from the form (i.e. when the initFromArgs method is called).
     
    I have then added this logic in the beginning of the unpack method (no need to unpack the stored variable values as they are being assigned from the called form):
    if (calledFromForm)
    {
    return false;
    }
     
    Could you indicate if it is appropriate to modify the unpack method as I did?
  • Martin Dráb Profile Picture
    Martin Dráb 227,979 Super User 2024 Season 2 on at
    RunBaseBatch pack unpack variables coming from a form
    When you debug your code, you'll likely see that the values are overwritten inside prompt(), because the system loads previously used values.
  • Jason0810M Profile Picture
    Jason0810M 250 on at
    RunBaseBatch pack unpack variables coming from a form
    Hello Martin,
    Please find the class logic
     
    public class TestClass extends RunBaseBatch implements BatchRetryable
    {
        FromDate            fromDate;
        ToDate              toDate;

        #define.CurrentVersion(1)
        #localmacro.CurrentList
            fromDate,
            toDate
        #endmacro
        
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public container pack()
        {
            return [#CurrentVersion, #CurrentList];
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name = "packedClass"></param>
        /// <returns></returns>
        public boolean unpack(container packedClass)
        {
            Version     version = RunBase::getVersion(packedClass);
            switch (version)
            {
                case #CurrentVersion:
                    [version, #CurrentList] = packedClass;
                    break;
                default:
                    return false;
            }
            return true;
        }
        
        /// <summary>
        ///
        /// </summary>
        /// <param name = "_args"></param>
        protected void initFromArgs(Args _args)
        {
            if (_args && _args.caller())
            {
                FormRun formRun = _args.caller();
                callerDS = formRun.dataSource();
                if (formHasMethod(formRun, identifierStr(xxxFromDate)))
                {
                    fromDate = formRun.xxxFromDate();
                }
                if (formHasMethod(formRun, identifierStr(xxxToDate)))
                {
                    toDate = formRun.xxxToDate();
                }
            }
        }
        
        /// <summary>
        ///
        /// </summary>
        protected void process()
        {
           //I have run the class a first time where fromDate=01/01/2024 and toDate=31/01/2024
           //I have rerun the class where the fromDate is being initialized in the initFromArgs to new value 02/01/2024 and toDate=05/02/2024
           //However, they are still fromDate=01/01/2024 and toDate=31/01/2024 when reaching in the process method
        }
       
        /// <summary>
        ///
        /// </summary>
        public void run()
        {
            this.process();
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name = "_args"></param>
        public static void main(Args _args)
        {
            TestClass manager = new TestClass();
            manager.initFromArgs(_args);
            if (manager.prompt())
            {
                manager.runOperation();
            }
        }
     
        [Hookable(false)]
        public boolean isRetryable()
        {
            return true;
        }
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        protected boolean canRunInNewSession()
        {
            return false;
        }
     
    public boolean canGoBatch()
        {
            return true;
        }
    }
  • Martin Dráb Profile Picture
    Martin Dráb 227,979 Super User 2024 Season 2 on at
    RunBaseBatch pack unpack variables coming from a form
    Please show us your code and explain where it fails.
     
    By the way, you won't have to implement serialization (pack/unpack) by yourself if you use SysOperation framework instead of RunBaseBatch.

Helpful resources

Quick Links

Dynamics 365 Community Update

Welcome to the inaugural Community Platform Update. As part of our commitment to…

Dynamics 365 Community Newsletter - August 2024

Catch up on the latest D365 Community news

Community Spotlight of the Month

Kudos to Mohana Yadav!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,142 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 227,979 Super User 2024 Season 2

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans