Use a variable for a dropdown in Prompt for User Input?

I am using a “Prompt for User Input” action as shown in the screenshot. Is it possible to use a variable “CustomerList” to create a dropdown for the “ChosenCustomer…” field instead of typing/pasting the whole list into the field? There are 1000+ Customers and I would like an easy way to maintain the list in a separate file instead of manually updating the macro to reflect changes to the list.

I think you made a mistake. You need to insert a vertical bar before your variable. Like I did in this quick test.

I think this is properly explained in the documentation for the Prompt action....

If you want to provide a list in a variable, then you do so by starting the field with a bar (“|”), followed by your list (or your default value and then your list) in a variable (which contains choices separated by | ), for example “|%Variable%Current Choice%|%Variable%Choice List%”.

2 Likes

Thanks a lot. That works perfectly.

If you're reading in "Custom list" every time (and not using that variable in other macros without reading in the file) you really should make it a Local variable so KM isn't wasting time, memory and disk space persistently storing those 1000+ customer details.

There's more info about variable type, scope and persistence in the KM Manual.

2 Likes

This screenshot attached is from another macro but it deals with Prompt for User Input

I am using the format "0.0037__Bond" so that "Bond" is shown in the list but it gives the value of 0.0037. Is there a way to also use the "Bond" in a different variable? I want to use the value to do a calculation but I also want to use the description "Bond" in the output.


Easiest way is to double-up:

"0.0037_Bond__Bond|0.0044_24lb / 60lb__24lb / 60lb|etc"

So the list shows "Bond" but the returned value would be 0.0037_Bond. You can then extract the parts using either the pseudoarray method:

or the regex ([^_]+)_([^_]+):

And I'll add my usual suggestion that Type be a Local, rather than Global, variable unless you need it to persist...

If this is a long list you'll find it a lot easier to maintain if you put it into a text action at the beginning of the macro. You could even simplify it and then build your formatted list at execution time. Demo using tab-delimited text:

Paper List Demo.kmmacros (5.6 KB)

Image

I don’t understand the formulas in that macro but it works nonetheless. I am wondering if I can use the last entered Type as the default? Would I need to make it a global variable then and how would I place it in the default field before the “|”?

Do you mean the one inside the "For Each" that builds the list? That's using pseudo arrays again.

%Variable%Local_paper[1]\t%_%Variable%Local_paper[2]\t%__%Variable%Local_paper[2]\t%|

Local_paper[1]\t is "the first element of the contents of Local_paper when we split the text on tab characters (\t)". And so on.

So, first loop of the "For Each", Local_paper contains the first line of our list:

0.0037<tab_character>Bond

The first element when spilt on \t is 0.0037 and the second element is Bond. We combine the elements, along with the appropriate underscores and a pipe, to get the format we want for the "Prompt for User Input" list item:

<element1><underscore><element2><underscore><underscore><element2><pipe>

...to get:

0.0037_Bond__Bond|

The next loop does the same with the next line, appending the result to the variable:

0.0037_Bond__Bond|0.0044_24lb / 60lb__24lb / 60lb|

...and so on.

So we'll end up with a trailing | character when we finish, which means an ugly empty line in our prompt -- the "Substring" action simply deletes that last character.

Yep -- you're bang on. So you'd go back to the variable Global_Type (or just Type if you prefer) and put that variable as the first item in your "Prompt with List" text field. Again, you'll want to process the variable to get your "friendly list" format -- you can do that in the text field:

..where the field text is:

|%Variable%Global_Type%__%Variable%Global_Type[2]_%|%Variable%Local_list%