Obscure/advanced AppScript question .... deleting an item related to other items...

We have 2 Primary tables with 3 different types of items:

In primary table Shipping_Pkgs we have a "Shipping_Manifest" type item and a "Manifest_Line" type item.  In the 2nd primary table (Envelopes) we have Envelope type items.

The user creates a new Shipping Manifest.  In the following description, assume that the "current item" is a Shipping Manifest.  In the "Add Envelopes" transition the user selects Envelope items to be added to the Shipping Manifest.  We have scripts that run that create a Manifest Line record for each Envelope selected by the user.  We associate that Manifest Line with the Shipping Manifest via a single-rel field.  The script then associates the selected Envelope to the Manifest Line item created by the script, and the Manifest Line back to the Envelope via a pair of single-relational fields.  The Shipping Manifest is associated to the Manifest Line via a multi-relational field because a user wants to ship multiple Envelopes in a single Shipping Manifest.  All-in-all we have 3 single-relational fields and 1 multi-relational field to track these relationships.

The users know about Envelopes and Shipping Manifests, but not about Manifest Line items .... the "Manifiest Line" is just a way to relate a Shipping Manifest to one or more Envelopes.  The embedded reports that the user sees displays the Manifest Lines on the Shipping Manifest but it shows Envelope information (via sub-relational fields).  The user thinks they're seeing Envelopes.

So:

  • A Shipping Manifest can have 0 or more Manifest Lines items.  The related Manifest Line items are in a Multi-Relational field "Manifest Lines" (note plural).
  • A Manifest Line item is created by scripts whenever a user wants to add an Envelope to their Shipping Manifest.
  • The Envelope item points to the Manifest Line item with the "Manifest Line" (note singular) single-relational field.  The Manifest Line item points back to that same Envelope with the "Envelope" single-relational field.
  • The Manifest Line item is related to it's "parent" Shipping Manifest via the "Manifest" single-relational field.

Let's say we want to "remove" an Envelope from the Shipping Manifest.  Currently our scripts are doing all the work of deleting the Manifest Line item, clearing up the Envelope's "Manifest Line" field, removing the ID of the deleted Manifest Line item from the Shipping Manifest's "Manifest Lines" field.

I was thinking ... what if our scripts just delete the Manifest Line item??  I think if I manually delete a Manifest Line item in the GUI (with the Delete transition), SBM will find all the references to the deleted Manifest Line record and remove those.  Is that right??  I'm willing to bet that deleting the Manifest Line record via the AppRecord "Delete" method might do the same thing. Does it?  And if so, is it faster to let SBM clean up all the references to the deleted item, or are we better off continuing to do it ourselves in the AppScripts?

Now for some obscure extra-points questions:

Will SBM clean up everything and delete the Manifest Line record if we only modify only one of the single or multi relational fields???  I.e. will SBM do all that work if we just do one of the following:

  1. Remove the ID of the Manifest Line to be deleted from the Shipping Manifest's "Manifest Lines" multi-relational field (disassociating the Manifest Line from the Shipping Manifest)?
  2. Clear the Manifest Line's "Manifest" single-relational field (disassociating the Shipping Manifest from the Manifest Line)??
  3. Clear the Manifest Line's "Envelope" single-relational field (disassociating the Envelope from the Manifest Line)??
  4. Clear the Envelope's "Manifest Line" single-relational (disassociating the Manifest Line from the Envelope)???

My guess is "no" to any of those.

  • Verified Answer

    +1  

    Wow. This is an intense question for New Years. I'm glad I waited until now to read it. ;-)

    This idea is actually the same thing we do in-house to link cases and defects. We have a "middle" record that sits between case records and defect records. These "link" records that only contain single-relational fields to the case and the defect and a bunch of sub-relational fields for reporting. The difference for us is that we only have these 2 single-relational fields in the "link" record. We don't try to maintain relational fields going back to the parent records.

    I setup a little test to see what happens to the relational fields if you delete the "middle" record (like manifest line in your example). I used an Aux table, but i'm sure it's all the same. When I did the delete, I got this message from the UI: 

    Sure enough, the relational fields that referenced this record were all updated automatically. I was surprised honestly. I was expecting the relational fields to show with a value of "deleted" or something similar. (That's what happens if you delete a user who is an owner of a record.) But, no. Instead SBM cleaned up the records very nicely. If your scripts are already doing the cleanup, I probably wouldn't take the time to re-write the scripts, but this is good it know. If you need to update the scripts for something else later, you could pull out the code then. 

    For your second question, I would answer no to all 4 points. SBM won't delete anything unless you specifically delete it. Removing references from different fields to a record will not automatically delete the record. SBM is an information hog and loves to keep data and information forever, if you let it. That's part of why auditor love it. SBM save everything and records every change. 

    Hope that helps.