Skip to main content

Notifications

Announcements

No record found.

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

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

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
  • Peta Peta Profile Picture
    Peta Peta 59 on at
    Updating a Field on a form via the click of a button
    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.");
        }
    }

  • Martin Dráb Profile Picture
    Martin Dráb 230,188 Most Valuable Professional on at
    Updating a Field on a form via the click of a button
    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
    Peta Peta 59 on at
    Updating a Field on a form via the click of a button
    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
    Martin Dráb 230,188 Most Valuable Professional on at
    Updating a Field on a form via the click of a button
    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.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans