Diving a little deeper into Document Generation

  • 17 December 2015
  • 8 replies
  • 14 views

Userlevel 7
Badge +11

Well hopefully you have already had a quick look at It's here... Nintex Document Generation and had a play around with the Doc Gen action in Office 365. With this post I'm going to go a little deeper and try to explain how to get repeating section data from a Nintex Form into a table in a document. Kok Koon Gan​ just posted a great post as well detailing Using Document Generation to create Excel Report from List Items​.

 

Continuing on from It's here... Nintex Document Generation  I'm going to use the same example and template, but just change it up, just a little.

 

The thing you have to get in your head early in this process is that to have a table in a document you have to build up a collection of data that can be inserted in.. Now for those of you that have worked with Collections, you'll know that it is just an array.. Eg One, Two, Three, Four etc. So knowing this, how do you get multiple columns into a table ? We have dictionaries, these are close to what we need, but we can't use the Query XML action and put the data from a repeating section into a dictionary in a meaningful way as we cannot specify what the key is.. What we actually need to do is may sounds a little confusing, but once you have done once or twice it will make a bit more sense..

 

Now if you haven't done this already, connect your repeating section inside your form to a Multiline text field (Plain). This can be a hidden filed in the content type if you wish.

152480_pastedImage_0.png

 

Here you can see the fields I have in my repeating section:

152481_pastedImage_5.pngAnd then populated.154788_pastedImage_10.png

 

In a XML world it looks a bit like this :

 

<?xml version="1.0" encoding="utf-8"?><RepeaterData><Version /><Items><Item><ItemId type="System.String">8675309</ItemId><ItemRating type="System.String">Bad</ItemRating><ItemNotes type="System.String">Graffiti on wall.</ItemNotes></Item><Item><ItemId type="System.String">6060842</ItemId><ItemRating type="System.String">Bad</ItemRating><ItemNotes type="System.String">Intermittent disconnections.</ItemNotes></Item></Items></RepeaterData>

 

Using the "Query XML" action and a little XPath, we can pull the values out of the XML like this:

 

152483_pastedImage_0.pngNotice the XPath string :

 

/RepeaterData/Items/Item/ItemId

 

What this will do is pull all the ID values into a Collection called 'IdCol'. Now you have to do the same for the other values you wish to place in your table. For this example that means I need a collection for the ID, Rating and Notes.

 

Once I have those collections, I need to put these into a dictionary.. WHY?? you ask.. Well, this allows me to form up the data with a 'Key' for which I need when trying to reference specific data in my document.

 

To picture this we have this in Collections Variables:

IdCol = 8675309;6060842

RatingCol = Bad;Bad

NotesCol = Graffiti on wall.;Intermittent disconnections.

 

Now we need to put this information in a dictionary as so we can assign keys,, So we need to put the collections through "For Each" loop. I'm assuming you know how this works here.. So in simplistic terms, the loop will cycle through a collection variable pulling the values out and putting them into a Text Variable, and keep running until there are no values left. Using this with an Index, we can grab other values out of other collections to build out our related data.

 

153599_pastedImage_6.png

As you can see in the above graphic we also have a Build Dictionary action.

 

153600_pastedImage_12.png

 

With the "Build Dictionary" action I'm grabbing the Single Line Text values from the collections and putting them in the dictionary but associated with a key. So after my loop is finished I will have a dictionary variable with the following value.

 

ItemsFinal (Dictionary Variable) = {"Id":"6060842","Rating":"Bad","Notes":"Intermittent disconnections."}

 

Then by adding an "Add item to Collection" action to the loop I can grab my newly formed dictionary value and place it back into a collection, looping on itself thus adding all the new individual dictionary values to the collection with a delimiter.. The big difference now is that I have a single collection that is nicely formed with all my collection data paired with a dictionary key.

 

FinalCollection (Collection Variable) = [{"Id":"8675309","Rating":"Bad","Notes":"Graffiti on wall."},{"Id":"6060842","Rating":"Bad","Notes":"Intermittent disconnections."}]

 

153601_pastedImage_0.pngOk so that was the hard bit.. , and I hope you haven't been confused by it all.. It took me a couple of tries and a few "please help" emails to get my head around it..

 

Now for the easy part.. Building out the document, and in our case the word doc.

 

Opening up the Document Generation action as per It's here... Nintex Document Generation​, for a repeating table of info the first thing we need to do is set the magic tag to tell the table that there is more coming happy.png

 

154650_pastedImage_1.pngIn the table it looks a little like this

154651_pastedImage_6.pngWe then need to go through and add our other tags to the table, and this is where the Keys come in.

 

154665_pastedImage_12.png

 

Using the Keys from earlier, build out the rest of the table

154670_pastedImage_0.pngWith the table built out, save the template and go run that workflow..

154787_pastedImage_5.png

 

That's all there is to it .. Until next time, happy nintexting happy.png


8 replies

Userlevel 4
Badge +8

Thanks Dan Stoll for posting this!  I just created my first sample workflow using Document Generation with data from a repeating section.  This article was very helpful for that.  Much appreciated and Happy New Year!

Badge +6

Awesome Dan Stoll​. I was just trying to reinvent the wheel!

Cheers mate!

In this step:

"What this will do is pull all the ID values into a Collection called 'IdCol'. Now you have to do the same for the other values you wish to place in your table. For this example that means I need a collection for the ID, Rating and Notes."

You may get back collections with different sizes if the fields in the repeating section are not required. Running a For Each over IdCol, and using the same index to access values in the other collections, may get you out of range errors. 

The better solution is to query XML the repeater field with XPath //Items/Item/, return the outer XML, then do a For Each over the items and query /Item/ID/ (and /Item/Rating/ and /Item/Notes/). In this case, if the field is empty the query will return an empty string which is valid and won't crash the workflow. Here is what it looks like. 

Badge +3

Great insights. Any ideas can doc gen populate images instead of test into tables?

Userlevel 7
Badge +11

Hey Nicolas, 

The current Doc Gen action in O365 cannot do images, however it will be updated very soon to be the same as the the new one that released on-prem a month or so ago.. This can allows you to do images, multiple templates and merges, and dynamically selected template and is a lot easier to build tables. Check out mine and Rick De Marco‌'s InspireX presentation here InspireX 2017 

 - This post is for on-prem but will be the same action (as seen in the video above) in O365 very soon. 

Badge +3

Any idea whether Macro Enabled Templates will be allowed/included in the upcoming release?

Userlevel 3
Badge +8

Hi Nicholas, Yes this will be possible in the upcoming release using the image URL data type when configuring your table.. 

Userlevel 3
Badge +8

Unfortunately we currently don't support macro enabled templates however I'd like to understand your use case if you'd like to share? Otherwise you can always email me directly.. Cheers .. 

Reply