Server Side Javascript conditional statement asset_type_code


(Lauren) #1

Matrix Version: 5.5.4.2

I am using server side javascript to generate our recipient emails. I would like to be able to output different code if the asset_type_code is form_question_type_file_upload. I have a conditional if/else but it does not work. Is there any obvious reasons why this will not work? Thanks

var assetIDs = %globals_asset_children_link_type_2:221324^as_asset:asset_children_link_type_2,asset_type_code^empty:[]%;
if (assetIDs.length > 0) {
  assetIDs.forEach(function(assetID) {
    if (assetID.asset_type_code === "form_section") {
      assetID.asset_children_link_type_2.forEach(function(asset) {
      	let typecode = `%globals_asset_type_code:${asset}%`;
      	var asset = asset.replace(':','_');
        if (typecode === "form_question_type_file_upload") {
        	print(`<p style="-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%;font-size:14px;line-height:25px;font-weight:400;mso-hide:% response_${asset}_file_size^empty:all%;display:% response_${asset}_file_size^empty:none%;">% question_name_${asset}%: % response_${asset}%</p>`);   
        } else {
        	print(`<p style="-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%;font-size:14px;line-height:25px;font-weight:400;mso-hide:% response_${asset}^empty:all%;display:% response_${asset}^empty:none%;">% question_name_${asset}%: % response_${asset}%</p>`);
        }
      });
    }
  });
}

(John gill) #2

You can’t evaluate keywords during SSJS execution. Picture your code going through three distinct phases:

  • Complete keywords are processed - in your example that would only be the one on the first line
  • Then the JavaScript is executed
  • Then the output of the SSJS print statements is processed for keywords

Any keyword results you want to do logic on, such as lines 6-8, must be completely evaluated before the SSJS runs. When your code runes line8, the value of typecode is “%globals_asset_type_code:12345%”, not the result of that keyword.

I’m not sure you can pull child and grandchild assets into a variable with a single keyword, you may need to use an asset listing.


Variables not resolving as expected in SSJS
(Lauren) #4

Thanks John,

I have been able to get this working but it may not be very elegant.

<script runat="server">
    var parent = %asset_metadata_id^as_asset:asset_children_link_type_2^as_asset:{asset_children^as_asset:asset_type_code,asset_assetid}^empty:[]%

    if (parent.length > 0) {
        parent.forEach(function(par) {
            if (Array.isArray(par)) {
                par.forEach(function(child) {
                    var childID = child.asset_assetid.replace(':','_');
                    if (child.asset_type_code.includes('form_question_type_file_upload')) {
                        print(`<p style="-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%;font-size:14px;line-height:25px;font-weight:400;mso-hide:% response_${childID}_file_size^empty:all%;display:% response_${childID}_file_size^empty:none%;">% question_name_${childID}%: % response_${childID}%</p>`);
                    } else if (child.asset_type_code.includes('form_question')) {
                        print(`<p style="-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%;font-size:14px;line-height:25px;font-weight:400;mso-hide:% response_${childID}^empty:all%;display:% response_${childID}^empty:none%;">% question_name_${childID}%: % response_${childID}%</p>`);
                    }
                });
            }
        });
    }
</script>

Thanks again for your help,
Lauren