Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: Thomas Arnbjerg on January 18, 2023, 10:15:28 pm

Title: Undefined variable in javascript script
Post by: Thomas Arnbjerg on January 18, 2023, 10:15:28 pm
Hi all,

When I'm debugging a javascript script containing the following fragment the variable 'traceElement' is reported as 'undefined' at the first line containing 'traceElement.'. Any suggestions?
..
if (currentRequirement.Type == "Requirement"){
   Session.Output( "Found link between " + currentUseCase.Alias + " to " + currentRequirement.Alias );
   var traceElement as EA.Element;               
   traceElement = tracePackage.Elements.AddNew( currentUseCase.Alias + " to " + currentRequirement.Alias, "");
   traceElement.TaggedValues.AddNew("from", currentUseCase.Alias);
   traceElement.TaggedValues.AddNew("to", currentRequirement.Alias);
   traceElement.Update();
   tracePackage.Elements.Refresh();
}
..
Title: Re: Undefined variable in javascript script
Post by: Paolo F Cantoni on January 18, 2023, 11:01:28 pm
Hi all,

When I'm debugging a javascript script containing the following fragment the variable 'traceElement' is reported as 'undefined' at the first line containing 'traceElement.'. Any suggestions?
..
if (currentRequirement.Type == "Requirement"){
   Session.Output( "Found link between " + currentUseCase.Alias + " to " + currentRequirement.Alias );
   var traceElement as EA.Element;               
   traceElement = tracePackage.Elements.AddNew( currentUseCase.Alias + " to " + currentRequirement.Alias, "");
   traceElement.TaggedValues.AddNew("from", currentUseCase.Alias);
   traceElement.TaggedValues.AddNew("to", currentRequirement.Alias);
   traceElement.Update();
   tracePackage.Elements.Refresh();
}
..
Hi Thomas,
The first thing I'd do is to change the name to see if it was name related or structural.


Paolo
Title: Re: Undefined variable in javascript script
Post by: Thomas Arnbjerg on January 18, 2023, 11:11:30 pm
Thanks for the tip.

Changed the name to foobar - did not solve the problem. Also I tried to make the variable global (yikes) - also without solving the issue.

Also the variable does not appear under 'locals'
Title: Re: Undefined variable in javascript script
Post by: Geert Bellekens on January 18, 2023, 11:32:20 pm
Did the addNew operation fail for some reason? (because you passed an empty string as the type)

It might have failed silently. IIRC there is a getlatestError thing somewhere in like Repository. It might help, but I'm not sure.

Geert
Title: Re: Undefined variable in javascript script
Post by: Thomas Arnbjerg on January 18, 2023, 11:39:30 pm
Modified the script as follows. 'lastError' is empty in both locations.
'newElem' (renamed variable) is now reported as undefined on the line after the second assignment to 'lastError'. 'newElem' still does not appear under 'locals'.

..
if (currentRequirement.Type == "Requirement"){
    var newElem as EA.Element;
    var lastError;
    lastError = Repository.GetLastError();
    Session.Output( "Found link between " + currentSre.Alias + " to " + currentRequirement.Alias );                                    
    newElem = tracePackage.Elements.AddNew( currentSre.Alias + " to " + currentRequirement.Alias, "");                     
    lastError = Repository.GetLastError();
    newElem.TaggedValues.AddNew("from", currentSre.Alias);
    newElem.TaggedValues.AddNew("to", currentRequirement.Alias);
    newElem.Update();
    tracePackage.Elements.Refresh();
}
..
Title: Re: Undefined variable in javascript script
Post by: Geert Bellekens on January 18, 2023, 11:43:28 pm
Try passing a type as second parameter to the AddNew operation. An empty string is not a valid type.

Geert
Title: Re: Undefined variable in javascript script
Post by: Thomas Arnbjerg on January 18, 2023, 11:48:00 pm
That did it! Thanks! Set the second parameter to 'Class'
Title: Re: Undefined variable in javascript script
Post by: Eve on January 23, 2023, 02:12:33 pm
I'm guessing your tags not working will be your next issue.
Code: [Select]
var newTag as EA.TaggedValue;
newTag = newElem.TaggedValues.AddNew("from", currentSre.Alias);
newTag.Update();
newTag = newElem.TaggedValues.AddNew("to", currentRequirement.Alias);
newTag.Update();