Accessing the value of a Choice within code studio

In code studio, I have created a reference to a specific choice within a Choice data type (‘ref_choice_declined’). I now need to access the value of this choice in order to set a field to that value. However, I cannot find how to access the value. I have tried mats.variable and mats.config, but they can’t resolve my Choice reference.
Anyone know how to do this?

Hi Angela,

There are several ways in which you would achieve this, but it does all depend on how you are storing it, as above you mention both a variable and a choice value.

If you store this as a Variable and want to declared the variable within your code then you would assign the variable as follows within Code Studio :
var status_Decline = mats.variable(mats.ref(“ref_choice_declined”));

I am not sure what benefit you would get setting the variable by means of code studio, as I don’t think this is what you are trying to do.


If what you are trying to do is declare a value from a choice, then you could declare as follows:
var status_Decline = mats.ref(“ref_choice_declined”);

when setting this field value for a record, you would use something like:

_record.set(mats.ref(“Status”), mats.ref(‘ref_choice_declined’) )

There are other ways, but this would be hard coding and we need to try refrain from hard coding things, as snapshots to other environments and upgrades will have impact.

1 Like

Hi Ryan,
I am working with a Choice property in a data object.
Referring to it using mats.ref worked. I was initially unsure because it returned the ID of the Choice. But on inspection I realised that that was what was being stored in the field.
I would still be interested to know how I would access the Name or Displayable Name of a Choice, in case I should need to include it in a message to the user

Wrapping what you have declared with Displayable should allow you to see the Field Lable rather than the ID,

e.g. var status_Decline = mats.ref(“ref_choice_declined”), ‘displayable’ );

1 Like

you could have two Variables, one to store for use within your logic and a displayable version for outputting to a user

var status_DeclineValue = (mats.ref(“ref_choice_declined”), ‘displayable’ );
var status_DeclineID = mats.ref(“ref_choice_declined”);

Unfortunately, the result of mats.ref(‘ref_choice_declined’,’displayable’) still gave me the ID rather than the value

I can see some brackets are incorrect there, what Code studio event are you working on? i will take a look for you

The final answer, for getting the displayable value for a property with data type Choice is
Answer = sqrRecord.get(mats.ref(‘ref_choice_declined’,’displayable’)
Where sqrRecord has previously been set using mats.record.

Still don’t know how to access the displayable value of a Choice data type (as if you are looking it up in data store / data types, rather than looking up the value of a property in a data object) but happy to leave it here for now.