Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 Community / Forums / Finance forum / Active method not work...
Finance forum
Unanswered

Active method not working as expected

Posted on by 236
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?
 
  • CU04051814-0 Profile Picture
    CU04051814-0 236 on at
    Active method not working as expected
    Hi Martin,

    can you please check my previous reply?
  • CU04051814-0 Profile Picture
    CU04051814-0 236 on at
    Active method not working as expected
    Hi Martin,

    1. The active method on salesLine datasource has a lot of code. I'm not really sure where should i put it?
    And btw, i do think it's a standard behaviour. I just tried it on a new custom checkBox field.
    I was standing on a record where this Field1 = false (not locked) and the record above it showed that this custom checkBox is editable, even though the record has Field1 =true (locked). When i moved directly to this record above by clicking on the custom checkbox to change it's value to true, it took the value for less than 1 second then made the record locked and returned the checkbox value to false

    2. I mean that my logic is ignoring standard code as u said, as it makes the record as allow edit true only based on the new field i created.

    So i think my logic should do the following, right? but how?

    if standard code has this record as allow edit true, then:
    -- if my new field has is locked true, then i should lock the record
    -- if my new field has is locked false, then i shouldn't lock the record
     
    if standard code has this record as allow edit false, then:
    -- the record should be kept as locked, regardless of my locked field

    So I added this and condition (&& this.allowEdit())  -- would that fix the issue?
    and i will need to also check this allow edit property when clicking on the 'unlock' action menu item that i will add to unlock the record. right?

    [ExtensionOf(formDataSourceStr(SalesTable, SalesLine))]
    final class SalesTableFormDataSourceSalesLine_Extension
    {
        public int active()
        {
            int ret = next active();
            if(ret)
            {
                if(!Global::isSystemAdministrator() && this.allowEdit())
                {
                    SalesLine salesLine = this.cursor();
    
                    if (salesLine)
                    {
                        if (salesLine.Field1 == NoYes::Yes)
                        {
                            this.allowEdit(false);
                        }
                        else
                        {
                            this.allowEdit(true);
                        }
                    }
                }
            }
            return ret;
        }
    }




     
  • Martin Dráb Profile Picture
    Martin Dráb 225,588 Super User on at
    Active method not working as expected
    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".
  • CU04051814-0 Profile Picture
    CU04051814-0 236 on at
    Active method not working as expected
    Hi Martin,

    1. Where would I put the code if it's not in active method then to achieve my result?

    2. Is there a way to say if the standard logic has allow edit false, then if I change my field to locked = false, then the line should still be not editable?
    I mean my logic should make the record unlocked, only if it was originally not locked.
    I hope you get back to me when you have the time
    do you mean i need to repeat the active logic again or what exactly?
     
  • Martin Dráb Profile Picture
    Martin Dráb 225,588 Super User on at
    Active method not working as expected
    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.
  • CU04051814-0 Profile Picture
    CU04051814-0 236 on at
    Active method not working as expected
    Hi Martin,

    Can you please see my reply regarding the critical bug and help me with it?
  • CU04051814-0 Profile Picture
    CU04051814-0 236 on at
    Active method not working as expected
    Hi Martin,

    1. even if i disable at first in init method of the form. When i got to a not locked record, that then go back to a locked record, i will still see the field editable for 1 second... i think this is standard behavior and there is nothing i can do about it right?
    As i said, you can try it by standing on sales price field for a non locked record, then move to another locked record by clicking on sales price directly, you will see it's editable for 1 second then locked.
     
    2. I was also thinking of adding the a button for locking
     
     
    3. Regarding the critical bug, i think u are right, but what shall i do about it? If i look at the code of salesTable form, i can see alot of lines that say SalesLine_ds.allowedit(false)  and maybe there is sth on table level as well.
    What do you suggest? 
  • Martin Dráb Profile Picture
    Martin Dráb 225,588 Super User on at
    Active method not working as expected
    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.

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,989 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,588 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans