Expand my Community achievements bar.

SOLVED

JS activity: Array of objects to use in Alert activity

Avatar

Level 4

Hi all!

 

I am working on a JS activity that will fetch the mirror page of specific deliveries. I have now succesfully queried the given deliveries and stored them in a JS array containing objects. Now comes the part where I want to access these objects in a delivery, but I am not sure how to convert the array into meaningful data to reference from a delivery. 

I tried to simply do: vars.myArray = Array[object]. but this does not work, it seems like the "vars.xx" is not ablle to just convert the js variable. 

 

Here is my code so far: 

var deliveries = ["'play_win_v3_eml_1_a.a_'","'play_win_v3_eml_1_b.a_'","'play_win_v3_eml_1_c.a_'","'play_win_v3_eml_1_d.a_'","'play_win_v3_eml_1_e.a_'","'play_win_v3_eml_1_f.a_'"];
var mirrorUrls = [];

for each (var del in deliveries){
  var query = xtk.queryDef.create(
    <queryDef schema="nms:broadLogRcp" operation="get" lineCount="1">
        <select>
            <node expr="@id"/>
            <node expr="@eventDate"/>
            <node expr="[delivery/@id]"/>
            <node expr="[delivery/@deliveryCode]"/>
        </select>
        <where>
           <condition expr={"[delivery/@deliveryCode]="+del}/>
        </where>
        <orderBy>
            <node expr="@eventDate" sortDesc="true"/>
        </orderBy>
    </queryDef>
    )

  var result = query.ExecuteQuery();

  var obj = 
  {
    "name": result.delivery.@deliveryCode,
    "eventDate": result.@eventDate,
    "id": result.@id,
    "url": nms.delivery.GetMirrorURL(result.delivery.@id, result.@id)
  };
  
  mirrorUrls.push(obj);
};

vars.mirrorUrlArray = mirrorUrls;

for each (var instance in mirrorUrls){
  logInfo("name: " + instance.name);
  logInfo("date: " + instance.eventDate);
  logInfo("url: " + instance.url);
}

 I need to somehow be able to fetch the value of each object in my delivery - How can I reach that goal?

Any help would be much appreciated!!

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hello @SorenDP ,

yes it is because you have created object from database results which are of   XML type, you would need to parse each to respective type eg.

var obj = 
  {
    "name": result.delivery.@deliveryCode.toString(),
    "eventDate": new Date(result.@eventDate.toString()),
    "id": parseInt(result.@id),
    "url": nms.delivery.GetMirrorURL(result.delivery.@id, result.@id).toString()
  };

Marcel

View solution in original post

7 Replies

Avatar

Level 4

So I can do something like this, but that is kind of a very bad and non-scalable nor flexible solution: 

vars.url1;
vars.url2; 
vars.url3; 
vars.url4; 
vars.url5; 
vars.url6;

vars.name1;
vars.name2; 
vars.name3; 
vars.name4; 
vars.name5; 
vars.name6;

var index = 1; 
for each (var item in mirrorUrls){
  switch (index){
  
  case 1:
    vars.url1 = item.url;
    vars.name1 = item.name;
    break;
  
  case 2: 
    vars.url2 = item.url;
    vars.name2 = item.name;
    break;
    
  case 3: 
    vars.url3 = item.url;
    vars.name3 = item.name;
    break;
    
  case 4: 
    vars.url4 = item.url;
    vars.name4 = item.name;
    break;
    
  case 5: 
    vars.url5 = item.url;
    vars.name5 = item.name;
    break;
    
  case 6: 
    vars.url6 = item.url;
    vars.name6 = item.name;
    break;
  }
  index++;
}

Avatar

Community Advisor

Hello @SorenDP

vars. only can store string values so you need to JSON.stringify the variable before saving it to the vars. variable and the use JSON.parse before using it as object in another activity. 

 

If you are save and read the variable  in within the  same activity you do not have to map mirrorUrls to vars. variable at all.

 

Marcel Szimonisz

MarTech Consultant
for more tips visit my blog
https://www.martechnotes.com/

Avatar

Level 4

Hi @Marcel_Szimonisz.  Thank you very much for taking your time to provide some feedback here! 

So i tried this: 

vars.mirrorUrlArray = JSON.stringify(mirrorUrls);

But that throws this error: 

"14/07/2022 15:53:19 JST-310000 Error while compiling script 'PWKF5147/js' line 36: String.prototype.toJSON called on incompatible XML."

 

Avatar

Correct answer by
Community Advisor

Hello @SorenDP ,

yes it is because you have created object from database results which are of   XML type, you would need to parse each to respective type eg.

var obj = 
  {
    "name": result.delivery.@deliveryCode.toString(),
    "eventDate": new Date(result.@eventDate.toString()),
    "id": parseInt(result.@id),
    "url": nms.delivery.GetMirrorURL(result.delivery.@id, result.@id).toString()
  };

Marcel

Avatar

Employee Advisor

Hello @SorenDP 
You could move this JS snippet to Alert activity
Hope that helps!

Avatar

Level 4

I did what you suggested here, which works fine for this use-case. Thank you!