CASE: migrate data from one collection to another with the rollforward function (copy details)

What if we have a situation where we create a reconciliation template in which the custom collection was incorrectly linked to the period drop, like this :

{% fori overeenstemming in period.custom.overeenstemmingen %}
  {% input overeenstemming.bedrijfsleider %}
{% endfori %}

As you know, custom drops never can be linked to the period drop - doing so will result in a malfunction of copying data from one period to another (there simply will be no data copied, all because the custom drop is linked to the period drop).

Now, as data has already been entered in collection A ( = period.custom.overeenstemmingen ) you cannot simply change your code into this:

{% fori overeenstemming in custom.overeenstemmingen %}
  {% input overeenstemming.bedrijfsleider %}
{% endfori %}

Doing that, would mean users won’t see the data they entered previously (the data is not gone mind you, simply not visible anymore due to the fact the collection changed from period.custom.overeenstemmingen to custom.overeenstemmingen, which are 2 different collections!).

There’s a workaround for that however!

We can use the rollforward function (explained here) in which we go through the collection A and copy all data from that to collection B.

We can do this in a new reconciliation template you can create, like this:

{% comment %}
we loop over the original collection which is (incorrectly) linked to the period drop ( = collection A called period.custom.overeenstemmingen )
in each loop, we will rollforward the data of each object to the new object from the collection B ( = custom.overeenstemmingen) which can be done by forloop.index0 followed by the name of the object
{% endcomment %}
{% fori overeenstemming in period.custom.overeenstemmingen %}
  {% input overeenstemming.bedrijfsleider %}
  {% rollforward overeenstemming.bedrijfsleider custom.overeenstemmingen[forloop.index0].bedrijfsleider %}
{% endfori %}


* * *

{% comment %}test new collection B{% endcomment %}
{% for item in custom.overeenstemmingen %}
  {{ forloop.index0 }}{{ item.bedrijfsleider }}
{% endfor %}

So with the forloop.index0 we go loop by loop in each individual object (which is the .bedrijfsleider part).

A migration done by Liquid code; how cool is that :sunglasses:

1 Like