Book a Demo

Author Topic: Script fragment error only on first try  (Read 5538 times)

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Script fragment error only on first try
« on: February 26, 2016, 06:28:50 pm »
I have  a weird problem that I can't seem to get my head around.

I'm using RTF templates with some script fragments. Now usually the first time I generate a document I get an error (or more) and some of the content from the script fragments has not been generated. Not all of it mind you. Some parts (using the same script fragment template) generate just fine, but other don't.

When I try that a second time it (almost always) generates perfectly fine.
What do they say again about someone who does exactly the same thing but expects a different outcome ???

It feels like the script engine has to "warm up" before it is ready to really do the job. :-\
Running the script manually always works just fine.

Anyone had the same problem, or has an idea how to solve it?

Anyway here's the script I use in the script fragment

Code: [Select]
'[path=\Projects\Project A\Template fragments]
'[group=Template fragments]
option explicit

!INC Local Scripts.EAConstants-VBScript
!INC Atrias Scripts.Util

'
' Script Name:
' Author:
' Purpose:
' Date:
'
function MyPackageRtfData(packageID, tagname)
dim package as EA.Package
dim element as EA.Element
set package = Repository.GetPackageByID(packageID)
if not package is nothing then
set element = Repository.GetElementByGuid(package.PackageGUID)
if not element is nothing then
MyPackageRtfData = MyRtfData (element.ElementID, tagname)
end if
end if
end function

function MyRtfData (objectID, tagname)

dim xmlDOM
'set  xmlDOM = CreateObject( "Microsoft.XMLDOM" )
set  xmlDOM = CreateObject( "MSXML2.DOMDocument.4.0" )
xmlDOM.validateOnParse = false
xmlDOM.async = false

dim node
set node = xmlDOM.createProcessingInstruction( "xml", "version='1.0'")
    xmlDOM.appendChild node
'
dim xmlRoot
set xmlRoot = xmlDOM.createElement( "EADATA" )
xmlDOM.appendChild xmlRoot

dim xmlDataSet
set xmlDataSet = xmlDOM.createElement( "Dataset_0" )
xmlRoot.appendChild xmlDataSet

dim xmlData
set xmlData = xmlDOM.createElement( "Data" )
xmlDataSet.appendChild xmlData

dim xmlRow
set xmlRow = xmlDOM.createElement( "Row" )
xmlData.appendChild xmlRow

dim element as EA.Element
set element = Repository.GetElementByID(objectID)

dim formattedAttr
set formattedAttr = xmlDOM.createAttribute("formatted")
formattedAttr.nodeValue="1"

dim descriptionfull
descriptionfull = getTagContent(element.Notes, tagname)

dim xmlDescNL
set xmlDescNL = xmlDOM.createElement( "DescriptionNL" )

xmlDescNL.text = getTagContent(descriptionfull, "NL")
xmlDescNL.setAttributeNode(formattedAttr)
xmlRow.appendChild xmlDescNL

set formattedAttr = xmlDOM.createAttribute("formatted")
formattedAttr.nodeValue="1"

dim xmlDescFR
set xmlDescFR = xmlDOM.createElement( "DescriptionFR" )
xmlDescFR.text = getTagContent(descriptionfull, "FR")
xmlDescFR.setAttributeNode(formattedAttr)
xmlRow.appendChild xmlDescFR

MyRtfData = xmlDOM.xml
end function

'msgbox MyPackageRtfData(3357,"")
function test
dim outputString
dim fileSystemObject
dim outputFile

outputString =  MyRtfData(62899, "definition")

set fileSystemObject = CreateObject( "Scripting.FileSystemObject" )
set outputFile = fileSystemObject.CreateTextFile( "c:\\temp\\NLFRtest.xml", true )
outputFile.Write outputString
outputFile.Close
end function

'test