Restricting records in Survey for Question with data type as Reference
Many a times we use reference data type in Surveys as questions. One limitation of using reference data type in Survey is about filtering the records from the reference table i.e. out of the box reference qualifier cannot be applied for reference data type question in Surveys.
Consider an example that you are having a survey with question with data type as reference and pointing to Incident (incident) table and you want to show only those incidents where logged in user is the caller of incident. So you can use the below solution/approach:
- Create a query business rule on incident table
- In the condition of the business rule ensure it checks for interactive session and doesn't run for admin users
- Restrict this query business rule to run only when someone opens incident table records from the survey page reference question and not from any other place in the native UI/instance.
- In this way this query business rule will filter and show only the required/filtered records on the survey page and it won't affect any other place.
Business Rule:
When: before
Table: Incident
Query: TRUE
Condition: gs.getSession().isInteractive()&& !gs.hasRole("admin")
Script:
(function executeRule(current, previous /*null when async*/) {
var url = gs.action.getGlideURI().getMap().get('sysparm_target');
if(url.indexOf('ASMTQUESTION') >= 0){
current.addQuery('caller_id', gs.getUserID());
}
})(current, previous);
Approach/Solution: Whenever user opens the question of type reference to select the incident; it open ups the URL and the URL contains the url parameter as sysparm_target with value as ASMTQUESTION:sys_id which indicates this incident list is being opened up from the survey page. This helps us in identifying from where the incident list is being opened up and apply the query business rule accordingly. You can try printing the URL in the logs and confirm the same. This business rule will only work when someone tries to open incident list from survey.
Note: This would work in Global and Custom scope as well. As of now this business rule would work or filter incident records for all the surveys which has Data Type as Reference and table as Incident.
Additional Note: In order to make this work only for the question of type reference for particular Survey; check the URL in the logs or check the URL when you open up the incident list from the question and update script as below as per the sys_id you are getting; I have added it as per my sys_id showing up in the URL
(function executeRule(current, previous /*null when async*/) {
var url = gs.action.getGlideURI().getMap().get('sysparm_target');
if(url == 'ASMTQUESTION:d89d7a414f598410fc11fa218110c77f'){
current.addQuery('caller_id', gs.getUserID());
}
})(current, previous);
Screenshots below:
Incidents where System Admin is caller: 237
Thanks for reading the blog and do provide your inputs/suggestions if any.
Hope you find this article helpful. Don’t forget to Mark it Helpful, Bookmark.Thanks,
Ankur Bawiskar
https://www.servicenow.com/community/developer-blog/restricting-records-in-survey-for-question-with-data-type-as/ba-p/2282037