cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How can I access the "loop" variable in my workflow task?

dt_martin
Dynatrace Enthusiast
Dynatrace Enthusiast

Hello team,

I have a workflow action where I want to loop over an array of locations (lat, long)
I named the item variable "locations" and the list would look something like this.
[{lat: 48.29226713, long: 14.29553878}]

How can I access these values in my HTTP Request?

EDIT: I have tried writing {{locations.lat}} but it doesn't seem to work this way.

EDIT_2: Figured out that my value for the list was malformed (invalid json). Using [{ "lat": 48.29226713, "long": 14.29553878}] works but still don't know who to read those values

9 REPLIES 9

michal_zawislak
Dynatrace Advisor
Dynatrace Advisor

Hi Martin,
To access loop variable with e.g. its name set to locations. You can access it like {{ _.locations.lat }} (mind the _. at the beginning)

2022-12-12 11_27_08-Clipboard.png

2022-12-12 11_34_10-Clipboard.png

Your AutomationEngine ally!

Hi , 
I want to do the same thing but in my case task is JS code. So How I can access this loop variable (added to my js code task) in my js code, how should be the syntax, please help.

I referred  above example, but got an error in js code.
console.log({{ _.localtions.lat }});

 

Also ,is there  a way to access the task loop counter inside task(JS code)??
Regards,
Heramb Sawant

 

Hi Heramb,

To access the loop item in the "Run Javascript" task, I'd suggest using automation-utils SDK as it would be the simplest and fastest way.

michal_zawislak_0-1692862045894.pngmichal_zawislak_1-1692862056328.pngmichal_zawislak_2-1692862060439.png

I hope this will help. If you have any more questions, don't hesitate to reach out.

Your AutomationEngine ally!

Hey this works for me. I am able to access this Loop item values. I also tried string instead of integer like,  item   ['USA', 'UK].

Now I have little more addition. If  I have item values like ,
item  [{Country : 'India', Code: '100'} ,{Country : 'USA', Code: '200'}]

How Can I access this Country and Code values in JS code?? I tried  like below but got an error
    console.log('Loop item' , actionExe.loopItem.Country);
    console.log('Loop item' , actionExe.loopItem.Code);



Glad it works!

Yes, you can also access object attributes. Your example is almost correct. There's a little thing missing: a reference to the "loop item".

console.log('Loop item' , actionExe.loopItem.item.Country);
^^^^^^

 

Your AutomationEngine ally!

Thanks , Now I am able to access Object attributes as well.

The loop list must be a valid Python syntax, for example

[{"Country": "India", "Code": "100"}, {"Country": "USA", "Code": "200"}]

 

Your AutomationEngine ally!

Yes I understood that now.

Hey I am not yet good with JS scripting 🙂  Can I write the same JS code(code to access item object attributes) in standard JS code user task ( please refer below image for the same)

heramb_sawant_0-1692952608862.png

 



Hey I am able to do that , I am good for now

heramb_sawant_0-1692953548113.png

 

Featured Posts