Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX (Archived)

Update of multiple records

Posted on by Microsoft Employee

Her's the code in which i want to select 2 records from the table using logical and condition && and then update the values accordingly if it satisfies both of the conditions. But it looks like i am writing something wrong. Kindly check this out.

class DisplayClass

{        

   /// <summary>

   /// Runs the class with the specified arguments.

   /// </summary>

   /// <param name = "_args">The specified arguments.</param>

   public static void main(Args _args)

   {        

       PTable pt;

      ttsbegin;

      select forupdate pt

           where pt.P_ID=="P101" &&

                 pt.P_ID=="P103";

       {  

           pt.PStatus = "Sold";

           pt.update();

           ttscommit;

       }

   }

}

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Update of multiple records

    Thanks a lot.  I was having a problem in my build. It worked now.

  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,235 Most Valuable Professional on at
    RE: Update of multiple records

    where pt.P_ID=="P101" || pt.P_ID=="P103" is the correct syntax. You either have a problem in data or in the way how you're testing the code.

    I'm pretty sure that we would have noticed if || operator didn't work.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Update of multiple records

    Hee is the code what i did to update those records:

    class DisplayClass

    {        

       /// <summary>

       /// Runs the class with the specified arguments.

       /// </summary>

       /// <param name = "_args">The specified arguments.</param>

       public static void main(Args _args)

       {        

           PTable pt;

           update_recordset pt

               setting PStatus = "Confirmed"

               where pt.Pricee <= 120;

       }

    }

    So, what i am trying to say is like here i have put my condition on quantity ,similarly i want to put my condition on P_ID so how can i do that.?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Update of multiple records

    And when i write (where pt.P_ID=="P101" || pt.P_ID=="P103"), then P101 is only updated.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Update of multiple records

    I can update them separately defining their condition. But  the requirement is that i have to update multiple records in one line .

    like i want P101, P103, P105 , P107 all of their pstatus to be "sold". So how can i write that.

  • Martin Dráb Profile Picture
    Martin Dráb 230,235 Most Valuable Professional on at
    RE: Update of multiple records

    You still have the same logical error there - no record can ever fulfil your condition, therefore nothing is updated.

    Ask yourself - can any ID be both "P101" and "P103" at the same time? Then please read my first reply once more.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Update of multiple records

    One thing, here i also tried compiling the code without ttsbegin and commit , and also with them. But nothing worked.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Update of multiple records

    Here's what i did as per your advice:

    class DisplayClass
    {
    /// <summary>
    /// Runs the class with the specified arguments.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {
    PTable pt;

    ttsbegin;
    update_recordset pt
    setting PStatus = "Invoicedd"
    where pt.P_ID=="P101" &&
    pt.P_ID=="P103";
    ttscommit;


    }

    }


    However there are no errors.
    But the P101 and P103 are not changing their pstatus to invoicedd.
    Can you tell me why?

  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,235 Most Valuable Professional on at
    RE: Update of multiple records

    You're almost there:

    PTable pt;
    
    ttsBegin;
    
    select forupdate pt
       where ...
    {  
        pt.PStatus = "Sold";
        pt.update();
    }
    
    ttsCommit;

    This would work, but it's inefficient. You don't need to fetch all 100 records from database; you merely want to update PStatus to Sold:

    PTable pt;
    
    update_recordset pt
        setting PStatus = "Sold"
        where ...;

    Also, please don't forget that AX has documentation and that you see thousand examples in the standard application.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Update of multiple records

    Ok, but let me ask something. If i have 100 of records in my table, and i want to update some records accordingly, then how can i do it?

    So that in only one loop, the selected records are updated. Kindly help me with the code.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans