Use a power survey to get and post profile variables in Community. These scripts will only work with live responses; test responses will not return or write profile variables.
Get a member's profile variable value
Example: Use a profile variable to show or hide a question
Your power survey contains the following single choice profile question:
| Country |
|---|
|
However, you only want to show the question to members who do not
already have a profile variable value for
Country_PV
in the Community database. To do this, you would
create a script to retrieve the member value for
Country_PV
in the Community database:
ChoiceQuestion Sparq1Single = Country;
String Sparq3PVName = "Country_PV";
string Sparq3PVValue = GetProfileVariableText(Sparq3PVName);
foreach(Answer ans in Sparq1Single.Answers) {
if (ans.Text == Sparq3PVValue) {
ans.Selected = true;
break;
}
}
Save(Sparq1Single);
Then you create a condition to hide the question in your Power
Survey when there is already a value for
Country
. After you distribute your power survey, only the
members who do not have a profile variable value for "Country_PV" will see the
question.
| Profile Variable Type | Script template | Notes |
|---|---|---|
| Single Choice |
|
|
| Multiple Choice |
|
|
| Open End |
Set(Open_End_Get,
GetProfileVariableText("Sparq3PVName"));
|
|
| Numeric |
Set(Numeric_Get,
GetProfileVariableNumeric("Sparq3PVName"));
|
|
| Date |
Set(Date_Get,
GetProfileVariableDate("Sparq3PVName"));
|
|
Set a profile variable in a survey
Add the following scripts to translate answer options in single and multiple choice questions to the default language. You can use the script On Exit in the profile question, or in a separate Script action after the profile question.
- The Community profile variable must be in the Community's default language.
- The survey must be authored in the default language regardless of whether you distribute the survey in that language.
- Profile variable data will appear in the default language in your reports.
- The answer options in your power survey must match the options in your profile variable exactly.
- You cannot test the script in Test Mode. To ensure the script is working properly, perform a soft launch. For more information, see Editing and sending distribution emails.
| Profile variable type | Script template | Notes |
|---|---|---|
| Single Choice |
|
|
| Multiple Choice |
|
|
| Open End |
|
|
| Numeric |
|
|
| Date |
|
|
Example single choice profile variable
ChoiceQuestion Sparq1Single = Single_Choice_Set;
String Sparq3PVName = "SetSingleChoicePV";
SetProfileVariableText(Sparq3PVName, Sparq1Single.SelectedAnswer.Text1);
Example multiple choice profile variable
ChoiceQuestion Sparq1Multi = Multi_Choice_Set;
String Sparq3PVName = "SetMultiChoicePV";
ArrayList selAnswers = new ArrayList();
foreach(Answer ans in Sparq1Multi.GetSelectedAnswers())
selAnswers.Add(ans.Text1);
SetProfileVariableText(Sparq3PVName,
(string[])selAnswers.ToArray(typeof(string)));
Set a profile variable using recodes
Use the following scripts to set profile variables using recodes.
| Profile variable type | Script template | Notes |
|---|---|---|
| Single Choice Recode |
|
|
| Multiple Choice Recode |
|
|
When using a question to set the profile variable, paste the script into the On Exit tab of the script editor. The script must appear after the point in the survey where the recode conditions are met.
If you are unsure of where to place the script, you can add a script action to the end of your survey before the termination point.