Get Enumerations in JavaScript

alexanderh76698

04-08-2017

Hello everyone,

I currently try to get enumerations list in a javascript object.

I wasn't able to find a standarf function in the JS API of campaign, so I fetch all the values using xtkQueryDef.

The return of my query is this:

<enumValue-collection>   <enumValue label="4305615529813"/>   <enumValue label="4305615440668"/> </enumValue-collection>

When I try to parse the XML to get the child nodes (<enumValue>) that actually contain the values I need, I face the issue that it doesn't recognize the hyphen in the parent node. The error message tells me that "collection" isn't defined (it cuts the string at the hyphen).

04.08.2017 13:15:48    js    JST-310000 Fehler bei der Auswertung des Scripts 'rosImportProducts/js' Zeile 40: collection is not defined.

This is what I try:

for(var ean in availProductsRes.enumValue-collection)

  logInfo(availProductsRes.enumValue-collection[ean].@label);

Is there a better way of fetching an enumation list in JS?

Or is this issue (hypen in XML node name) already known at Adobe?

Best regards

Alex

Accepted Solutions (1)

Accepted Solutions (1)

Jean-Serge_Biro

MVP

07-08-2017

Hi again,

I have an easiest way, using the standard JS API 6.1, I don't know if you have the same version of AC.
For me, I have tested in a workflow and it works well.

1274089_pastedImage_0.png

So I recommend this way above, it avoids doing that way below (while working as well, it always better to make standard way):

loadLibrary("xtk:shared/nl.js");

loadLibrary("xtk:formbase.js");

var enumElt = getEnumValuesElt('nms:recipient', 'gender');

etc

Regards.
J-Serge

Answers (8)

Answers (8)

Jean-Serge_Biro

MVP

21-08-2017

Hello Alex,

Sorry for answering a bit late.

I am sure that I confused you a bit with my previous post, sorry in advance.
Actually, when you proceed inside a workflow JS activity, the collection is directly named as the schema parameter part without the space name.
It is in webApp and report that XML element is added with -collection.

Please may you execute this code in a Javascript activity in a workflow, you should get the right values of the itemized list given in parameter:

// itemized list (énumération in French) for a database enum (xtk:enum)

var enumId = "576797577"; // webAppNature itemized list (check your value in your own instance)

var queryXtkEnumValue = xtk.queryDef.create(

    <queryDef schema="xtk:enumValue" operation="select">

      <select>

        <node expr="@label"/>

      </select>

      <where>

        <condition expr={"[@enum-id]=" + enumId} />

      </where>

    </queryDef>)

try

{

  var resXtkEnumValue = queryXtkEnumValue.ExecuteQuery();

  logInfo("[WKF337] get the values enumValue of the itemized list id = " + enumId + " (xtk:enum)");

  logInfo("[WKF337] Count of values = " + resXtkEnumValue.child('enumValue').length());

}

catch(e)

{

  logWarning("[WKF337] Error in getting the values enumValue of the itemized list id=" + enumId + " (xtk:enum)");

  logError("[WKF337] Error number: " + e); // or use logWarning the treatment must go on despite this error

}

for each(var value in resXtkEnumValue.enumValue) // there is no['enumValue-collection'] node in workflow (as there are in webApp and report)

  logInfo("[WKF337] value = " + value.@label);

for each(var value in resXtkEnumValue.enumValue) // there is no['enumValue-collection'] node in workflow (as there are in webApp and report)

  logInfo("[WKF337] value = " + value.@label);

You should get this result in the log trace:

1283081_pastedImage_1.png

Moreover, if you don't need complex queries (or some specific queries as dynamic IN parameter), whatever being in workflow/webApp/report, you can use Query activity instead.

Best Regards.
Jean-Serge

Jean-Serge_Biro

MVP

07-08-2017

Hi alexandrh,

I have found the easiest way (instead doing your queryDef code) that I was talking about in my previous post:

See the function code getEnumValuesElt in either xtk:formbase.js or server/formbase.js below:

1273954_pastedImage_0.png

FYI, it is this function that it is called  by the webApp source code, in order to implement a list form value populated on an enumeration list (sys enum or dbEnum) with the right xpath value (in the Advanced tab settings).
For instance, with a test webApp:

1273956_pastedImage_2.png

Of course, it is not  webApp/report v5 but webApp v6 or workflow,  you have to declare the library formbase.js accordingly,  so in your workflow Javascript activity for a workflow.

Regards
Jean-Serge

Jean-Serge_Biro

MVP

07-08-2017

Hello alexanderh,

