Hi
I’m trying to find a way to use the detail records in an xml data as my output records and not just for a Detail table. So I need to find a way to produce a pdf page for each Detail record and not just for each record.
I think the way to archive this is in the Control Panel in Designer. I’ve tried some scripts but without success.
Is this possible using the Control Panel? And if not is there an alternative solution you could recommend?
Hi @Joe, Can you let us know please what these pages contains?
It is pretty easy to change the number of pages based on the number of records in a detail table but I don’t know what these pages need to contain. You can for example use a Control Script like the following to change the number of pages of a Print Context Section based on the number of records in a detail table:
Control Script
Name: Example
JavaScript code:
var detailTableCount = 0;
var printSection = merge.context.sections["sectionName"];
if ("detailTable" in record.tables) {
detailTableCount = record.tables["detailTable"].length;
}
if (detailTableCount > 0) {
printSection.minPages = detailTableCount;
}
It’s not so much the number of pages I need from the Detail records but a true or false result based on two of the Detail results matching the Print Section.
Below is a screen grab of my Template, I’ve highlighted the Detail records which are used.
I’m still learning Java Script so may have made an rooky mistake!
No still get the same result, it does work if I change the detail to[1] on line 2:
var itemSKUandName = record.tables.detail[1].fields.ProductSKU + ’ - ’ + record.tables.detail[1].fields.ProductName;
But only works if the Section matches the second detail record - detail[1]. I could in theory create a Control for each detail line but my data could have up to 100 Detail records in one top level record! Which isn’t workable.
please see example:
Have you already checked if the record fields “ProductSKU” and “ProductName” doesn’t contain a whitespace character at the beginning or at the end? You can check this for example by checking the result of the following logging in the Messsages pane of the Connect Designer application:
var productSKU = record.table["detail"][0].fields.ProductSKU;
var productName = record.table["detail"][0].fields.ProductName;
logger.warn("ProductSKU: '" + productSKU + "'");
logger.warn("ProductName: '" + productName + "'");
P.S. When the Messages pane isn’t visible please add it via ‘Window > Show View > Messages’ to the interface of the Connect Designer application.
Hi @Joe, Can you please share the ProductName and ProductSKU values of record.tables.detail[0]? I’m asking you this because the shared screenshots so far only contains the data of index 1 and 2 and because I am not able to reproduce the issue you’re facing when I run some tests via the version 2021.2.1 PReS Connect Designer application.
P.S. Please anonymise the data when it shows sensitive data.
Hi Marten,
As requested,
ProductSKU - ProductName are
COM101603 - Share your Story - A5 Postcard Jan 2019
COM101602 - Share your Story - Cover Letter Jan 2019
COM101838 - Bangtail updated 2021
COM101865 - Sponsor a child leaflet - June 21
COM101840 - Eco-friendly Pens
COM101705 - A3 generic church poster (single-sided)
Can you let me know please why you would like to check the values of the fields “ProductName” and “ProductSKU” of one specific record in the detail table “detail”? The reason why I am asking this is because of the following situation:
Please note that currently you are disabling each Print Context Section by default and that you only enable a Print Context Section when it’s name does match with the value of the variable “itemSKUandName”. And because you are checking only the name of a single record in the detail table “detail” this will have the shared situation as result.
Thanks for getting back to me, I’ve had some help from someone more efficient in JavaScript and managed to fix it so the if the detail ‘ProductName’ and ‘ProductSKU’ match the Print Context selection, turn on.
The Control script is:
var printSections = merge.template.contexts.PRINT.sections;
var itemSKUandName = record.tables.detail[0].fields.ProductSKU + ’ - ’ + record.tables.detail[0].fields.ProductName;
for (var a = 0; a < printSections.length; a++) {
var section = printSections[a];
section.enabled = false;
}
for (var jbt = 0; jbt < record.tables.detail.length; jbt++) {
for (var i = 0; i < printSections.length; i++) {
var currentsection = printSections[i];
pdn = record.tables.detail[jbt].fields.ProductSKU + ' - ' + record.tables.detail[jbt].fields.ProductName;
csn = currentsection.name;
if( pdn == csn ) {
currentsection.enabled = true;
}
}
The second part of the job is to Clone (duplicate) the page by the ‘Quantity’ value in the detail table for the Item. The below script was taken from a past Forum response which I’ve tried:
var ps = merge.context.sections;
for (var a = 0; a < record.tables.detail.length; a++){
for (z = 0; z < ps.length; z++ ){
if ( record.tables.detail[a].fields.ProductSKU + " - "+ record.tables.detail[a].fields.ProductName == ps[z].name){
var quantity = record.tables.detail[a].fields.Quantity;
for ( d = 0; d < quantity; d++){
results.parent().append(results.clone());
}
}
}
But this doesn’t give the correct number of pages. Could you let me know if you can see any issues with the above script. Or if you have any suggestions to Clone Pages on detail table values.
Please note that “record.tables.detail[a].fields.Quantity” returns a string instead of a number. This is because the data type of the record field “Quantity” has been set to “String”.
Please replace the following line of JavaScript code…
var quantity = record.tables.detail[a].fields.Quantity;
…with something like…
var quantity = 0;
var field = record.tables.detail[a].fields.Quantity;
if (field !== "") {
quantity = parseInt(field);
}
…to solve this in the current situation.
The most important part here is the “parseInt(string)” method which will convert a string to a integer.
Please let me know if implementing the mentioned change will result in the expected result.