Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • John 10 posts 161 karma points
    Mar 27, 2013 @ 13:17
    John
    0

    v6.0.2 - Document properties need preincrementing instead of postincrementing to get saved

    I'm migrating a site from v4.7.0 to v6.0.2 and have found strange behaviour when editing a Document's properties.

    Incrementing a "quantity" property in a document only works if it is done with the preincrement operator. The following method should return a number that increments each time it is called (e.g. via /base/namespace/IncrementQuantity/1234)

            [RestExtensionMethod(returnXml = false)]
            public static string IncrementQuantity(int id)
            {
                Document doc = new Document(id);
                int quantity = Convert.ToInt32(doc.getProperty("quantity").Value);
                doc.getProperty("cantidad").Value = quantity++;
                doc.Save();
                quantity = Convert.ToInt32(doc.getProperty("quantity").Value);
                return string.Format("quantity: {0}", doc.getProperty("quantity").Value.ToString());
            }
    

    The number never increments though, and stepping through the code shows that as soon as doc.Save() is called the value of quantity is what it originally was. In the backoffice that node's property also stays the same.

    Changing the line

    doc.getProperty("cantidad").Value = quantity++;

    to

    doc.getProperty("cantidad").Value = ++quantity;

    Fixes the issue and results in the number incrementing each time the page is refreshed.

    Any clues as to why?

  • kim Thomsen 59 posts 277 karma points
    Mar 27, 2013 @ 13:31
    kim Thomsen
    100

    Isnt this a c# thing¨

    doc.getProperty("cantidad").Value= quantity++;

    this will add quantity to doc.getProperty("cantidad").Value and then +1 to quantity

    And doc.getProperty("cantidad").Value=++quantity;

    Will add 1 to quantity and then set doc.getProperty("cantidad").Value=quantity

  • John 10 posts 161 karma points
    Mar 27, 2013 @ 14:21
    John
    1

    You're right, I can't believe I missed that. My only excuse is my brain was fried from trying to pinpoint the issue I was having :)

    The original issue I was having was wrapped in a few layers of abstraction and this was the bare-bones reproducible code I arrived at.

    The original issue was updating a C# property that wrapped access to Document.getProperty was behaving strangely, e.g.

    wrappedDocument.Quantity = 1;
    wrappedDocument.Quantity = wrappedDocument.Quantity + 1; wrappedDocument.Save();
    //wrappedDocument.Quantity == 1 instead of 2.  

    Was causing the updated quantity to get saved to the database but reverted to the original value in the variable wrappedDocument. On the other hand this code worked fine:

    //wrappedDocument.Quantity = 1;
    wrappedDocument.Quantity = 1 + wrappedDocument.Quantity; wrappedDocument.Save();
    //wrappedDocument.Quantity == 2, as expected. 

    I've gone back and retested and am not able to reproduce the original issue though, so I don't know what's up.

Please Sign in or register to post replies

Write your reply to:

Draft