Book a Demo

Author Topic: Adding Tags via Javascript  (Read 8471 times)

hofzge

  • EA Novice
  • *
  • Posts: 7
  • Karma: +0/-0
    • View Profile
Adding Tags via Javascript
« on: January 25, 2018, 12:08:07 am »
When I try to tag elements in a model I get this problem:
Quote
Error
Test Scripts.AliasToTag
currentElement.TaggedValues is undefined, Line: 115

This error occurs when I try to tag the elements:
Code: [Select]
var currentElement as EA.Element;
currentElement = theElement;

var taggedValue as EA.TaggedValue;
taggedValue = null;

if(currentElement != null)
{
// Output the current element's name and alias
Session.Output(indent + "Element=" + currentElement.Name + ", Type=" + currentElement.Type + ", Alias=" + currentElement.Alias);

        taggedValue = currentElement.TaggedValues.AddNew(taggedValueName, taggedValueValue);
currentElement.TaggedValues.Refresh();
taggedValue.Update();
}

in this line:
taggedValue = currentElement.TaggedValues.AddNew(taggedValueName, taggedValueValue);

Nabil

  • EA User
  • **
  • Posts: 149
  • Karma: +5/-2
    • View Profile
    • View My LinkedIn Profile Here
Re: Adding Tags via Javascript
« Reply #1 on: January 25, 2018, 12:15:59 am »
Try commenting the below lines and say are you getting session output value?

[/quote]
Code: [Select]


        taggedValue = currentElement.TaggedValues.AddNew(taggedValueName, taggedValueValue);
currentElement.TaggedValues.Refresh();
taggedValue.Update();

[/quote]
Refer LocalScripts -> Element Extras Example

// Add a new tag
      var tags as EA.Collection;
      tags = theElement.TaggedValues;
      
      var newTag as EA.TaggedValue;
      newTag = tags.AddNew( "MyTag", "Number" );
      newTag.Update();
      tags.Refresh();
Nabil

hofzge

  • EA Novice
  • *
  • Posts: 7
  • Karma: +0/-0
    • View Profile
Re: Adding Tags via Javascript
« Reply #2 on: January 25, 2018, 12:38:23 am »
I tried to do it that way and added this:
Code: [Select]
var currentElement as EA.Element;
currentElement = theElement;
var tags as EA.Collection;
tags = currentElement.TaggedValues;
var newTag as EA.TaggedValue;
newTag = tags.AddNew(taggedValueName, taggedValueValue);

Now I get a similar error in your code:
Quote
Error
Test Scripts.AliasToTag
tags is undefined, Line: 106

which is this line:
newTag = tags.AddNew(taggedValueName, taggedValueValue);

The reason must be somewhere else...

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Adding Tags via Javascript
« Reply #3 on: January 25, 2018, 12:38:40 am »
Where is
Code: [Select]
theElement coming from?
This variable is probably not holding an object of type EA.Element

Geert

hofzge

  • EA Novice
  • *
  • Posts: 7
  • Karma: +0/-0
    • View Profile
Re: Adding Tags via Javascript
« Reply #4 on: January 25, 2018, 12:54:40 am »
Code: [Select]
function SetElementTaggedValue(indent, theElement, taggedValueName, taggedValueValue)
{
var currentElement as EA.Element;
currentElement = theElement;
var tags as EA.Collection;
tags = currentElement.TaggedValues;
var newTag as EA.TaggedValue;
newTag = tags.AddNew(taggedValueName, taggedValueValue);

and it is called like this:
Code: [Select]
var currentElement as EA.Element;
currentElement = theElement;
var childElementEnumerator = new Enumerator(currentElement.Elements);


// Tag the current element with its alias
SetElementTaggedValue(indent, currentElement, "old_ID", currentElement.Alias);
« Last Edit: January 25, 2018, 01:02:10 am by hofzge »

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Adding Tags via Javascript
« Reply #5 on: January 25, 2018, 01:11:00 am »
I'm still not seeing you initialize "theElement".
If you don't initialize the variable then it will have a null value (and null.TaggedValues.AddNew() won't work)

Geert

Nizam

  • Prolab Moderator
  • EA User
  • *
  • Posts: 320
  • Karma: +15/-2
  • Model Sharing - Simplified
    • View Profile
    • Professional Model Collaboration
Re: Adding Tags via Javascript
« Reply #6 on: January 25, 2018, 01:17:11 am »
Code: [Select]
function SetElementTaggedValue(indent, theElement, taggedValueName, taggedValueValue)
{
var currentElement as EA.Element;
currentElement = theElement;
var tags as EA.Collection;
tags = currentElement.TaggedValues;
var newTag as EA.TaggedValue;
newTag = tags.AddNew(taggedValueName, taggedValueValue);

and it is called like this:
Code: [Select]
var currentElement as EA.Element;
currentElement = theElement;
var childElementEnumerator = new Enumerator(currentElement.Elements);


// Tag the current element with its alias
SetElementTaggedValue(indent, currentElement, "old_ID", currentElement.Alias);

I guess you are passing theElement, which could be from GetTreeSelectedObject or similar. These functions may not always return a value.

add if ( theElement != null) condition and see if it goes through.

hofzge

  • EA Novice
  • *
  • Posts: 7
  • Karma: +0/-0
    • View Profile
Re: Adding Tags via Javascript
« Reply #7 on: January 25, 2018, 01:40:04 am »
I think that is what happened:
I initialize it like this:
Code: [Select]
rootElement= Repository.GetTreeSelectedObject();
   
DumpElements("", rootElement);
and it seems this sometimes does not work and I get null. After checking if the tags exist it runs through:
Code: [Select]
if(tags!=null){
newTag = tags.AddNew(taggedValueName, taggedValueValue);
}

Thanks to all the answers. I was not aware the getTreeSelectedObject was this dangerous.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Adding Tags via Javascript
« Reply #8 on: January 25, 2018, 02:18:35 am »
I was not aware the getTreeSelectedObject was this dangerous.

It's not really dangerous, but have to test what it returns. Depending on what you actually selected in the project browser it could any of EA.Package, EA.Diagram, EA.Attribute, EA.Operation, EA.Element or even null (if you haven't selected anything at all in the project browser)

You can't assume it will return an EA.Element

Geert