Author Topic: Upsert capability - Check for existing and update before creating new elements  (Read 172 times)

jsandman77

  • Newbie
  • *
  • Posts: 2
Hi there, is there a code snippet somewhere that shows the best way to check for an existing element by ID to do an upsert rather than create a new element? I went through the examples and couldn't find one that does this. Thoughts?

Phil Beauvoir

  • Archi Guy
  • Hero Member
  • *****
  • Posts: 1501
  • Archi Guy
    • Archi
Hi,

you could use the ID selector to get an element:

Code: [Select]
var element = $("#id-1234")
if "element" is returned or is null then you can proceed on that basis.

https://github.com/archimatetool/archi-scripting-plugin/wiki/jArchi-Collection#selectors
If you value and use Archi please consider making a donation! https://www.archimatetool.com/donate

jsandman77

  • Newbie
  • *
  • Posts: 2
Thanks Phil. Doing that already.

I am using the code example to export / import to a csv and have the properties show up as columns. Unfortunately, the import duplicates everything so I was looking to tweak it so that it would update all of the properties when an element id is already found.

Is there a "round trip" set of scripts where you can export to a CSV, do some bulk updates to properties and then re-import to update the model?

Phil Beauvoir

  • Archi Guy
  • Hero Member
  • *****
  • Posts: 1501
  • Archi Guy
    • Archi
Sounds like a problem in the logic of the code. When parsing the CSV file, if you are able to get an element by its ID then you can update its name, documentation, properties etc. If the element does not exist then it's up to the code whether to ignore it or create a new element. Perhaps the code example you are using needs more adjustment to suit your case?
If you value and use Archi please consider making a donation! https://www.archimatetool.com/donate

Jean-Baptiste Sarrodie

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 647
  • Archi Evangelist and Contributor. Visionary.
    • About me...
If you value and use Archi please consider making a donation! https://www.archimatetool.com/donate