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 :
Dynamics 365 Community / Forums / Finance forum / Active method not work...
Finance forum
Unanswered

Active method not working as expected

(1) ShareShare
ReportReport
Posted on by 252
Hi,

On SalesLine grid,

I added a field called "Field1" in SalesLine table, to lock the salesline from being edited.

I need to do the following 2 points:

1. If I move between salesLine records, then if this "Field1" record is true, then the record needs to be non editable. And if I stand on a record 'field1' as false, then the record will be editable. I did the code below.
However, it seems if i click edit in the form, all records look editable at first, and when i try to edit one of the fields where 'field1' = no, then the record is converted to non editable. why? it's like it appears for 1 second as editable, then it becomes non editable.
Try it by standing on sales price field for example then move to another record by clicking on sales price directly, you will see it's editable for 1 second then locked, why it's not locked directly? or is this standard behaviour?
[ExtensionOf(formDataSourceStr(SalesTable, SalesLine))]
final class SalesTableFormDataSourceSalesLine_Extension
{
    public int active()
    {
        int ret = next active();
        if(ret)
        {
            if(!Global::isSystemAdministrator())
            {
                SalesLine salesLine = this.cursor();

                if (salesLine)
                {
                    if (salesLine.Field1 == NoYes::Yes)
                    {
                        this.allowEdit(false);
                    }
                    else
                    {
                        this.allowEdit(true);
                    }
                }
            }
        }
        return ret;
    }
}

 
2. Only sysAdmin can tick the "Field1" as true. So the record will stay non editable for sysAdmin, except for lock field, the sysAdmin can untick it to be able to edit the whole record. How to do that?
because in point1, i made the whole line as non editable, so how i'm going to say the even though the line is not editable, but u could still edit one field only to make the whole record editable again?
 
  • Martin Dráb Profile Picture
    241,353 Most Valuable Professional on at
    1. Yes, the data source is editable by default and disabled only when your code executes, after selecting a record and completing all standard code. But you can disable the data source by default (when initializing the form).
    2. If you disable a data source, none of its fields can be changed. I suggest you use a button to change the field. If you insist on changing the field directly, you must leave the record editable and disable all fields except of the one.
     
    Unfortunately, your code contains a critical bug. It ignores the fact that that there is standard logic deciding whether a record can be edited. For example, the system decided that a line can't be edited for some reason, but your code will happily change AllowEdit to true, if Field1 happens to be No. Instead of ignoring standard code and then simply running your specific logic in active(), you must look at what the standard code does and identify the right place for your logic.
  • Martin Dráb Profile Picture
    241,353 Most Valuable Professional on at
    1) "move to another locked record by clicking on sales price directly, you will see it's editable for 1 second then locked" suggests that it's enabled by active() (the standard logic discussed before), not by records being enabled by default. Therefore it's yet another problem caused by wrong code placement and one more reason for fixing that.
    3) I suggest the same thing as before. You need to analyze code called from active() that decides whether the record should be editable. Unfortunately, I don't remember all the code there and I can't look into Visual Studio at the moment.
  • Martin Dráb Profile Picture
    241,353 Most Valuable Professional on at
    1) As I explained, I can't do the analysis for you, because I don't have an F&O on hand at the moment. The key thing is that you must stop ignoring standard code in active() method. Instead of putting your logic directly to active(), you should do it in the appropriate piece of code called by Microsoft from active().
     
    2) The property controlling whether a record can be edit is AllowEdit. I'm not sure what you mean by making a field locked, but you're making a mistake if you don't change AllowEdit.
     
    No, I'm not saying anything about a "need to repeat the active logic again".

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Finance

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans