Use profile variables in a power survey

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.

Note: If you rename profile variables after the power survey is live, it will break the get and post functionality. You must update the scripts with the new profile variable names.

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
  • Canada
  • United States
  • Mexico

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.

Note: Place your GET script before the profile question you want to show or hide in your power survey.
Profile Variable Type Script template Notes
Single Choice

ChoiceQuestion Sparq1Single = Single_Choice_Get;
String Sparq3PVName = "GetSingleChoicePV";

string Sparq3PVValue = GetProfileVariableText(Sparq3PVName);
foreach(Answer ans in Sparq1Single.Answers) {
  if (ans.Text == Sparq3PVValue) {
    ans.Selected = true;
    break;
  }
}
Save(Sparq1Single);
  • Single_Choice_Get is the name of the Single Choice question in your power survey.
  • GetSingleChoicePV is the name of the Single Choice profile variable as it appears in Community.
Multiple Choice

ChoiceQuestion Sparq1Multi = Multi_Choice_Get;
String Sparq3PVName = "GetMultiChoicePV";
string[] Sparq3PVValue = GetProfileVariableMultiText(Sparq3PVName);
ClearAnswers(Sparq1Multi);
foreach(Answer ans in Sparq1Multi.Answers) {
if(Sparq3PVValue != null)

{ if (Array.IndexOf(Sparq3PVValue, ans.Text) >= 0) ans.Selected = true; }
}
Save(Sparq1Multi);
  • Multi_Choice_Get is the Multiple Choice name of the question in your power survey.
  • GetMultiChoicePV is the name of the Multiple Choice profile variable as it appears in Community.
Open End Set(Open_End_Get, GetProfileVariableText("Sparq3PVName"));
  • Open_End_Get is the name of the Open End question in your power survey
  • Sparq3PVName is the name of the Open End profile variable as it appears in Community.
Numeric Set(Numeric_Get, GetProfileVariableNumeric("Sparq3PVName"));
  • Numeric_Get is the name of the Numeric question in your power survey.
  • Sparq3PVName is the name of the Numeric profile variable as it appears in Community.
Date Set(Date_Get, GetProfileVariableDate("Sparq3PVName"));
  • Date_Get is the name of the Date question in your power survey.
  • Sparq3PVName is the name of the Date profile variable as it appears in Community.

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.

Note: For multilingual power surveys:
  • 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

ChoiceQuestion Sparq1Single = Single_Choice_Set;
String Sparq3PVName = "SetSingleChoicePV";

SetProfileVariableText(Sparq3PVName, Sparq1Single.SelectedAnswer.Text1);
  • Replace Single_Choice_Set with the name of the Single Choice question in your power survey.
  • Replace SetSingleChoicePV with the name of the Single Choice profile variable as it appears in Community.
Multiple Choice

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)));
  • Replace Multi_Choice_Set with the name of the Multiple Choice question in your power survey.
  • SetMultiChoicePV is the name of the Multiple Choice profile variable as it appears in Community.
Open End

SetProfileVariableText("Sparq3PVName", (String)Open_End_Set);
  • Replace Sparq3PVName with the name of the Open End profile variable as it appears in Community.
  • Replace Open_End_Set with the name of the Open End question as it appears in your power survey.
Numeric

SetProfileVariableNumeric("Sparq3PVName", Numeric_Set.IntValue);
  • Replace Sparq3PVName with the name of the Numeric profile variable as it appears in Community.
  • Replace Numeric_Set with the name of the question at it appears in your power survey.
Date

SetProfileVariableDate("Sparq3PVName", Date_Set.DateTimeValue);
  • Replace Sparq3PVName with the name of the Date profile variable as it appears in Community.
  • Replace Date_Set with the name of the date question in your power survey.
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
DynamicChoiceQuestion Sparq1Single2 = Age_RollUp_v2;
String Sparq3PVName2 = "PQ_AgeRollUp";

SetProfileVariableText(Sparq3PVName2, Sparq1Single2.SelectedAnswer.Text1);
  • Replace Age_RollUp_v2 with the name of the Single Choice Recode question in your power survey.
  • Replace PQ_AgeRollUp with the name of the Single Choice Recode profile variable as it appears in Community.
Multiple Choice Recode
DynamicChoiceQuestion Sparq1Multi = Kids_Age_RollUp;
String Sparq3PVName = "PQ_AgeRollUp_KidsAtHome";

ArrayList selAnswers = new ArrayList();
foreach(DynamicAnswer ans in Sparq1Multi.GetSelectedAnswers())
selAnswers.Add(ans.Text1);
SetProfileVariableText(Sparq3PVName,
(string[])selAnswers.ToArray(typeof(string)));
  • Replace Kids_Age_RollUp with the name of the Multiple Choice Recode question in your power survey.
  • PQ_AgeRollUp_KidsAtHome is the name of the Multiple Choice Recode profile variable as it appears in Community.

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.