Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested answer

Extension of button control clicked method

(0) ShareShare
ReportReport
Posted on by 8

Hi,

i am trying to extend the clicked method in the custfreeinvoce  form button ButtonCustPostInvoiceJob .

But i keep getting the following error  object reference not set .please find below my code

thanks

[ExtensionOf(formControlStr(CustFreeInvoice,ButtonCustPostInvoiceJob))]

final class custfreetext_extension
{
public void clicked()
{



CustInvoiceTable _CustInvoiceTable;
propinvoice _propinvoice;
FormButtonControl bc = any2Object(this) as FormButtonControl;
FormDataSource ds = bc.formRun().dataSource(tableStr(CustInvoiceTable));
next clicked();



_CustInvoiceTable = ds.cursor();

if (_CustInvoiceTable.Posted==NoYes::Yes)
{
while select forupdate _propinvoice where _propinvoice.Propinvoiceid==_CustInvoiceTable.Propinvoiceid
{

_propinvoice.InvoiceId=_CustInvoiceTable.InvoiceId;
_propinvoice.update();
info("invoice updated");
}

}

}

}

  • Suggested answer
    Martin Dráb Profile Picture
    233,488 Most Valuable Professional on at
    RE: Extension of button control clicked method

    Please make sure you paste code with line indentation, otherwise your code is difficult to read. Use Insert > Insert Code (in the rich formatting view) to do that. This is the result:

    [ExtensionOf(formControlStr(CustFreeInvoice,ButtonCustPostInvoiceJob))]
    final class custfreetext_extension
    {
    	public void clicked()
    	{
    		CustInvoiceTable _CustInvoiceTable;
    		propinvoice _propinvoice;
    		FormButtonControl bc = any2Object(this) as FormButtonControl;
    		FormDataSource ds = bc.formRun().dataSource(tableStr(CustInvoiceTable));
    		next clicked();
    
    		_CustInvoiceTable = ds.cursor();
    
    		if (_CustInvoiceTable.Posted==NoYes::Yes)
    		{
    			while select forupdate _propinvoice where _propinvoice.Propinvoiceid==_CustInvoiceTable.Propinvoiceid
    			{
    				_propinvoice.InvoiceId=_CustInvoiceTable.InvoiceId;
    				_propinvoice.update();
    				info("invoice updated");
    			}
    
    		}
    	}
    }

    When I'm looking at it, let me also fix a few problems in your code. It'll help you write better code.

    • Use underscore (_) prefix only for method parameters, not local variables.
    • Use formDataSourceStr(), not tableStr(), to refer to a form data source name.
    • I have no idea why you're using any2Object(this) - it looks very strange. But let me throw it away and get the data source by simply calling this.dataSourceObject().

    [ExtensionOf(formControlStr(CustFreeInvoice, ButtonCustPostInvoiceJob))]
    final class CustFreeText_Extension
    {
        public void clicked()
        {
            CustInvoiceTable custInvoiceTable = this.dataSourceObject().cursor();
      
            next clicked();
    
            if (custInvoiceTable.Posted == NoYes::Yes)
            {
                PropInvoice propInvoice;
    
                while select forupdate propInvoice
                    where propInvoice.PropInvoiceId == custInvoiceTable.PropInvoiceId
                {
                    propInvoice.InvoiceId = custInvoiceTable.InvoiceId;
                    propInvoice.update();
                    info("invoice updated");
                }
            }
        }
    }

    You should also use update_recordset instead of the update in a loop.

    But the fundamental problem is in the fact that your code is at a wrong place - it belongs to the posting logic and to the same transaction that does the posting. You're also forgetting that posting can be executed in batch and multiple invoices can be posted at once.

    The right place seems to be postProcessCustPostInvoiceUpdate() method in CustPostInvoiceJob class.

    Finally, I've assigned the category Development / Customization / SDK to this thread, because it clearly is about development.

  • nmaenpaa Profile Picture
    101,158 Moderator on at
    RE: Extension of button control clicked method

    Moved to D365 Finance forum.

    You need to debug your code to find out which line throws the error. Once you find it out, please share an update with us.

    Thanks!

    By the way, please remember to use your prefix when naming extension classes. CustFreeText_Extension is not a good class name.

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

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

#1
Martin Dráb Profile Picture

Martin Dráb 318 Most Valuable Professional

#2
Abhilash Warrier Profile Picture

Abhilash Warrier 273

#3
Jonas "Jones" Melgaard Profile Picture

Jonas "Jones" Melgaard 181 Super User 2025 Season 1

Overall leaderboard

Product updates

Dynamics 365 release plans