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, ...
Answered

CoC on Form init

(0) ShareShare
ReportReport
Posted on by 596

Hi guys,

If in original form, there is a method Init() which is using element.args().dataset(), can we use the same in CoC of this form ?

Take example in the original form, the method init() has this code : 

 public void init()
{
        LedgerJournalTable  ledgerJournalTableCheck;
        LedgerJournalTrans  ledgerJournalTransCheck;
    
        switch (element.args().dataset())
        {
            case tableNum(SalesLine):
                salesLine SalesLineItemReq = salesLine::findRecId(element.args().record().RecId);
                if (SalesLineItemReq.SalesType == SalesType::ItemReq)
                {
                    curTransactionType = SubBillDeferralTransactionType::ItemReq;
                }
                else
                {
                    curTransactionType = SubBillDeferralTransactionType::SalesOrder;
                }
    
                curSourceRecType = SubBillDeferralSourceRecType::SalesLine;
                break;
        }
        
}

I want to create CoC for this method init(), and still want to check whether the caller record is SalesLine

So I'm creating my CoC like this : 

[ExtensionOf(formStr(SubBillDeferralTransactionDeferral))]
final class My_SubBillDeferralTransactionDeferralForm_Extension
{
    
    public void init()
    {
        next init();
        if (element.args().dataset() == tableNum(SalesLine))
        {
            
        }
    }


}

But I have errors saying "element not declared", What is the correct way of doing it ?

Thanks,

I have the same question (0)
  • Suggested answer
    GirishS Profile Picture
    27,827 Moderator on at

    Hi Lars Volt,

    Don't use element for form extension. Instead use "this" keyword.

    Thanks,

    Girish S.

  • Mohit Rampal Profile Picture
    12,565 Moderator on at

    element is used for formRun within a form. In form extension class keyword 'this' should be used.

  • Voltes Profile Picture
    596 on at

    Thanks,

    In the same method, if I want to refer or get FormRun object, can I use this :

    FormRun formRun = this as FormRun;

    because next I want to have the datasource. From some resource I learn is something like this :

    FormDataSource SubBillDeferralTransactionLineDeferral_DS=formRun.datasource(FormDatasourceStr(SubBillDeferralTransactionDeferral,SubBillDeferralTransactionLineDeferral));

    but when debug, the 1st statement already give me null.

    Thanks,

  • GirishS Profile Picture
    27,827 Moderator on at

    You can directly use this keyword.

    FormDataSource SubBillDeferralTransactionLineDeferral_DS = this.dataSource(formDatasourcestr(SubBillDeferralTransactionDeferral,SubBillDeferralTransactionLineDeferral));

    Please try that and see if it works.

    Thanks,

    Girish S.

  • Mohit Rampal Profile Picture
    12,565 Moderator on at

    Try adding below code

    FormDataSource SubBillDeferralTransactionLineDeferral_DS=this.datasource(FormDatasourceStr(SubBillDeferralTransactionDeferral,SubBillDeferralTransactionLineDeferral));

  • GirishS Profile Picture
    27,827 Moderator on at

    You can get DataSource buffer in the init method. But you won't get current record in the init method. As it will be too early to get the current DataSource record in the init method.

    Thanks,

    Girish S.

  • Voltes Profile Picture
    596 on at

    Hi both,

    Thanks for such prompt response. I genuinely thought it is correct, but turns out the variable DS is null when I debug

    Here is the code now: 

    [ExtensionOf(formStr(SubBillDeferralTransactionDeferral))]
    final class MY_SubBillDeferralTransactionDeferralForm_Extension
    {
        public void init()
        {
            FormDataSource SubBillDeferralTransactionLineDeferral_DS = this.dataSource(FormDatasourceStr(SubBillDeferralTransactionDeferral,SubBillDeferralTransactionLineDeferral));
            SubBillDeferralTransactionLineDeferral SubBillDeferralTransactionLineDeferral = SubBillDeferralTransactionLineDeferral_DS.Cursor();
    
            FormControl.deferred = this.design().ControlName(FormControlStr(SubBillDeferralTransactionDeferral, SubBillDeferralDeferred));
    
            next init();
            SubBillDeferralTransactionLineDeferral.SubBillDeferralDeferred = NoYes::Yes;
    
        }
    
    }

    This form is called from Sales Order, by click button Deferrals on Line Action Pane. Will it make the code different ?

    My intention is, inside this form (which gonna pop up after click that button), there is check box Deferred which I need to turn it ON (checked).

    Thanks,

  • Voltes Profile Picture
    596 on at

    Yes, speaking of which, I even not yet put the condition if it is relate to SalesLine, like the original Init method :)

    Noted I need to use keyword "this" instead "element", will try after the current issue solved.

  • Verified answer
    GirishS Profile Picture
    27,827 Moderator on at

    If you want to set default value to the field, you can try initValue method instead of init method.

    In that case you need to write extension of form DataSource init method.

    Thanks,

    Girish S.

  • Mohit Rampal Profile Picture
    12,565 Moderator on at

    Try adding same code on init method of form datasource which has Deferred field instead of init method of form.

    dynamics365musings.com/.../

    There's still chances that code will not work because standard code is enabling/disabling this fields based on some conditions. In that case you need to find where its disabling it like check enableFields method. You might need to extend that method as well.

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 551 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 278 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans