cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
yjamous
Helper IV
Helper IV

Random Request Number

Hi

I'm trying to Generate a Random number to be used as application number. This application number depends on the category of application you choose from Drop Down.

If the request is of type "Application Training Request", then I'll add "APP" as prefix to the randomly generated variable.

If the request is of type "Service Support Request", then I'll add "SERV" as prefix to the randomly generated variable.

 

 

What I did so far:

 

1) Created a Variable and named it "VarRequestNumber" that gets initiated once screen loads

UpdateContext({VarRequestNumber:Text(Round(Rand()*10000,0),"[$-en]0000#")});

 

yjamous_4-1623433135487.png

 

2) I used the following for OnSelect and OnChange properties of the request type dropbox:

Switch(DataCardValueRequestType.Selected.Value,
"Application Training Request",Concatenate("APP","-",VarRequestNumber),
"Service Support Request",Concatenate("SERV","-",VarRequestNumber))

yjamous_3-1623433094375.png

3) For the Text Input, I used the put in the Default Value:

VarRequestNumber

 

 

yjamous_2-1623433039730.png

 

My Challenge: The text is not updating. It just display the initially generate Random number, the value doesn't change.

yjamous_6-1623433232207.png

 

What should I do to make the changes reflect as I change the request type?


Thanks in advance

 

1 ACCEPTED SOLUTION

Accepted Solutions
RandyHayes
Super User
Super User

@yjamous 

This is one of the drawbacks of repeating the same formula in several places.

 

Please consider the following.

- In your OnVisible action of the screen, add the following formula:

Set(varRequestBase, Text(Round(Rand() *10000,0), "00000"))

 

- In the OnChange action of DataCardValueRequestType add the following formula:

With({_rnd: Text(Round(Rand() *10000,0), "00000")},
    Set(varRequestNo,
       Switch(Left(Self.Selected.Value,3)
          "App", "APP" & "-" & _rnd,
          "Ser", "SERV" & "-" & _rnd
       )
    )
)

 

- In the Default property of the Text Input for the request number, set the following formula:

Coalesce(varRequestNo, Parent.Default, varRequestBase)

 

-In the OnSuccess action of your form, set the formula to:

Set(varRequestNo, Blank())

 

The assumptions are the following:

1) You are using an EditForm and the Request number is part of your form and is a field value from your underlying record (thus the addition of the Parent.Default to the formula so that you can actually edit and retrieve the submitted request number)

2) That changing the RequestType should always generate a new random number based on the request type.

 

If all the above are true, then this should give you what you want.

 

 

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

View solution in original post

10 REPLIES 10
CNT
Super User
Super User

@yjamous you have to use,

Set(VarRequestNumber, Concatenate("APP","-",VarRequestNumber)) 

RandyHayes
Super User
Super User

@yjamous 

Please consider changing your Formula to the following on the OnChange:

With({_rnd: Text(Round(Rand() *10000,0), "00000")},
    Set(varRequestNumber,
       Switch(Left(Self.Selected.Value,3)
          "App", "APP" & "-" & _rnd,
          "Ser", "SERV" & "-" & _rnd
       )
    )
)

 

You're not capturing the output of your formula in your original.

 

I hope this is helpful for you.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

Hi @CNT I tried to do the following:

I used the following for OnSelect and OnChange:

Switch(DataCardValueRequestType.Selected.Value,
"Application Training Request",Set(VarRequestNumber, Concatenate("APP","-",VarRequestNumber)),
"Service Support Request",Set(VarRequestNumber, Concatenate("SERV","-",VarRequestNumber)));

yjamous_1-1623444555900.png

 

 

 

And my text input is as shown below:

yjamous_0-1623444474538.png

 

Still the number doesn't change in the text input

@yjamous Ooops! one is a Context variable and the other is a Global variable (with the same name!!!)

Change the OnVisible to

Set(VarRequestNumber, Text(Round(Rand()*10000,0),"[$-en]0000#"));

 

Please remember to give a 👍 and accept the solution as it will help others in the future.

Ok

I did the following:

I set the OnVisible as you suggested:

yjamous_0-1623445736255.png

I changed OnSelect and OnChange for the dropdown and added a new variable:

yjamous_1-1623445870418.png

Text Input Property:

yjamous_3-1623446031333.png

 

Now, the text input doesn't change when I change the request type as shown below:

yjamous_2-1623445980311.png

 

It's frustrating 😞 

 

 

@yjamous Oh No! I only told you to change that Context variable in OnVisible to a global variable. Please do not change anything in the OnSelect. Now again we have 2 variables and the only difference is we have 2 global variables. We need only ONE variable. So please put the code back in the OnSelect as it was 25 minutes ago.

Now that I'm using one Variable it's not adding the prefix.

 

 

yjamous_0-1623447357110.png

 

@yjamous Could you please share the code you have now in the onVisible and on OnSelect as a code snippet (not picture).

RandyHayes
Super User
Super User

@yjamous 

This is one of the drawbacks of repeating the same formula in several places.

 

Please consider the following.

- In your OnVisible action of the screen, add the following formula:

Set(varRequestBase, Text(Round(Rand() *10000,0), "00000"))

 

- In the OnChange action of DataCardValueRequestType add the following formula:

With({_rnd: Text(Round(Rand() *10000,0), "00000")},
    Set(varRequestNo,
       Switch(Left(Self.Selected.Value,3)
          "App", "APP" & "-" & _rnd,
          "Ser", "SERV" & "-" & _rnd
       )
    )
)

 

- In the Default property of the Text Input for the request number, set the following formula:

Coalesce(varRequestNo, Parent.Default, varRequestBase)

 

-In the OnSuccess action of your form, set the formula to:

Set(varRequestNo, Blank())

 

The assumptions are the following:

1) You are using an EditForm and the Request number is part of your form and is a field value from your underlying record (thus the addition of the Parent.Default to the formula so that you can actually edit and retrieve the submitted request number)

2) That changing the RequestType should always generate a new random number based on the request type.

 

If all the above are true, then this should give you what you want.

 

 

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

Helpful resources

Top Solution Authors
Top Kudoed Authors
Users online (4,368)