1
0
-1

Hi,

I'm trying to prefill some prefill data to a repeat block using a form load rule.

I can capture the prefill data in the rule, but can't seem to add it to the repeat instance(s).

The repeat instance count is set correctly based on the data.length - if there is 1 data object then one repeat instance is created and so on.

However, when I try to use the following code:

Util.addRepeatContent(data.content_1 , data.$accounts);

which is from this question - Can you dynamically set the number of repeats for a block in Maestro?

I get errors.

I have tried many combinations of data.content, "data.content", data.repeatContent and so on, but all fail.

The error is mostly "Parameter repeatItem must be a repeat".

I've used Chrome dev tools and tried many options, but can't crack this one.

Any ideas appreciated.

Thanks

Mark

    CommentAdd your comment...

    3 answers

    1.  
      1
      0
      -1

      Hi Mark,

      I cannot get Form.getRepeatData to work either, the code inserted by right clicking doesn't work. The most reliable way seems to be to double click on Content which produces data.contentA for me. Using Form.data.contentA works the same also, as you would expect. Both produce an array of objects representing the repeats. Please see screenshot. I think repeats may still be a work in progress as we have had several issues working with them, some as mentioned earlier are pending a product release. 

      This code does what you are trying to do. I hope it is useful.

      // get array of objects representing repeat data from repeat A
      var aRptData = data.contentA
      
      // copy that data into a new array of objects to inject into repeat B.
      // property names must match their respective field ids
      var bRptData = aRptData.map(function (entry) {
      	return {
      		name_1: entry.name,
      		color_1: entry.color
      	}
      })
      
      // get the item representing repeat B used in the following functions.
      var bRptItem = Form.getItemFromPath('data.contentB')
      
      // Set the size of the B repeat to match the number of repeat items in bRptData
      Form.setInstanceCount(bRptItem, data.contentB, bRptItem.length);
      
      // Apply the new repeat data to the B repeat.
      Util.addRepeatContent(bRptItem, bRptData);
      
      // Notes
      // I don't know why the second parameter data.contentB is necessary in Form.setInstanceCount yet
      // is not required in Util.addRepeatContent. It seems redundant.
      
      // In the process of creating this I renamed the repeats a number of times and always Accepted to update
      // the ids when prompted. When previewing the form I get a whole host of errors relating to previous repeat names.
      // Unable to find item data.content_1 : getItemProperty method. 
      // I've looked thru the json definition for the form and cannot find a reference to content_1 anywhere
      // Reloading the form in Maestro fixed this.
      
      // If you want to filter the source repeat items you could do this
      
      var bRptData = aRptData.filter(function (entry) { return entry.include == true }).map(function (entry) {
      	return {
      		name_1: entry.name,
      		color_1: entry.color
      	}
      })
      
      
      // where "include" is a checkbox in the source repeat.
      1. Matthew White

        Actually you can skip much of this code and assign directly to contentB


        data.contentB = data.contentA.map(function (entry) {
        	return {
        		name_1: entry.name,
        		color_1: entry.color
        	}
        })
        
        
        // This seems to be the most reliable way of working with repeats.
      2. Mark Murray

        Hi Matt,

        thanks for the map suggestion; that really helped. After a bit more research I managed to get filter and map to work as I wanted.

        There are some more complications, so I'll need to revisit this page I'm trying to create and change the way it works.

        But at least now I have something that I can work with.

        Thanks again

        Mark

      CommentAdd your comment...
    2.  
      1
      0
      -1

      Hi Mark,

      The first parameter is the item rather than the data model, eg:

      Form.setInstanceCount(Form.items.content_1, data.content_1, myArray.length);
      
      Util.addRepeatContent(Form.items.content_1  , myArray);

      There is currently an issue that has been fixed in Maestro 17.10.4 that the repeat needs to have the intended number of instances before addRepeatContent is called.

      1. Matthew White

        I don't see a 17.10.4 anywhere. Did you mean 18.05?

      2. Glenn Crook

        Hi Matt, 17.10.4 is a maintenance release that is being released at the same time as 18.05 that will be coming out in the next week.

      3. Matthew White

        I see a reference to 18.05 being rolled out yesterday but no mention of 17.10.4. Once the rollout completes will we see these both as upgrade options inside Maestro?

      4. Mark Murray

        Hi Glenn,

        thanks for the additional information.

        I managed to get this working; I can get the data populated into the repeat block.

        The next step is to get the selected accounts and pass them to another repeat block.

        I'm using the code below, but get an error, but the console.log displays an array with 3 elements, all 'undefined'.

        // data
        data.$selected = Form.getRepeatData("Form.data.content_1.repeatedContent", data) ;
        console.log(data.$selected);
        // set instances
        Form.setInstanceCount(Form.items.content_2, data.content_2, data.$selected.length);
        // set data
        Util.addRepeatContent(Form.items.content_2, data.$selected);


        The line to get the form data was generated using the right click helper menu.

        The API docs say that the data can be filtered - what is the filter expression? I could not find any examples.

        Thanks

        Mark

      5. Glenn Crook

        Hi Matt, once the update is rolled out you will see both 18.05 and 17.10.4 available in Maestro.

        Hi Mark, without seeing the structure of your form I cannot comment on a the error but it does not look like this will work because the array of JS objects passed to Util.addRepeatContent() needs to match with the field IDs. This will not work the way you are doing it because the fields in content_1 will have different IDs to those in content_2. It seems you will need an intermediate step to translate the data to populate the second repeat.

        Regarding the filter function, what we do is check if that parameter is a function. If it is then before returning the array we run Array.filter() on the array and return the filtered array, please see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter for details on how to use this.

      6. Mark Murray

        Hi Glenn,

        thanks for the explanation; that makes sense, unfortunately, as it means I need to do some more work.

        My next idea is to get the data from content_1 as JSON, and use that to drive a data driven radio button group.

        However, I have tried to use Util.jsonify, but get 'undefined' for the data.

        It seems that the code "Form.getRepeatData(blah) is not returning any meaningful data.

        But if I use dev tools I can see the object 'maestro.Form.data.content_1' is an array of three objects, which corresponds to the three items displayed in the repeat.

        Any ideas?

        Thanks

        Mark

      CommentAdd your comment...
    3.  
      1
      0
      -1

      Hi Mark,

      We had an issue with calling addRepeatContent which we raised with Avoka and is currently flagged as "Pending Version Release" which I think means it has not yet made it into Maestro, it may be affecting you. We encountered the issue when loading multiple records using the Dynamic Lookup Button. The workaround for us was to set an instanceCount first and then addRepeatContent would work. 

      Matt

      var rptItem = Form.getItemFromPath("data.content_1");
      
      
      // Without this line we get "TypeError: Cannot read property 'hasOwnProperty' of undefined"
      Form.setInstanceCount(rptItem, data.content_1, 5);
      
      
      var arr = [
        { name: 'Fred', type: 'Sparky' },
        { name: 'Bill', type: 'Bricky' },
        { name: 'John', type: 'Test' },
        { name: 'Shelly', type: 'Test1' },
        { name: 'Tim', type: 'Test2' },
      ];
      
      
      Util.addRepeatContent(rptItem, arr);
      1. Mark Murray

        Hi Matt,

        thanks for the suggestion.

        I tried that and got it working. I also got Glenn's suggestion above working. So, I'm making progress, but it seems a bit slow. I'll keep at it.


        Thanks

        Mark

      2. Matthew White

        I'm pretty sure we suggested the same thing (smile). I didn't see his answer when I answered.

      3. Glenn Crook

        I think Matt got his answer up 4 mins before I did but I did not refresh the page before posting my answer (smile)

      CommentAdd your comment...