I have a survey where there is a number of “audit” questions for a retail store visit. Each audit question have four potential answers:
- In Compliance
- Deviation
- Critical Deviation
- Exempt
There is also a comment box attached to each question that is not required.
What I want to do is the following via custom scripting:
- Display all comments on the final summary page (not the page post clicking the submit button, but just the last page of the survey) where the comment entered was rated a Deviation or Critical Deviation. I know how to access the answers via scripting but I can not find an ID number to reference the comment text box.
- I would like to set the requirement to enter a comment if the auditor answers the question as Deviation or Critical Deviation.
Does anyone know how to do either of these two processes.
Thanks in advance.
Best answer
Hi Jon,
I order to display the comments for questions rated “Deviation” or “Critical Deviation” I would grab the survey’s page map and iterate over it using a few different sgapi functions to access their values and comments if appropriate. For example…
// get the map of your survey
%%pagemap = sgapiPageMap();
// iterating over pages of the pagemap
foreach(%%pagemap as %%page_id => %%questions_on_page_arr){// iterating over question types
foreach(%%questions_on_page_arr as %%question_id => %%question_type){//if it’s a radio button
if(%%question_type == ‘RADIO’){// get the rating
%%val = sgapiGetValue(%%question_id);// if the rating was Deviation or Critical Deviation, get it’s comment
if(%%val == ‘Deviation’ || %%val == ‘Critical Deviation’) {// get the comment form runtime properties…
%%comment = sgapiGetQuestionRuntimeProperty(%%question_id, ‘Comment’);// right now %%comment is an obj, we need it as array, so cast it as one…
%%comment = (array) %%comment;// there is another nested obj, so cast it as an array too…
%%comment = (array) %%comment[’atoms’][0];// finally if there is a comment, get it and print it…
if(%%comment[’raw_value’] != ”) {
%%output .= sgapiPrint_R(%%comment[’raw_value’]);
%%output .= ‘<br/>’;
}
}
}
}
}
As for setting the comment fields to be required, you could potentially use sgapiSetQuestionRuntimeProperty. However, that would require a large amount of page jumping and headache so I would use a JS action instead. With JS you can evaluate DOM events and trigger errors dynamically.
Here is a rough start…
$(function(){
// add listener to page submit event
$(‘.sg-survey-form’).submit(function(e){// select all radio buttons and iterate over them
$(‘.sg-type-radio’).each(function(){// remove the error message if present
$(this).find(‘.sg-comment-title’).find(‘.sg-error-message’).remove();var value = $(this).find(‘input:checked’).attr(‘title’);
// if they answered Deviation or Critical Deviation run custom validation…
if(value == ‘Deviation’ || value == ‘Critical Deviation’) {// check to make sure there is a comment
var comment = $(this).find(‘textarea’).val();if(comment == ”) {
//if blank throw an error message
$(this).find(‘.sg-comment-title’).append(‘<div class=”sg-error-message sg-error-display”>This comment field is required.</div>’);// stop page from submitting
e.preventDefault();}
}});
});
});
The JS action would need to be on every page and it would target every question on that page that has is a radio button. To get around this (if you need to) you could add a unique class to all the questions you want targeted. I hope this helps!
i need help to modify this script to apply for continuous sum question to force the respondent if they put a value in field (others ) to specify others in the comment
I found this article that I had not seen before and I think it addresses the point about accessing the comments to pull a summary of them.
https://script.surveygizmo.com/help/article/link/comment-text
My question about trying to make the comment box required if one of the answers is selected but not the others is still unanswered.
There are over 140 questions to this retail store audit process which is why I am hoping that there is some type of question ID or option ID as it will be a lot of work trying to add all those question (even with the question copy function once the styling is done to the first one). I know your suggested solution will work, it is just not too practical in this case. It would also address my second point but again I hope I can find some type of ID that I can use in the custom scripting. Thanks for the response.
I am not an expert in script but you could make the comments actually another questions (essay type) and add logic to it. Then use the script to hide the title, script below and then use hidetitle in the css field. It will look like one question with logic
.hidetitle .sg-question-title {display:none;}