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)

insert when doe snot exist

(0) ShareShare
ReportReport
Posted on by

I am tring to insert a record in a table only if it doe snot exist already. Can some help me withis

Below is my code

Bom BOM

BOM.LineNum=7;
BOM.ItemId="freight";
BOM.BOMQty=1;
BOM.Calculation=1;

BOM.insert();

if there is alreayd an item with item id freight i do not  want to insert if not then I should be able to insert

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    237,882 Most Valuable Professional on at

    First run a select to see if the record doesn't exist. Alternatively, you could try to insert it and then catch the exception.

    By the way, don't forget calling validateWrite() before insert() and update() (and checking its return value).

  • Community Member Profile Picture
    on at

    Martin,

    Thanks for the reply. Right now its not throwing error when i insert. Its just inserting duplicates. This is first X++ program , can you please refer to any resources  to do this and also how to write validatewrite(). I have noticed there is not much information on X++ online compared C#, that's very disocuraging.

  • Verified answer
    Rudi Hansen Profile Picture
    4,075 on at

    Try this:

    static void TestInsertBOM(Args _args)
    {
        BOM bom;
    
        select firstonly bom
            where bom.LineNum == 7
            &&    bom.ItemId  == "freight";
    
        if(!bom.RecId)
        {
            bom.LineNum     = 7;
            bom.ItemId      = "freight";
            bom.BOMQty      = 1;
            bom.Calculation = 1;
            bom.insert();
        }
    }
  • Community Member Profile Picture
    on at

    you are awesome sir, thank you

  • Martin Dráb Profile Picture
    237,882 Most Valuable Professional on at

    validateWrite() does some kernel-level checks (namely enforcing reference integrity) and can be overridden to check additional business-logic rules (AX is full of example, just search for them). You shouldn't call insert() on something invalid. For example, you already have a rule saying that duplicate records shouldn't be inserted, but you failed to validate it.

    Depending on your requirements, you may skip insert() or throw an exception if validations fail. For example:

    if (!bom.validateWrite())
    {
        throw error(...);
    }

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
Priya_K Profile Picture

Priya_K 4

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#3
Sukrut Parab Profile Picture

Sukrut Parab 2 Moderator

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans