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, ...
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");
}

}

}

}

I have the same question (0)
  • nmaenpaa Profile Picture
    101,166 Moderator on at

    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.

  • Suggested answer
    Martin Dráb Profile Picture
    239,040 Most Valuable Professional on at

    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.

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!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

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

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 616

#2
André Arnaud de Calavon Profile Picture

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

#3
Syed Haris Shah Profile Picture

Syed Haris Shah 331 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans