title: Enterprise Architect scripting with javascript - Add a legend to a diagram
body: <p>I am trying to add a legend to a digram partly following question: <a href="https://stackoverflow.com/questions/26280146/enterprise-architect-scripting-with-java-add-customproperty">Enterprise Architect scripting with java - add CustomProperty</a> but I have  no luck in getting a visible legend on my diagram. Here is my code:</p>
  
  <pre><code>function addLegend()
  {
     Repository.EnsureOutputVisible( "Script" );
     Repository.ClearOutput("Script");
     var pkg as EA.Package; 
  
     pkg = Repository.GetTreeSelectedPackage();   
     elements = pkg.Elements;
     var legend = elements.AddNew('Diagram Legend', 'Text');
  
     legend.Subtype = 76;
     legend.Update();
     elements.Refresh();
     var diagram as EA.Diagram;
  
     diagram = Repository.GetDiagramByID(10);
     Session.Output(diagram.Name);
     diagramObjects = diagram.DiagramObjects;
  
  
     diagramObject = diagramObjects.AddNew("l=100; r=100; t=100; b=500;", "");
     diagramObject.ElementID = legend.ElementID;
     diagramObjects.Refresh();
     diagram.Update();
  }
  </code></pre>
  
  <p>` 
  As said no legend is displayed - any help appreciated. My EA version is 12.0.</p>