Code example
The following example shows how data is retrieved from af collection of templates and used to create a collection of records (with values copied from the template).
String thisKeyToParentTemplate = "VALGTSKABELON"; //Entity
String templateSolutionName = "tasktemplate"; //Entity
ArrayList fieldsTemplate = new ArrayList(String [] {"TASK","DEADLINE","RESPONSIBLE"}); //list of fields
String templateKeyToParentTemplate = "SKABELON"; //Entity
String instanceSolutionName = "taskinstance";
String instanceKeyToParent = "LIST";
Now to the code ...
int parentTemplateDataID = Parser.getInteger( c.fields.getElementByFieldName(thisKeyToParentTemplate).FieldValue );
try (Session session = SessionFactory.getSession(this)) {
//Get data
SolutionQuery opsaetning = session.getSolutionQuery(templateSolutionName);
for(int i=0; i<fieldsTemplate.size(); i++ )
opsaetning.addSelectField(fieldsTemplate.get(i));
opsaetning.addWhereCriterion(templateKeyToParentTemplate, parentTemplateDataID);
SolutionQueryResultSet recordsToCopy = opsaetning.executeQuery();
int recordCount = recordsToCopy.size();
for( int i=0; i<recordCount; i++ ) {
SolutionRecordNew instance = session.getSolutionRecordNew(instanceSolutionName);
for(int fi=0; fi<fieldsTemplate.size(); fi++ ) {
String fieldNameToCopy = fieldsTemplate.get(fi);
String value = recordsToCopy.getRecordValue( i, fieldNameToCopy );
instance.setValue( fieldNameToCopy, value );
}
instance.setValueInteger( instanceKeyToParent, c.DataID );
if( ! Parser.isEmpty(instanceKeyToTemplate) )
instance.setValueInteger( instanceKeyToTemplate, recordsToCopy.getReference(i) );
instance.persistChanges();
}
} catch(Exception e) {
e.printStackTrace();
} catch (TsCloseObjectRequired ex) {
}