Good Day
I have a weird issue where my PrintTask outputs are missing legend, sometimes, but not others, and I can't find a pattern as to why. I have a unique value render setting with ~160 different values in it, and on most of the maps I use a valueExpression to set the renderer.
If I take two maps which use the same valueExpression, literally the same one, and compare them, one will generate with legends, and the other won't.
Map 1:

Map 2:

The valueExpression: When($feature.aType == 3 && $feature.ci > -10 && $feature.ci <= 40, 'condition_3_Crititcal',$feature.aType == 3 && $feature.ci > 40 && $feature.ci <= 50, 'condition_3_Poor',$feature.aType == 3 && $feature.ci > 50 && $feature.ci <= 65, 'condition_3_Fair',$feature.aType == 3 && $feature.ci > 65 && $feature.ci <= 90, 'condition_3_Good',$feature.aType == 3 && $feature.ci > 90 && $feature.ci <= 99, 'condition_3_Excellent',$feature.aType == 3 && $feature.ci > 99 && $feature.ci <= 100, 'condition_3_Perfect',$feature.aType == 3 && $feature.ci > -10 && $feature.ci <= 0, 'condition_3_Not Defined','condition_Not Defined')
That is the valueExpression shared by both maps, the first will generate a legend, the second won't. This is my print function:
public printMap(): void {
const titleText = 'Map: ';
/* let layerLegend = new LegendLayer();
let layerIds = [];
this._view.map.layers.forEach((layer) => {
layerIds.push(layer.id);
})
layerLegend.layerId = "layer1";
layerLegend.subLayerIds = layerIds;
legendLayers: [layerLegend],
*/
const template = new PrintTemplate({
format: 'pdf',
layout: 'a4-landscape',
layoutOptions: {
authorText: 'Blah',
titleText
},
});
const params = new PrintParameters({
view: this._view,
template,
});
//
// NOTE: Start the print progress spinner
//
this.printProgress = true;
const printURL = 'https://utility.arcgisonline.com/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export%20Web%20Map%20Task';
PrintTask.execute(printURL, params).then((result) => {
//
// NOTE: Stop the spinner and open the print output
// into a new tab.
//
this.printProgress = false;
this.forceChange();
window.open(result.url);
}, () => {
this.alert.errorDialog('Print Error', 'Please try again or contact support!');
this.printProgress = false;
this.forceChange();
});
}
Is there any reason for this behaviour? I'm I missing something in the PrintTemplate or PrintParameters?
On a strange note, I don't use a valueExpression, I always get a legend on the print output:

I suspect the problem has to do with the use of the valueExpression, so is there a way to pass my legend / layer information directly to the print task?
Thanks