Including Variable Fields in Exports
If your application includes conditional questions, it may be useful to build exports that include data from different fields based on these conditions. For example, you may need applicants who graduated from high school to answer one set of questions, and applicants who earned a GED to answer another. Then you may want to include the responses to the appropriate questions in a variable field in your export. In this example, whichever set of questions the applicant responded to will be included in this variable field.
In this article, you'll learn to use JavaScript to create a conditional export that includes different fields based on the conditions you indicate. This allows you to have a field in your export that can display answers from different questions, depending on what conditions are met. To build this, you'll need to:
- Identify your variable questions
- Determine which fields need to be included in your export
- Create a custom property that recognizes these conditions and pulls in data from the appropriate fields
- Include the custom property in your export
Building the Custom Property
For this step, you'll build a custom property that evaluates which data to pull in. To set this up, you'll need to identify a "fork" question and your related variable questions. Like a "fork in the road", a fork question refers to a question on the application that determines which path of variable questions become relevant to the applicant. For example, a fork question could ask the applicant if they graduated from high school or obtained a GED. If they selected "high school," your variable questions might ask the location of the high school, graduation year, etc. If the applicant selected "GED," your variable question might ask what year the GED was obtained.
Add the JavaScript Code
Create a calculated custom property. Add the following code into the Expression window. Use the view source button to copy the code.
{ // index must be set when using data with rows (ex. test scores, high schools, colleges) let index = 0; // forkQuestion represents the question that determines which values will return for a specific applicant let forkQuestion = forms?.your_question?.your_field; // forkAnswer1 is the answer to forkQuestion that will lead you down path1, forkAnswer2 is the answer that will lead you to path2 let forkAnswer1 = "Option1"; let forkAnswer2 = "Option2" // data paths for what the respective forks should return, [index] must always be included after .rows let forkPath1 = forms?.fork_pathone?.fields_needed?.rows[index].data let forkPath2 = forms?.fork_pathtwo?.fields_needed?.rows[index] // locate the ID of the data you would like to return, one for each path let forkData1 = "dataOfYourChoice1" let forkData2 = "dataOfYourChoice2" if (forkQuestion == forkAnswer1){ return forkPath1[forkData1] } else if (forkQuestion == forkAnswer2){ return forkPath2[forkData2] } else { return ""; } }
Next, you'll need to replace the variables in the code with fields that are relevant to your desired result.
Replacing the Variables
The variables included in the code snippet above are as follows:
- let index = 0 - if the fields you are pulling data from are found in a table (e.g., test scores, high schools, or colleges), you can adjust this value to indicate which row from the table you are interested in. For example, in the case of test scores, let index = 0 will extract the first test score, let index = 1 will extract the second test score, and so on. When using data found in tables, knowing this value is necessary for the code to work.
- forms?.your_question?.your_field; - this is the field that determines which values are returned for each applicant. For example, if you want to return different values if an applicant has a high school diploma rather than a GED (as in the example above), this variable would correspond with the question that asks if the applicant whether they graduated high school (e.g., forms?.demouniversity?.cq_did_you_graduate_high_school_or_earn_an_equivalency)
- "Option1" - this refers to the answer option that will return the first set of values. In the example above, this could be "High School", which would return the applicant's responses to high school related questions.
- "Option2" - this refers to the answer option that will return a different set of values. In the example above, this could be "Equivalency", which would return the applicant's responses to the GED related questions.
- forms?.fork_pathone?.fields_needed?.rows[index].data - this refers to the data that you want returned if the applicant's response matches Option 1 above. If there are rows involved, this variable should include [index]. In the example above, this could be forms?.high_school_info.high_schools?.rows[index].data.
- forms?.fork_pathtwo?.fields_needed?.rows[index] - this refers to the data that you want returned if the applicant's response matches Option 2 above. If there are rows involved, this variable should include [index]. In the example above, this could be forms?.high_school_equivalency?.rows[index]
- "dataOfYourChoice1" - this refers to the specific data point that you'd like to pull from the Option 1 rows you've indicated above. In the example above, this could be "highSchoolName".
- "dataOfYourChoice2" - this refers to the specific data point you'd like to pull from the the Option 2 rows you've indicated above. In the example above, this could be "dateEarned".
Where necessary, use the Field Dictionary to determine the names (field keys) of the fields you want to use.
Setting Up the Export
To include the variable fields in an export, in the Available Fields area, find the custom property you created above and add it to the Columns window.
Alternatively, you could add a Calculated field in your export, and paste the code listed above into the Expression field.
Testing the Code
While building the export or your calculated property, click the Try It button to confirm that your variables are working.
Search for a record to complete your tests with. Confirm that the appropriate value appears in the Output window.