Skip to main content

Notifications

Announcements

No record found.

Finance forum
Answered

CoC on Form init

Posted on by 554

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,

  • Voltes Profile Picture
    Voltes 554 on at
    RE: CoC on Form init

    Thanks, Mohit.

    I will try this as well, for my learning purpose.

    Once again thanks all for your guidance.

  • Voltes Profile Picture
    Voltes 554 on at
    RE: CoC on Form init

    Ahh yes, sorry to missed that.

    Finally it works.

    This is my code

    [ExtensionOf(formDataSourceStr(SubBillDeferralTransactionDeferral, SubBillDeferralTransactionLineDeferral))]
    final class MY_SubBillDeferralTransactionDeferralFormDS_Extension
    {
        public void initValue()
        {
            FormDataSource formDS = this;
            SubBillDeferralTransactionLineDeferral lineDeferral = formDS.cursor();
            next initValue();
    
            lineDeferral.SubBillDeferralDeferred = NoYes::Yes;
    
        }
            
    
    }

    Thanks

  • Mohit Rampal Profile Picture
    Mohit Rampal 12,540 Super User on at
    RE: CoC on Form init

    Can you try CoC on enableFields method in form extension class. This method is setting value to false for Deferred checkbox. If it works then you need to control, when to disable the checkbox.

    protected void enableFields()
    {
        next enableFields();
        SubBillDeferralDeferred.value(true);
    }

  • GirishS Profile Picture
    GirishS 27,803 Super User on at
    RE: CoC on Form init

    As I already said best practice for setting the default value will be initValue method on the form datasource level and table level.

    Moreover adding this code at table level is best practice as it will reduce the load on the form.

    Thanks,

    Girish S.

  • Voltes Profile Picture
    Voltes 554 on at
    RE: CoC on Form init

    I think I cannot create CoC on its FormDataSource since it is says Hookable=False ->

    Unless I misunderstood what is it for.

    Or perhaps that is why I have error when CoC Form level as well ?

    Thanks

  • Mohit Rampal Profile Picture
    Mohit Rampal 12,540 Super User on at
    RE: CoC on Form init

    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.

  • Verified answer
    GirishS Profile Picture
    GirishS 27,803 Super User on at
    RE: CoC on Form init

    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.

  • Voltes Profile Picture
    Voltes 554 on at
    RE: CoC on Form init

    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.

  • Voltes Profile Picture
    Voltes 554 on at
    RE: CoC on Form init

    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,

  • GirishS Profile Picture
    GirishS 27,803 Super User on at
    RE: CoC on Form init

    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.

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,696 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,490 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans