web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

RunBaseBatch pack unpack variables coming from a form

(0) ShareShare
ReportReport
Posted on by 8
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.
I have the same question (0)
  • Martin Dráb Profile Picture
    237,795 Most Valuable Professional on at
    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.
  • Jason0810M Profile Picture
    8 on at
    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
    237,795 Most Valuable Professional on at
    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
    8 on at
    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
    237,795 Most Valuable Professional on at
    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
    8 on at
    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.
  • Verified answer
    Martin Dráb Profile Picture
    237,795 Most Valuable Professional on at
    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.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 660 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 549 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 307 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans