Evaluating variables in globals keywords


(Chris) #1

Hi, I am having a problems using variables inside the globals keyword. I have adjusted the bottom example in the Server Side JavaScript page in the manual.

The problem is that the ‘%globals_asset_metadata_Studyarea:’ + asset + ‘%’ doesn’t evaluate out as a value at the same time as the straight %asset_metadata_Value%

When I view the page, the print command resolves both variables as the same text but the if condition fails.

When I view the page with the ?SQ_VIEW_SERVER_JS param it shows the following.

print(’

%globals_asset_metadata_Studyarea:’ + asset + ‘%’ + ’ — ’ + ‘Business and Law

’);

How can I get the globals with a variable asset number to evealuate at the same time as everything else so that I can compare them?

Thanks

<script runat="server">
    
    const assets = %globals_asset_children:792600^empty:[]%;
    var matches = [];
    
    //Only run if there are actually any assets in the array
    if(assets.length > 0){
        assets.forEach(function(asset){
            print('<p>%globals_asset_metadata_Studyarea:' + asset + '%' + ' --- ' + '%asset_metadata_Value%</p>');
            if('%globals_asset_metadata_Studyarea:' + asset + '%' == '%asset_metadata_Value%'){
                matches.push(asset);
            }
        });
    }
</script>

#2

I can’t test this on my version but could you put the metadata value into your array.

Something like
const assets = %globals_asset_children:792600^as_asset:asset_assetid,asset_metadata_Studyarea^empty:[]%


(Chris) #3

Thanks @bkelly, that has fixed the problem.

Lesson learnt, avoid unnecessary calls to globals by utilising modifiers.