Save item transaction

オフライン

Hello

I have a certain Item1 and on after save event of this item I trigger another item2 (custom item we created) save. In case first one fails the second wont trigger. But the otherway around obviously doesnt work. This is handled client side.

Is it possible to create transaction scope and add two transactions for saving those two items so if either fails we roll back. Each transaction would be executing applyAML (i guess) 

How would you approach this?

Best regards

Parents Reply Children
  • Hello,

    I think this idea of adding another item to the transaction AML could work, but it would need to be done in an onBeforeAdd server event instead of an onAfterAdd server event.

    As an alternative, you could add the attribute serverEvents="0" when you create the second item in the onAfterAdd server event. This will prevent the creation of that item from triggering another server event and will prevent an infinite loop.

    You can add this programmatically using the line below.

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

    Chris

    Christopher Gillis

    Aras Labs Software Engineer

  • Mr. Gillis my 2nd item (or items) are made after first one is created. I'll show you excerpt of the code

    1. registered handler. So when i save Item1 i call some_method which goes over Item1 (its properties and relationships etc...) 

    mainWnd.registerCommandEventHandler(window, function () {  some_method(); }, 'after', 'save');
    2. some_method as said iterates over props and relationship of Item1  and calls several different serverside methods which add, delete or update various Item2 types
    Item2 is not handled in onAfterAdd of Item2 due to specific logics. Not sure if even it could be done (I didnt create those methods). Come to think of it I cant do mine nor your ideas. My first idea would maybe work (with loads of rework) if i could calll single AML for item1 with addition of method calls for Item2 inside the same tag. But i don't see how that would work as a single transaction. Is that possible?
    Better sollution would be if AML calls in serverside  would return some kind of tx which i could rollback later if any subsequent tx fails - is this possible?
  • Hello,

    This kind of transaction rollback exists inherently inside of Aras Innovator. For example, if I run the method code below, the transaction that created the Part will be rolled back because of the error thrown at the end.

    Innovator inn = this.getInnovator();

    // Create a new part
    Item part = inn.newItem("Part", "add");
    part.setProperty("item_number", "My Part 1");
    part.apply();
    // The part now exists in the database

    // But the transaction is rolled back when this error is thrown
    // If we check for the part after running this method, it will not exist
    return inn.newError("Can not add part");

    This kind of transaction rolls back for any number of nested server calls. For instance, if I call another method which successfully creates an item and then throw an error, the item created in the called method is still part of the same transaction and will be rolled back when the error is thrown from the calling method. Similarly, if an error is thrown in a server event, any and all transactions associated with the triggering item or any other server events will be rolled back.

    I'd suggest taking a look at your existing approach to see if some of this logic could be migrated from the client to the server. We typically recommend keeping this kind of logic inside of a server event so that the same functionality will be run whether the item is saved through the client or an external application.

    Chris

  • Technically i could take the aml of the object then used the loadAML and started adding logics on the serverside. I dont think my colleague will be all too happy:)

    Btw is there a way to override the save button on specific form (item type)

  • Hello,

    You can use CUI to add a new button as outlined in this blog post. You can then follow relatively similar steps to disable the existing Save button. If you run into any trouble with the exact implementation of this CUI button, I'd recommend creating a new forum post to ask for help since it's a much different question to the one you originally asked.

    Chris

  • Thank you for now Mr Gillis I will have to come back when i've started hacking at this. I will definitely use the information from this thread!