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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

UPDATE TABLE Differences between four parts of code

(0) ShareShare
ReportReport
Posted on by

Hi,

I started a new job as a AX Programmer and i have a lot of questions which aren't exactly answered in web...

My first task is to create a class/job which update a lot of tables from hardcode data...

What are the differences between this four part of code... Because in my develop instance all works perfectly

### 1 ###

static void Job(Args _args)
{

ARP_HD_Books book;
book.AuthorFirstName = "FIRSTNAME";
book.insert();

}

### 2 ###

static void Job(Args _args)
{

ARP_HD_Books book;
select book;
book.AuthorFirstName = "FIRSTNAME";
book.insert();

}

### 3 ###

static void Job(Args _args)
{

ARP_HD_Books book;
select forUpdate book;
book.AuthorFirstName = "FIRSTNAME";
book.insert();

}

### 4 ###

static void Job(Args _args)
{

ARP_HD_Books book;
ttsBegin;
select forUpdate book;
book.AuthorFirstName = "FIRSTNAME";
book.insert();
ttsCommit;

}

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Vilmos Kintera Profile Picture
    46,149 on at

    They all look like bad code to me. I would rather do a buf2buf() call to copy values from the selected source table to your target table, or create a custom initFromTable() method where you initialize the values from the passed in record buffer, then insert the row after calling the initialization.

    In AX you would do it like:

    record.clear();

    record.initValue();

    record.field = value;

    if (record.validateWrite())

    {

       record.insert();

    }

    TTS block is only required if you are updating/deleting values, or if you purposefully cancel the insertion if you are on a lower TTSLevel if something fails during the execution within the TTS block. You should consider enabling Best Practices check in your User options, which should reveal some of the problems.

    Also make sure you use the tags on your post correctly, Current version means AX7/Dynamics365 operations.

  • Community Member Profile Picture
    on at

    Thanks!! That is exactly what i was looking for...

    I have one more question...

    Why when i want to add several records in loop I get an error "Cannot create a record in Books list (ARP_HD_books). The record already exists" ??

    static void Job(Args _args)

    {

       ARP_HD_Books book;

       int x

       ;

       for (x = 1; x < 10; x++)

       {

           book.AuthorFirstName = "FIRSTNAME";

           book.insert();

       }

    }

  • Suggested answer
    Vilmos Kintera Profile Picture
    46,149 on at

    You are violating the unique index on your table, it will not allow you to put the same value twice. It is typically an identifier field of some sort. Check the Primary index property on the table, and find that in the Indexes list, that will be the cause. There might be other unique indexes defined as well.

  • Community Member Profile Picture
    on at

    It is caused by RecId...

    I overrided clicked() button and put there the code for creating a new record...

    If i slowly click records adding without problems...

    But if i click fast (for example duble click) error apears...

    And after error when i click once again... there is new error and warning

    Warning: Record with RecId ..... in table 'Books list' has mismatching Sequence field values. Original value was 0, new value is .....

    Error:  Cannot edit a record in Books list (ARP_HD_Books). The record number does not comply with the original number. If the table uses entire table cache, thos may be caused by the cache being flushed. Restart your job if this is the case.

  • Suggested answer
    Vilmos Kintera Profile Picture
    46,149 on at

    You should never place database access code on client-side (form, clicked). Those must run on server-side, which you could do in a class that you instantiate in the init of your form, then you could use the public methods on it to be called in your clicked().

  • Community Member Profile Picture
    on at

    Ok it's "BestPractice" but Job with loop insert() which i wrote before doesn't work too... The clicked() trick was only helf for bug searching...

  • Vilmos Kintera Profile Picture
    46,149 on at

    Please make sure you mark verified answers on the right, if your original question in the topic is resolved, so other users could learn from it as well. Thank you

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

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 > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans