Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Unanswered

Problem with updating table field by selecting a value from a dialog form.

(0) ShareShare
ReportReport
Posted on by 737

Hello,

There is a requirement that specifies that when the user changes a value in the X fields, he should specify the reason for the change.
The X field is a standard field in SalesTable. I wrote the following code that, after changing the value of the field, displays a dialog form for the user to select the reason for changing the X field. The selected reason is saved in an additional field in the table.


1 I extended the update() method in SalesTable

public void update()
    {
        SalesTable salesTable_orig = this.orig();

        next update();
 
        if(this.Field_X != SalesTable_orig.Field_X)
        {
            this.runDialogToChangeReason(this.SalesId);
        }
    }

2. The runDialogToChangeReason method, with _salesId as an argument

 public void runDialogToChangeReason(SalesId _salesId)
    {
        Dialog dlg;
        DialogField dfg;

        dlg = new Dialog();
        dfg = dlg.addField(EnumStr(ChangeReason), "Reason");

        if(dlg.run())
        {
            salesTable::setChangeReason(_salesid, dfg.value());
        }
    }

3. The setChangeReason method, with _salesId and _reason as an arguments

public static void setChangeReason(SalesId _salesId, ChangeReason _reason)
    {
        SalesTable salesTable = SalesTable::find(_salesId);
        
        ttsbegin;
        salesTable.selectForUpdate(true);
        salesTable.ReasonField = _reason;
        salesTable.update();
        ttscommit;
    }

When selecting a value in the dialog and attempting to save, I always get the error "An unbalanced x TTSBEGIN/TTSCOMMIT pair..."

I tried debugging, but when starting the dialog, I immediately get the aforementioned error....

I also tried to insert ttsabort but also without success....

if (appl.ttsLevel() != 0)
        {
            ttsabort;
        }

Could I have your help please?

  • Martin Dráb Profile Picture
    Martin Dráb 230,962 Most Valuable Professional on at
    RE: Problem with updating table field by selecting a value from a dialog form.

    Both of your suggestions still mean calling your code from update() and therefore you'd still have the identical problem.

    Are you sure you need a dialog at all? Can't users simply set the value of ReasonField? Then you'll just validate (in validateWrite()) that it has a value.

  • Shooowtek Profile Picture
    Shooowtek 737 on at
    RE: Problem with updating table field by selecting a value from a dialog form.

    Of course I meant DataEventType::Updating. Sorry about that.

  • Shooowtek Profile Picture
    Shooowtek 737 on at
    RE: Problem with updating table field by selecting a value from a dialog form.

    Thank you Martin for your reply. It explains a lot.

    So in my case, where is the best place to do it? Is it before calling next metod or using DataEventType::Inserting?

    I need to display a dialog after the user changes the value in the X field.

    Thank you in advance for your suggestions.

  • Martin Dráb Profile Picture
    Martin Dráb 230,962 Most Valuable Professional on at
    RE: Problem with updating table field by selecting a value from a dialog form.

    There is a design flaw in your solution.

    update() will be called in a transaction and if you opened a dialog there, the transaction could stay open for a long time or even forever, and all locks obtained in the transaction could be blocking the system for a long time (or forever). As a last resort to prevent this potentially catastrophic situation, the transaction is aborted when the dialog is opening.

    You need to this work before starting the transaction.

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Congratulations to the January Top 10 leaders!

Check out the January community rock stars...

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,160 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,962 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans