How do I pull the text value of a merge code option for use in a script?

0

I have a question with multiple checkboxes, including 5 that are populated from previous text questions. So, in the gui for that question, there are five options that have something similar in the Option field:

 

[question(“value”), id=”346″] 

 

In the script action, I separate a question into %%key and %%value and try to perform logic on the %%value.

 

foreach (%%allowed as %%key => %%value)
{
%%output .= sgapiPrint_R(%%key);
%%output .= sgapiPrint_R(” “);
%%output .= sgapiPrint_R(%%value);
%%output .= sgapiPrint_R(” “);
%%output .= sgapiPrint_R(sgapiStrLen(%%value));
%%output .= sgapiPrint_R(“<br>”);

if(sgapiStrLen(%%value) < 2)
{
sgapiRemoveOption(%%q,%%value);
}

}

 

On the outputs to the screen, it displays the value as I expect it, but the StrLen appears to return what the length of the option merge code is.

 

On the screen it shows “10111 MyTextHere 31” 

 

Using scripting, not js, how can I access the length of “MyTextHere”?

 

EDIT: Also, since this ends up impacting multiple questions, I do not want to run logic that explicitly calls out the key value.

Marybeth answered
    1

    Hey Sundog!

    I think you should be able to use sgapiEvaluateMergeCodes to get the length of “MyTextHere” rather than the length of ‘[question(“value”), id=”346″]’. 

    https://script.surveygizmo.com/help/article/link/sgapievaluatemergecodes

    My guess is that you are only seeing “MyTextHere” when you output because SG evaluates merge codes when the survey page is rendered. I think “%%value” is actually the merge code but it’s being converted on page load.  I would do something like this:

    foreach (%%allowed as %%key => %%value)
    {
    %%output .= sgapiPrint_R(%%key);
    %%output .= sgapiPrint_R(” “);
    %%output .= sgapiPrint_R(%%value);
    %%output .= sgapiPrint_R(” “);
    %%output .= sgapiPrint_R(sgapiStrLen(sgapiEvaluateMergeCodes(%%value)));
    %%output .= sgapiPrint_R(“<br>”);

    if(sgapiStrLen(sgapiEvaluateMergeCodes(%%value))< 2)
    {
    sgapiRemoveOption(%%q,%%value);
    }

    }

    Hope that helps!

    Marybeth answered
      0

      Hi,

      Have you looked into using this function? sgapiStrLen(%%string) 

      https://script.surveygizmo.com/help/article/link/sgapistrlen 

      Mary

      Sundog Admin DEV commented
        • That is already the function that I am using in the If statement and what displays in the OP. And while “sgapiPrint_R(%%value)” renders as “MyTextHere”… sgapiPrint_R(sgapiStrLen(%%value)); returns 31. 31, I believe, is the length of “[question(“value”), id=”346″] “. I removed the spaces at the end and ran again… 29. So… sgapiStrLen(%%value) returns a value based on the literal text in the option field, but sgapiPrint_R(%%value) returns “MyTextHere”. I need to access the length of the dynamic field, that in this instance is “MyTextHere”.

        ×

        Login

        Question stats

        • Active
        • Views646 times
        • Answers2 answers
        • Followers1 follower