Author Topic: DataMiner - Accessing script class attributes  (Read 1292 times)

sjf

  • EA User
  • **
  • Posts: 24
  • Karma: +1/-0
    • View Profile
DataMiner - Accessing script class attributes
« on: October 02, 2024, 11:37:11 pm »
I have created a bunch of dataminers which generic scripts to import data from our CMDB. The scripts will for instance read an excel spreadsheet for a particular set of elements, check in a package to see if the elements already exist (if not create a new element), if it exists it will update any changed tags and remove any elements which don't exist in the import file.

This requires the excel to have the element name, description and columns containing the tags together with a worksheet which contains the mapping of column number to tag name. This is all good and works.

I would like to be able to access attributes of the DMScripts class to for instance hold the GUID of the package folder to load to, the names of the DMSets for the mapping dataset & the data dataset.

I have tried using this.attribute but get an error, I wondered whether anyone has found whether this is possible?

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +395/-301
  • I'm no guru at all
    • View Profile
Re: DataMiner - Accessing script class attributes
« Reply #1 on: October 03, 2024, 03:30:15 am »
So you got an error. And now you let us guess, which. Right?

q.

sjf

  • EA User
  • **
  • Posts: 24
  • Karma: +1/-0
    • View Profile
Re: DataMiner - Accessing script class attributes
« Reply #2 on: October 03, 2024, 08:56:27 pm »
Apologies,

on the DMScript class I have defined an attribute:

SomeData of type String with an initial value of "My Test"

In my DMScript code I have:

let x = this.SomeData;
Session.Output(x);

I get the error:
Data Miner: Failed to run 'ProcessData', script error: Parameter 1 type mismatch, Line:10

If I change the output to
Session.Output("[" + x + "]");

I get the output:
[undefined]

So I am looking to be able to access attributes of the DMScript class from the code.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +395/-301
  • I'm no guru at all
    • View Profile
Re: DataMiner - Accessing script class attributes
« Reply #3 on: October 03, 2024, 09:03:22 pm »
My VB is sort of not existing, but that's likely one of the traps where you deal with objects you need to make aware to that mumble (some call it language). Geert would know... Probably you have to declare it with dim.

q.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13171
  • Karma: +549/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: DataMiner - Accessing script class attributes
« Reply #4 on: October 03, 2024, 09:28:16 pm »
Geert would know... Probably you have to declare it with dim.
You would, if this were VBScript, but this looks like Javascript or Jscript.

Can you share the whole code, and not just the part that errors?
I'm gessing you get the same result if you do

Session.Output(this.SomeData)?


Geert

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13171
  • Karma: +549/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: DataMiner - Accessing script class attributes
« Reply #5 on: October 03, 2024, 09:37:40 pm »
The clue is probably how you defined this attribute with default value.

It it correct that you didn't define that in code, but in your model, as an attribute of the DMScripts class?

In that case this.SomeData is definitely not going to work. There is little to no magic involved when it comes to EA.
If you want to access the attributes of the element on which the script is defined, then you'll have to do that yourself.

What you can do is (in VBScript)

Code: [Select]
dim x
dim dmScriptsClass
set dmScriptsClass = Repository.GetElementByGUID(<guid of the DMScripts class>)
for each attr in dmScriptsClass
  if attr.Name = "SomeData" then
     x = attr.Default
     exit for
  end if
next

Geert

sjf

  • EA User
  • **
  • Posts: 24
  • Karma: +1/-0
    • View Profile
Re: DataMiner - Accessing script class attributes
« Reply #6 on: October 04, 2024, 01:27:48 am »
The clue is probably how you defined this attribute with default value.

It it correct that you didn't define that in code, but in your model, as an attribute of the DMScripts class?

In that case this.SomeData is definitely not going to work. There is little to no magic involved when it comes to EA.
If you want to access the attributes of the element on which the script is defined, then you'll have to do that yourself.

What you can do is (in VBScript)

Code: [Select]
dim x
dim dmScriptsClass
set dmScriptsClass = Repository.GetElementByGUID(<guid of the DMScripts class>)
for each attr in dmScriptsClass
  if attr.Name = "SomeData" then
     x = attr.Default
     exit for
  end if
next

Geert

Thanks, I'm happy to use the Repository API to get this. 8)

I defined it as an attribute on the DMSCript class in the DataMiner diagram. I'm not sure I would even know how to define an attribute in the code of the DMScript class.

You are correct that this is JavaScript, as I haven't used VBScript for a long time, but used to using the API for creating & managing elements.

I'm sure that using this to get attributes of a runtime class works with the in-built Add-In model but may be wrong.

Any other suggestions warmly welcomed.