• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit Search
0

Setting a required field based on a dropdown selection

Community Beginner ,
Dec 12, 2022 Dec 12, 2022

Copy link to clipboard

Copied

So I've seen a few similar topics but nothing I've been able to try/modify that fit what I need so hoping someone can help me fix this:

  • I have a dropdown menu with 7 options
  • 2 of the 7 options need to make a separate text field REQUIRED
  • If any of the other 5 options are selected the separate field text remains OPTIONAL
  • To try to simplify this I set the Export Values of the 2 required items to REQ and the remainder to X

 

Here's what I have so far:

var spod = this.getField("Dept");

if (event.value=="Req") {

spod.required = true;

}

else if (event.value=="X") {

spod.required = false;

}

 

but each time I select either of the req options the dept field doesnt change. What am I doing wrong?

TOPICS
JavaScript

Views

2.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 18, 2023 Jan 18, 2023

Use script in Dropdown field under 'Validate' tab → 'Run custom validation script'.

If the field name is "TARGET" then just replace "SpoDoM" with "TARGET":

var target = this.getField("TARGET");
target.required = event.value == "Req" ? true : false;
target.readonly = event.value == "Req" ? false : true;

 

If you still have trouble, please share your file.

Votes

Translate

Translate
Community Expert ,
Dec 12, 2022 Dec 12, 2022

Copy link to clipboard

Copied

Since you set export value you need to use script as calculation not validation.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 12, 2022 Dec 12, 2022

Copy link to clipboard

Copied

Thanks, scripts are pretty foreign to me, how would I go about that?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 12, 2022 Dec 12, 2022

Copy link to clipboard

Copied

Go to field properties, select 'Calculate' tab and put script under 'custom calculation script'. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 12, 2022 Dec 12, 2022

Copy link to clipboard

Copied

Thanks, it't still not working yet though so I've attached some screenshots to see if I'm missing something (its proprietary for a client so pardon the blackouts)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 12, 2022 Dec 12, 2022

Copy link to clipboard

Copied

Do you get any errors in console?

I assume you can't share an actual file, so try to recreate those fields in another form without confidential info and see if it will work, if it doesn't try sharing that file with us.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 18, 2023 Jan 18, 2023

Copy link to clipboard

Copied

Hi I don't get any errors but I made a stripped down version and attached it here. I tried to take out all fo the specifics but retain the general function. There are a 2 selections from the GENERAL field that are supposed to make the TARGET field a required field if selected (I gave them a value of REQ while everytrhing esle is a value of X)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 18, 2023 Jan 18, 2023

Copy link to clipboard

Copied

There is no field "TARGET" that field is named "SpoDoM" so replace it in script and it will work.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 18, 2023 Jan 18, 2023

Copy link to clipboard

Copied

Also that field is set to readonly, and since you set it to required you need to remove readonly, so instead use this script:

var target = this.getField("SpoDoM");
target.required = event.value == "Req" ? true : false;
target.readonly = event.value == "Req" ? false : true;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 18, 2023 Jan 18, 2023

Copy link to clipboard

Copied

sorry to clarify the field is titled SpoDoM but I changed it to target in my stripped out version. I didn't realie I missed that one so can you clarify where that was? Specifically where I should put that second code. I can add a revised version if that helps

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 18, 2023 Jan 18, 2023

Copy link to clipboard

Copied

Use script in Dropdown field under 'Validate' tab → 'Run custom validation script'.

If the field name is "TARGET" then just replace "SpoDoM" with "TARGET":

var target = this.getField("TARGET");
target.required = event.value == "Req" ? true : false;
target.readonly = event.value == "Req" ? false : true;

 

If you still have trouble, please share your file.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 19, 2023 Jan 19, 2023

Copy link to clipboard

Copied

Thanks, so does this look correct? I left the terms as they were intended for clarity (SpoDoM, not target). I took out the second script so I just have the one that you provided. Its not doing anything immediatly when I make a selection but maybe I'm not doing it right. Also I noticed that in both instances you had the Req but go from true : false to false : true. Should one of those be X or is there a reason they're the same? SInce I dont know js I'm trying to understand a bit better

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 19, 2023 Jan 19, 2023

Copy link to clipboard

Copied

Since you have export values set, move the script to 'Calculate' tab as 'Custom calculation script' and it will work.

They are not the same, one line is to set field to required and other is for readonly.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 19, 2023 Jan 19, 2023

Copy link to clipboard

Copied

You're a life saver - that worked! Thanks. So to better understand, I did put Req for the values I wanted to make teh SpoDoM active for but based on the script you gave me that turns off and on the field the 'X' wasnt needed? It's pretty much saying if its true that your value is Req then do this, but if your value isnt Req then do that. Is that about right?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 19, 2023 Jan 19, 2023

Copy link to clipboard

Copied

LATEST

Yes, if value is "Req" it will do 1st condition and if anything else it will do 2nd condition.

We can do it like that because you only had two export values "Req" and "X", if you had more values then we would have to use 'else if'.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines