Change record field value/content

Hello,

I have a detail table with a detail record called “color”. I want to count how many times every color is found in the whole detail table. In my example there are only the colors “brown” and “blue”. My problem is, I don’t know how to change the record field value/content.

I tried the following way:

  1. I create two record fields (brown and blue) in the datamapper (Mode: Javascript; Expression: 0; Type: Integer; Data format: Treat empty as 0).
  2. I create a script in the design template to count how many times single colors could be found. Sadly it don’t work.

brown = 0;
blue = 0;

for(var i in record.tables.detail){
if(record.tables.detail[i].fields.color == “brown”){
brown++;
}
else if(record.tables.detail[i].fields.color == “blue”){
blue++;
}
}

record.fields.brown = varBrown;
record.fields.blue = varBlue ;

So I tried to output the results of varBlue and varBrown in html.

var test = varBrown+ " + " + varBlue ;

results.html(test);

That works fine.

How can I change the record field content/value?

Regards,
Thomas

You can’t change the record values while you’re in the Template. Instead, you should do the same thing you’re doing, but at the DataMapper level. We want to maintain a clear separation of processes where, basically, the DataMapper writes the data and the Template reads it. Remember that the same data could potentially be used by other templates which might not expect the records to change after the initial data mapping operation has completed (which is what your Template is trying to do).

Thanks for your answer. I tried it around 100 times before in the datamapper without any success. But now it works.