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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Issue with random numb...
Power Apps
Answered

Issue with random numbers generator

(0) ShareShare
ReportReport
Posted on by

Hi,

 

I created a button to generate random codes. But there is a issue.

When I generate a code ex. in for the locker 1 it gives the code 6228

2020-09-11 17_57_08-Microsoft Edge.png

If I create a code to the locker 2 it gives 1866

2020-09-11 17_58_21-Microsoft Edge.png

The issue is this, if I go back to edit something in the locker 1 it replaces the last number generated, in this case from the locker 2 and so on.

2020-09-11 17_58_59-Microsoft Edge.png

What can I do to avoid this?

Thanks in advance!

 

Extra Info

Button -  OnSelect: UpdateContext({VarRandValue:(RoundDown(9999 * Rand(), 0))})

 

DataCardValue TextInput - Default: If(
IsBlank(VarRandValue),
"",
Len(Text(VarRandValue)) = 1,
Concatenate(
"000",
Text(VarRandValue)
),
Len(Text(VarRandValue)) = 2,
Concatenate(
"00",
Text(VarRandValue)
),
Len(Text(VarRandValue)) = 3,
Concatenate(
"0",
Text(VarRandValue)
),
Text(VarRandValue)
)

 

Categories:
I have the same question (0)
  • RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @bsoares 

    I believe what you are saying is that you want the value that is submitted to be shown when you edit.

    If so, consider changing your Default formula in your TextInput control to the following:

    With({lclRnd: Text(VarRandValue)},
     Coalesce(Parent.Default, Concat(Sequence(4-Len(lclRnd)), "0") & lclRnd)
    )

    This will give you either the stored (Parent.Default) value or the random value prepended with zeros.

     

    I hope this is helpful for you.

  • mdevaney Profile Picture
    29,991 Moderator on at

    @bsoares 

    Your issue is occurring because locker 01 and locker 02 share a common variable that stores the random number.  Instead, you might prefer to use a collection so the numbers can be assigned to specific lockers.

     

    Use this code in the OnSelect property of the Code button.

    Collect(
     colLockerCodes,
     {
     Locker: TextInput_Locker.Text,
     Code: Text(RoundDown(9999 * Rand(), 0), "0000")
     }
    );

     

    Then insert this code into the Default property of the code Text Input

    LookUp(colLockerCodes, Locker=TextInput_Locker.Text, Code)

     

    ---
    Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up."

  • bsoares Profile Picture
    on at

    @mdevaney It works but... when I delete/reset the form for the locker or even delete the code manually the generate number comes up always. It doesn't generate a new code.

    Code.gif

  • bsoares Profile Picture
    on at

    @RandyHayes Forgot to mention, it works but as I replied to @mdevaney it doesn't generate new codes and the same code comes up after deleted the code.

  • RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @bsoares 

     

    Let's recap - What are your formulas for the Default and Update on the Datacard?  What is your formula on the Default for the DataCardValuex in that datacard?

  • bsoares Profile Picture
    on at

    @RandyHayes 

     

     

    Screenshot 2020-09-11 at 22.25.16.png

    Screenshot 2020-09-11 at 22.25.45.png

    Screenshot 2020-09-11 at 22.26.04.png

  • RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @bsoares 

    Please change your DataCardValue12 Default property to the following:

    With({lclRnd: Text(VarRandValue)},
     Coalesce(Parent.Default, Concat(Sequence(4-Len(lclRnd)), "0") & lclRnd)
    )

    And your Button1 OnSelect action to the following:

    UpdateContext({VarRandValue:(RoundDown(9999 * Rand(), 0))})

     

    And try again.

  • bsoares Profile Picture
    on at

    @RandyHayes  Tested and the same as I said before. Can't generate new codes and after delete the previous generated code comes up.

    Code.gif

  • RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @bsoares 

    So where are you submitting your form?  You are changing the value in Code, but I don't see that you are actually doing a SubmitForm

  • bsoares Profile Picture
    on at

    @RandyHayes Yes it is a form and it is connected to a Sharepoint List. With the first code I used it work fine generating new codes and clear the text. The only issue is that I explained in the my post .

    Let me know if you need more info. It would be great if it can be fixed.

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
Valantis Profile Picture

Valantis 414

#2
WarrenBelz Profile Picture

WarrenBelz 377 Most Valuable Professional

#3
Kalathiya Profile Picture

Kalathiya 315 Super User 2026 Season 1

Last 30 days Overall leaderboard