Regarding the minus sign in the result set name (-collection), it is a usual issue for every task using AC JS and context ctx manipulation (report, webApp, workflow), if not using the get type of queryDef (that doesn't generate "XXX-collection" name but just XXX name).

So, in order to manipulate XXX-collection name, you need to use the escaping syntax:

ctx['XXX-collection'] instead of ctx.XXX-collection

Alternative way: you may rename the result query context set by using this instruction after your queryDef:
if res is the name of the result of the queryDef, you write this:
res.setName("XXX")
... ( rename the child if needed)

ctx.XXX = res

See this concrete example in surveyProperties in nms:webapp :

  res.setName("origin")

  for each(var child in res.*)

    child.setName("webAppLog")

  ctx.origin = res

Depending on the situation I use one of this 2 methods.

Regarding the access to enumeration directly from AC JS API, I must have a look, I think I did it a couple of years, I don't remember how. Standard JS API documentation gives methods but for dbEnum (alias, etc) but not for sysEnum, but I am quite sure there are easiest way of queryDef.

Regards
J-Serge

tejashriw155148

22-11-2019

Hi Jean-Serge Biron

I'm using below code in Webapp javascript but not getting anything in log.

Please let me know how to display enum label instead of value.

enumQuery.PNG

I tried to change enumValue to schemaname as well still not getting anything in display.

Regards,

Tejashri

Jean-Serge_Biro

MVP

21-08-2017

Hi Alex,

Sorry again, the raw html editor did it wrong so I copy/paste directly as is the JS code:

// itemized list (énumération in French) for a database enum (xtk:enum)

var enumId = "576797577"; // webAppNature itemized list (check your value in your own instance)

var queryXtkEnumValue = xtk.queryDef.create(

    <queryDef schema="xtk:enumValue" operation="select">

      <select>

        <node expr="@label"/>

      </select>

      <where>

        <condition expr={"[@enum-id]=" + enumId} />

      </where>

    </queryDef>)

try

{

  var resXtkEnumValue = queryXtkEnumValue.ExecuteQuery();

  logInfo("[WKF337] get the values enumValue of the itemized list id = " + enumId + " (xtk:enum)");

  logInfo("[WKF337] Count of values = " + resXtkEnumValue.child('enumValue').length());

}

catch(e)

{

  logWarning("[WKF337] Error in getting the values enumValue of the itemized list id=" + enumId + " (xtk:enum)");

  logError("[WKF337] Error number: " + e); // or use logWarning the treatment must go on despite this error

}

for each(var value in resXtkEnumValue.enumValue) // there is no['enumValue-collection'] node in workflow (as there are in webApp and report)

  logInfo("[WKF337] value = " + value.@label);


Regards

J-Serge

alexanderh76698

17-08-2017

Hey again,

thanks for the quick answer.

This is what my queryDef looks like:

var availProductsQuery = xtk.queryDef.create( 

  <queryDef schema="xtk:enumValue" operation="select">   

    <select>     

      <node expr="@label"/>     

    </select>   

    <where>     

      <condition expr={"[@enum-id] = " + enumID}/>

    </where> 

  </queryDef>);

 

var availProductsRes = availProductsQuery.ExecuteQuery();

availProductsRes.setName("Products");

I will try to use the query object in front of my JS object - how do I access the values returned by the query object?

Thanks again!

Best

Alex

Jean-Serge_Biro

MVP

17-08-2017

Hi Alexandre,

What does your queryDef look like?

In the 1st post, it seems to be:

for(var ean in availProductsRes.enumValue-collection) 

     logInfo(availProductsRes.enumValue-collection[ean].@label);

so it should become:

either

for(var product in availProductsRes.Products) 

     logInfo(product.@label);

(the toString being automately done by the logInfo method)

but what about the @label missing?

Sorry if I misunderstand something of your queryDef result set.

Regarding the enumerations, sorry I mislead you, I thought you were talking of sys enum in schemas.
If it comes from DB list xtk:enum and not from schema, so you can do either using the queryDef or if it is in a workfklow, you can even use a standard query activity withtarget and filtering mapping schemas xtk:enumValue.
So you can manipulate the context result (usually something as temp:query but you can give other name) as usual.

1280987_pastedImage_2.png

1280988_pastedImage_3.png

Regards
Jean-Serge

alexanderh76698

17-08-2017

Hey Jean-Serge,

I tried all your suggestions but I didn't find the right solution by now.

I currently encounter two problems:

1. Renaming the XML:

Renaming works just fine. But what happens when I try to loop through the renamed XML is acutally nothing.

I tried to rename "enumValue-collection" to "Products" and loop it afterwards. Just like this:

for each(var product in availProductsRes.Products)

  logInfo(product.toString());

It doesn't show anything in the logs.

Even if I try it in a different syntax (availProductsRes["Products"]) it doesn't work.

2. Trying to get enums by function

The main problem with this solution is, that I don't use the enums in a Schema or form. It's just a list. So I can't add the parameter for the schema it's used in.

Do you have any other solutions for me?

Thank you very much for your help so far!

Best regards

Alexander