Have an asset id as string need to convert to keyword


(paul walker) #1

Hi Folks
I understand that %asset_assetid^as_asset:asset_name% returns the name but, if I have the asset as a string id how would I achieve the same

For example what I am trying to achieve

“Parent name - Child name”

Below is the code I am using to try this
|||
SCRIPT
const assets = %asset_metadata_psccapabilities^json_decode^as_asset:asset_name,asset_url,asset_assetid,asset_parent%;

if(assets.length > 0){
    document.write('<ul>')
    assets.forEach(function(asset){
    document.write('<li>'+asset.asset_parent+' - '+asset.asset_name+ '</li>');
    });

document.write(’</ul’);
}
/SCRIPT
|||

thank you for you time
pw


(John gill) #2

Short but incomplete answer - %globals_asset_name:12345%


It looks like that JS is running in the browser, at which point it’s too late to fetch any fresh data that isn’t already available to your JS in assets.

I think you’d need to move the html generation code to SSJS, which would allow you to follow the process:

  • Get data from %asset_metadata... keyword (including the ID of each parent)
  • run SSJS over that data and spit out html which contains %globals_... keywords
  • Those globals keywords get processed before the html is sent to the browser.

It would end up looking something like

<script runat="server">
const assets = %asset_metadata_psccapabilities^json_decode^as_asset:asset_name,asset_url,asset_assetid,asset_parent%;

if(assets.length > 0){
    print('<ul>')
    assets.forEach(function(asset){
    print('<li>%globals_asset_name:'+asset.asset_parent+'% - '+asset.asset_name+ '</li>');
    });

print('</ul');
}
</script>

The key thing is that you write SSJS that spits out

<li>%globals_asset_name:12345% - Child name</li>

and rely on the final globals keyword processing after your code runs.


(paul walker) #3

Thank you so much - problem solved