onBeforeUpdate - 'Not a single item' error

I have onBeforeAdd and onBeforeDelete methods on an ItemType to change a property depending on the status of that property on other instances of that ItemType. Those work fine. But when I tried to add an onBeforeUpdate method to detect when another property was changed, the 'Not a single item' error shows up. I'm also not able to use the apply() method on any item that is changed on the update method. I just need to know how to make a change to the item stick when using an Update server method.

Parents
  • Hello,

    The onBeforeUpdate event triggers before the standard update functionality performs the apply() for you automatically. To update the property, all you need to do is set the property on the item and return that item from the onBeforeUpdate like so.

    this.setProperty("item_number", "New Item Number");

    return this;

    Chris

    Christopher GIllis

    Aras Labs Software Engineer

  • Thank you for your response. I have conditional statements to maybe change 'this', but I also need to be able to change every other ItemType of this type in the database. So I loop through them and set their properties depending on the value of 'this'. This works fine with onBeforeAdd and OnBeforeDelete, but onBeforeUpdate either freezes Aras or gives the 'Not a single item' error. I guess I need to know how to change other items in a loop using onBeforeUpdate.

  • Hello,

    The issue you're likely running into is that updating the other items in the database is causing an endless loop of onBeforeUpdates. That's why you haven't seen the issue with the onBeforeAdd or onBeforeDelete events.

    One workaround to this would be to specify that you do not want server events to run when you make your updates within the onBeforeUpdate event. You can do this by adding an attribute called serverEvents and setting it to 0. This would look like the code below.

    myItem.setAttribute("serverEvents", "0");
    myItem.apply();

    One note is that you'll need to set this attribute on every item that you're trying to update. We have a blog post on using these standard attributes if you're interested in learning more about them. 

    Chris

Reply
  • Hello,

    The issue you're likely running into is that updating the other items in the database is causing an endless loop of onBeforeUpdates. That's why you haven't seen the issue with the onBeforeAdd or onBeforeDelete events.

    One workaround to this would be to specify that you do not want server events to run when you make your updates within the onBeforeUpdate event. You can do this by adding an attribute called serverEvents and setting it to 0. This would look like the code below.

    myItem.setAttribute("serverEvents", "0");
    myItem.apply();

    One note is that you'll need to set this attribute on every item that you're trying to update. We have a blog post on using these standard attributes if you're interested in learning more about them. 

    Chris

Children