Question
· Sep 18, 2023

Loop through an array or collection in DTL

Hello community,

I want to iterate through a defined collection/list of numbers for example 1,3,4,5 in my DTL.

Something like

While/For/Foreach (x in 1,3,45)

{

///some code

}

I am not able to find the syntax of foreach or while loop to use in the DTL Object script.

Any suggestions, please provide.

Thanks,

Jimmy Christian

Product version: IRIS 2022.1
Discussion (11)2
Log in or sign up to continue

Hello @Jimmy Christian 

You can use the foreach element instead of code block if it's direct set to target and no code logic required. I have attached the sample codes below.  You can refer mr. @Sylvain Guilbaud samples as well.

XData DTL [ XMLNamespace = "http://www.intersystems.com/dtl" ]
{
<transform sourceClass='CSVtoHL7.Inputfile.Record' targetClass='CSVtoHL7.Test.NewClass2' targetDocType='2.5:ADT_A01' create='new' language='objectscript' >
<foreach property='source.EmailList()' key='k1' >
<assign value='source.EmailList.(k1)' property='target.ListOfEmails.(k1)' action='set' />
</foreach>
</transform>
}

DTL

output

Hi @Jimmy Christian,

the syntax in XML is :


XData DTL [ XMLNamespace = "http://www.intersystems.com/dtl" ]
{
<transform sourceClass='formation.RM.personne.Batch' targetClass='formation.msg.mysql.batch' create='new' language='objectscript' >
<foreach property='source.Records()' key='k1' >
<subtransform class='formation.transfo.personne' targetObj='target.list.(k1)' sourceObj='source.Records.(k1)' />
</foreach>
</transform>
}

Hi @Jimmy Christian 

another simpler sample :




XData DTL [ XMLNamespace = "http://www.intersystems.com/dtl" ]
{
<transform sourceClass='formation.RM.personne.Batch' targetClass='common.person' create='new' language='objectscript' >
<foreach property='source.Records()' key='k1' >
<assign value='source.Records.(k1).Nom' property='target.names.(k1)' action='set' />
</foreach>
</transform>
}