web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Unanswered

Initializing a table before inserting

(0) ShareShare
ReportReport
Posted on by 1,979
Hi,

What is better

Table1.Field1 = _basket.Field1
Table1.Field2 = _basket.Field2
Table1.Field3 = _basket.Field3

Table1.Insert()

OR:
 
 Table1.setField(fieldNum(Table1Field1), _basket.Field1);
 Table1.setField(fieldNum(Table1Field2), _basket.Field1);
 Table1.setField(fieldNum(Table1Field3), _basket.Field1);
 Table1.insert();
 
    public void setField(FieldId _fieldId, anytype _value)
    {
        this.(_fieldId) = _value;
 
        if (!this.validateField(_fieldId))
        {
            throw Exception::Error;
        }
 
        this.modifiedField(_fieldId);
    }


I can see that option1 is done alot... but is option2 better to validate each field and call modifed method??
let's assume Table1 is salesTable for example
or when to use each one
 
I have the same question (0)
  • GirishS Profile Picture
    27,829 Moderator on at
    Hi,
     
    We can't say which one is better because if you want the code in modified method to be run before inserting then you must call the modified method manually as you did in the option 2. Other option will be implementing the same logic in modified method in your method where you are inserting records - This will reduce an unnecessary call to the method.
     
    Thanks,
    Girish S.
  • .. Profile Picture
    1,979 on at
    Hi Girish,
     
    I have 4 questions please:
     
    Do you mean that option 2 will call the modified method twice. While the first option will call the modified after the insert by itself?
     
    And in option1 what would happen if i call the modified after insert? If the modified fail, will the i sert fail?
     
    Also can u give me an example on when would we need to call the modified method before the insert and when would we want to call it after? I can't differentiate the difference
     
    And how about the validate field method, u didn't mention it. Would it be called by itself in option1? Or when would i need it?
     
     
     
     
  • Martin Dráb Profile Picture
    238,244 Most Valuable Professional on at
    None of them call modified(). Option 2 calls modifiedField() and validateField(), while option 1 calls just insert() and nothing else. There is no magic - simply look at the code.
     
    If you call modifiedField() after insert(), the logic in modifiedField() executes, but if it changes any fields, the change won't be saved. So it's not really useful.
     
    A good idea in both cases would be calling validateWrite() and not inserting the record when validateWrite() says that the data is invalid. Throwing an exception in such a case is usually the right approach.
  • .. Profile Picture
    1,979 on at
    Hi Martin,
     
    I'm already calling validateWrite before the insert but i forgot to add it.
     
    So if I'm calling valdiateWrite.
    Which option is better? Is it better to call validateField and modified before the insert(option2 )? Or just directly insert(option 1)
     
  • GirishS Profile Picture
    27,829 Moderator on at
    Option1 won't call the standard modified or validate method. In the option2 you specifically called the method so it will get called.
    Again, I am saying calling the modified and validate method is not needed until you specifically need it. 
    My suggestion will be implementing the modified and validation method logic while inserting itself.
    Or As Marin mentioned for validation add your logic in the validateWrite method and call that method before inserting.
     
    Thanks,
    Girish S.

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

News and Announcements

Season of Giving Solutions is Here!

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
Abhilash Warrier Profile Picture

Abhilash Warrier 836 Super User 2025 Season 2

#2
André Arnaud de Calavon Profile Picture

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

#3
Martin Dráb Profile Picture

Martin Dráb 346 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans