Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Unanswered

Initializing a table before inserting

(0) ShareShare
ReportReport
Posted on by 1,309
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
 
  • GirishS Profile Picture
    GirishS 27,821 Super User 2024 Season 1 on at
    Initializing a table before inserting
    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.
  • .. Profile Picture
    .. 1,309 on at
    Initializing a table before inserting
    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)
     
  • Martin Dráb Profile Picture
    Martin Dráb 230,842 Most Valuable Professional on at
    Initializing a table before inserting
    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,309 on at
    Initializing a table before inserting
    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?
     
     
     
     
  • GirishS Profile Picture
    GirishS 27,821 Super User 2024 Season 1 on at
    Initializing a table before inserting
    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.

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,969 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,842 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans