web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Copilot Studio / Unable to Access Initi...
Copilot Studio
Answered

Unable to Access Initialized Variables Inside a 'For Each' Loop in Copilot Studio

(0) ShareShare
ReportReport
Posted on by 9

Hello Power Platform Community,

I am encountering an issue in Copilot Studio where I cannot access variables (both global and non-global) from within a For Each loop. Here is the situation:

  • I have initialized a variable (DealersPayload) outside the loop, either as a global variable or a regular one.
  • When I try to reference this variable inside the For Each loop to dynamically update its value (e.g., concatenating JSON objects), I get an error indicating that the variable does not exist or is not recognized.

Here’s an example of my setup:

  1. Variable Initialization (outside the loop):
    - kind: SetVariable
      id: setVariable_DealersPayload
      variable: DealersPayload
      value: "[]"
     

  2. Inside the Loop:
    - kind: Foreach
      id: foreach_Top
      items: =Topic.Top5
      value: CurrentTopDealer
      actions:
        - kind: SetVariable
          id: setVariable_UpdatePayload
          variable: DealersPayload
          value: |
            JSON(
              Concat(
                DealersPayload,
                "[",
                If(
                  Length(DealersPayload) > 2,
                  ",",
                  ""
                ),
                "{",
                "\"Dealer_Code\": \"", Text(CurrentTopDealer.Value), "\",",
                "\"Score\": ", Text(CurrentTopDealer.Score), ",",
                "\"Category\": \"Top\"",
                "}",
                "]"
              )
            )

However, every time I try to use DealersPayload inside the loop, I get an error saying the variable is not recognized. This happens even when I declare DealersPayload as a global variable.

Questions:

  1. Why are variables initialized outside the loop not accessible from within the loop in Copilot Studio?
  2. Is there a specific way to pass or reference variables within a For Each loop?
  3. Are there any known limitations or workarounds for dynamically updating variables during loop iterations?

I would greatly appreciate any insights, workarounds, or guidance on how to resolve this issue.

Categories:
I have the same question (0)
  • Verified answer
    Artur Stepniak Profile Picture
    1,539 Super User 2025 Season 2 on at
    Hello,
     
    did you reference the variable by the dot notation, e.g. Topic.DealersPayload or Global.DealersPayload. Those objects contain all variables defined in the topic/globally.
     
    In case of any other questions, let me know. If the answer helped you, mark it, so that others can benefit from it.
     
    Best regards,
     
    Artur Stepniak
    Interested in GenAI? Visit my site!
  • sandeep_angara Profile Picture
    424 Super User 2025 Season 2 on at
    @A.Tara
     
    Would you be able to post a screenshot, please? 
     
     
  • A.Tara Profile Picture
    9 on at
    I used Global.DealersPayload and it worked!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Copilot Studio

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 251 Super User 2025 Season 2

#2
Romain The Low-Code Bearded Bear Profile Picture

Romain The Low-Code... 201 Super User 2025 Season 2

#3
S-Venkadesh Profile Picture

S-Venkadesh 93 Moderator

Last 30 days Overall leaderboard