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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

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

Updating a Field on a form via the click of a button

(1) ShareShare
ReportReport
Posted on by 59
Hello everyone, In need of some help here.
I have a custom form and I have created a button to update the status of a field on the form. I overwrote teh clicked method on the button and wrote this code into it. my issue is this seems to update all the records on the form, what I need is for it to update only selected records

code: 
    [Control(/Button/)]
    class ApproveTestButton
    {
        public void clicked()
        {
            super();
            // Get the current record from the data source
            VendorPaymentHeaderTable movVendorPaymentHeaderTable = VendorPaymentHeaderTable_ds.cursor();
            if (VendorPaymentHeaderTable)
            {
                if (VendorPaymentHeaderTable.VendPaymentApprovalStatus == VendPaymentApprovalStatus::Approved)
                {
                    ttsbegin;
                    VendorPaymentHeaderTable.VendPaymentApprovalStatus = VendPaymentApprovalStatus::Approved;
                    VendorPaymentHeaderTable.update();
                    ttscommit;
                    info(/Payment approval status has been changed to 'Approved'./);
                }
                else
                {
                    info(/Payment is already approved./);
                }
            }
            else
            {
                error(/No record selected./);
            }
        }
    }
 
Kindly assist
I have the same question (0)
  • Martin Dráb Profile Picture
    238,778 Most Valuable Professional on at
    You populate movVendorPaymentHeaderTable variable, but they you don't use it for anything, therefore it has no effect. Your effective code is this:
    public void clicked()
    {
        super();
        
        if (vendorPaymentHeaderTable)
        {
            if (vendorPaymentHeaderTable.VendPaymentApprovalStatus == VendPaymentApprovalStatus::Approved)
            {
                ttsbegin;
                vendorPaymentHeaderTable.VendPaymentApprovalStatus = VendPaymentApprovalStatus::Approved;
                vendorPaymentHeaderTable.update();
                ttscommit;
                info("Payment approval status has been changed to 'Approved'.");
            }
            else
            {
                info("Payment is already approved.");
            }
        }
        else
        {
            error("No record selected.");
        }
    }
    
    It updates a single record, but in fact, it doesn't change anything, because it sets the status to Approved only if it already is Approved. You clearly wanted the opposite condition.
     
    It seems that you see the same value for all records, but it's likely caused by a problem in the form, not in data. For example, maybe you have a grid based on a data source and you have VendorPaymentHeaderTable linked by Delayed link (not joined) to the grid's DS. That's wrong - the value for all grid rows will show VendPaymentApprovalStatus of a single VendorPaymentHeaderTable record only.
  • Peta Peta Profile Picture
    59 on at
    Hello Martin, Thank you fo your response. I have been able to acheieve it with this
     
     [Control("Button")]
        class ApproveTestButton
        {
            public void clicked()
            {
                super();
                // Get the current record from the data source
                VendorPaymentHeaderTable VendorPaymentHeaderTable = VendorPaymentHeaderTable_ds.cursor();
                if (movVendorPaymentHeaderTable)
                {
                    if (VendorPaymentHeaderTable.VendPaymentApprovalStatus == VendPaymentApprovalStatus::Draft)
                    {
                        ttsbegin;
                        VendorPaymentHeaderTable.VendPaymentApprovalStatus = VendPaymentApprovalStatus::Approved;
                        VendorPaymentHeaderTable.update();
                        ttscommit;
                        info("Payment approval status has been changed to 'Approved'.");
                    }
                    else
                    {
                        info("Payment is already approved.");
                    }
                }
                else
                {
                    error("No record selected.");
                }
            }
        }
  • Martin Dráb Profile Picture
    238,778 Most Valuable Professional on at
    That's interesting that it works for you, because it's wrong.
     
    You're using movVendorPaymentHeaderTable variable in the condition, which will fail even to compile, unless you have such a variable defined in the form header. Even if it compiles, it makes no sense - you clearly wanted to use the variable defined on the line above:
    VendorPaymentHeaderTable VendorPaymentHeaderTable = VendorPaymentHeaderTable_ds.cursor();
    But this is wrong too, for several reasons. It shouldn't compile, because there already is vendorPaymentHeaderTable variable automatically created for the data source. And your code doesn't do anything useful - the automatically created variable already contains the same value as vendorPaymentHeaderTable_ds.cursor(). So you can throw the whole thing away:
    public void clicked()
    {
        super();
        
        if (vendorPaymentHeaderTable.RecId)
        {
            if (vendorPaymentHeaderTable.VendPaymentApprovalStatus == VendPaymentApprovalStatus::Draft)
            {
                ttsbegin;
                vendorPaymentHeaderTable.VendPaymentApprovalStatus = VendPaymentApprovalStatus::Approved;
                vendorPaymentHeaderTable.update();
                ttscommit;
                
                info("Payment approval status has been changed to 'Approved'.");
            }
            else
            {
                info("Payment is already approved.");
            }
        }
        else
        {
            error("No record selected.");
        }
    }
  • Peta Peta Profile Picture
    59 on at
    Thanks, Martin, I have been able to get it to work using your recommendation, the only issue is it updates just one record regardless of my multiple sections.
    public void clicked()
    {
        super();
        
        if (vendorPaymentHeaderTable)
        {
            if (vendorPaymentHeaderTable.VendPaymentApprovalStatus == VendPaymentApprovalStatus::Draft)
            {
                ttsbegin;
                vendorPaymentHeaderTable.VendPaymentApprovalStatus = VendPaymentApprovalStatus::Approved;
                vendorPaymentHeaderTable.update();
                ttscommit;
                info("Payment approval status has been changed to 'Approved'.");
            }
            else
            {
                info("Payment is already approved.");
            }
        }
        else
        {
            error("No record selected.");
        }
    }

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

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

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 514 Super User 2026 Season 1

#2
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 390

#3
Adis Profile Picture

Adis 266 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans