web
You’re offline. This is a read only version of the page.
close
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");
}

}

}

}

I have the same question (0)
  • nmaenpaa Profile Picture
    101,160 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
    237,801 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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
Martin Dráb Profile Picture

Martin Dráb 664 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 522 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 303 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans