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 of multiple records

(0) ShareShare
ReportReport
Posted on by

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

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

    One P_ID value can't ever be P101 and P103 at the same time. You probably meant to select both P101 and P103, therefore you must select records with P_ID P101 or P103 (where pt.P_ID=="P101" || pt.P_ID=="P103").

    Another bug is the semicolon after the select statement.

    When you fix the previous bug, you'll meet another one. Because ttsCommit will be called only if the condition is met, the transaction will be left open in all other cases (!).

  • Community Member Profile Picture
    on at

    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.

  • Suggested answer
    Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at

    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
    on at

    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?

  • Community Member Profile Picture
    on at

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

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

    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
    on at

    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.

  • Community Member Profile Picture
    on at

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

  • Community Member Profile Picture
    on at

    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.?

  • Verified answer
    Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at

    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.

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