Book a Demo

Author Topic: Create instances only if they don't exist currently with a specific ClassifierID  (Read 3220 times)

pianoman

  • EA Novice
  • *
  • Posts: 16
  • Karma: +0/-0
    • View Profile
Hi All,

I have a script running multiple functions to automate a lot of things among which creation of instances is one of them. I have a SQL Query that specifies the ClassifierIDs from which to create instances. This script works great and all, but I want to add one more level of complexity.

I want to make sure the script doesn't create instances if there is a case that there is an instance with a specified ClassifierID existing already. My function below has inputs of classifierID, stereotype, and name provided from a query in another function. I'm looking for help in figuring out the if statement to check for an existing instance with a classifierID that will be specified by my query.

Code: [Select]
function addInstance(classifierID, stereotype, name)
Dim package
Dim newinstance AS EA.Element
set package = Repository.GetTreeSelectedPackage()
if newinstance.ClassifierID exists for some instance in the model
                      do nothing
                else if
                      do the below things
                end if
' create the new instance element
set newinstance = package.Elements.AddNew("", stereotype)
' assign a classifier element to the new instance element
newinstance.ClassifierID = classifierID
' assign a name to the new instance element
newinstance.Name = name
' update and refresh for the push to happen to EA
newinstance.Update
package.Elements.Refresh
addInstance = newinstance.ElementID

End Function

Appreciate any help! Thanks!

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13471
  • Karma: +571/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Use Repository.SqlQuery with a query similar to this:

Code: [Select]
select o.Object_ID from t_object o where o.Classifier = 123
This returns an xml string with the results of the query that you can parse then.

Alternative is to use Repository.GetElementSet with the same query.

That returns a collection of elements. This is a bit slower, especially if there are a lot of elements to be returned.

Check also https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library/blob/master/Framework/Utils/Util.vbs for lots of utility functions that use SQLQuery

Geert

KP

  • EA Administrator
  • EA Expert
  • *****
  • Posts: 2919
  • Karma: +54/-3
    • View Profile
Use Repository.SqlQuery with a query similar to this:

Code: [Select]
select o.Object_ID from t_object o where o.Classifier = 123
This returns an xml string with the results of the query that you can parse then.

Alternative is to use Repository.GetElementSet with the same query.

That returns a collection of elements. This is a bit slower, especially if there are a lot of elements to be returned.

Check also https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library/blob/master/Framework/Utils/Util.vbs for lots of utility functions that use SQLQuery

Geert


If you just want to know if there are records, I think you can say something like SELECT Count(*) AS Num FROM ... etc and then see if Num is non-zero. Sorry I don't remember the exact syntax.
The Sparx Team
[email protected]

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13471
  • Karma: +571/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Use Repository.SqlQuery with a query similar to this:

Code: [Select]
select o.Object_ID from t_object o where o.Classifier = 123
This returns an xml string with the results of the query that you can parse then.

Alternative is to use Repository.GetElementSet with the same query.

That returns a collection of elements. This is a bit slower, especially if there are a lot of elements to be returned.

Check also https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library/blob/master/Framework/Utils/Util.vbs for lots of utility functions that use SQLQuery

Geert


If you just want to know if there are records, I think you can say something like SELECT Count(*) AS Num FROM ... etc and then see if Num is non-zero. Sorry I don't remember the exact syntax.
Yes indeed. The syntax is just fine. Just don't forget the alias 'AS Num'.
If you don't you get a very cryptic xml error message because EA tries to create an xml node without a name.

Geert