Custom 'Take Assessment' UI action
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2023 07:59 AM
Hello,
We are trying to improve our GRC assessment process by allowing users to take the assessment directly from the Risk.
The issue is there can be multiple respondants to the Risk and therefore multiple assessment instances. We have the code to determine the take assessment instance url based on the current user. We then want to redirect the user to that page.
We have tried the following 2 options.
1. Only using Server Side code
Outcome: Able to get the correct URL but does not redirect to the page
var sysID = current.sys_id.toString();
var userID = gs.getUserID().toString();
gs.addInfoMessage('Risk sysID: ' + sysID);
gs.addInfoMessage('Risk userID: ' + userID);
var gr = new GlideRecord('asmt_assessment_instance');
gr.addEncodedQuery('state=ready^user=' + userID + '^sn_grc_item=' + sysID);
gr.query();
if (gr.next()) {
var attSysID = gr.sys_id;
var link = new global.AssessmentUtils().getAssessmentInstanceURL(gr.sys_id);
var url = '' + link + '';
url += '&sysparm_view=default_view';
var urlLink = url.toString();
current.u_assessment_instance_link = urlLink;
current.update();
gs.addInfoMessage('Risk Link: ' + link);
gs.addInfoMessage('Risk URL: ' + url);
action.setRedirectURL(urlLink);
}
2. Server Side and Client Side
Outcome: The URL gets updated after the redirection, therefore the second time the user clicks the button, they are sent to the correct URL.
function getAssessment() {
var sysID = current.sys_id.toString();
var userID = gs.getUserID().toString();
gs.addInfoMessage('Risk sysID: ' + sysID);
gs.addInfoMessage('Risk userID: ' + userID);
var gr = new GlideRecord('asmt_assessment_instance');
gr.addEncodedQuery('state=ready^user=' + userID + '^sn_grc_item=' + sysID);
gr.query();
if (gr.next()) {
var attSysID = gr.sys_id;
var link = new global.AssessmentUtils().getAssessmentInstanceURL(gr.sys_id);
var url = '' + link + '';
url += '&sysparm_view=default_view';
var urlLink = url.toString();
current.u_assessment_instance_link = urlLink;
current.update();
gs.addInfoMessage('Risk Link: ' + link);
gs.addInfoMessage('Risk URL: ' + url);
action.setRedirectURL(atLink);
}
}
if (typeof window == 'undefined')
getAssessment();
function openAssessment() {
var atLink = g_form.getValue('u_assessment_instance_link');
g_navigation.openPopup(atLink);
gsftSubmit(null, g_form.getFormElement(), 'take_assessment');
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2023 11:00 AM
It seems that the issue is with the first option where only server-side code is used. The code is able to retrieve the correct URL, but the redirection to the page is not happening. This could be because the code is not being executed on the client-side.
In the second option, both server-side and client-side code is used. The server-side code retrieves the URL and updates a field on the current record, while the client-side code opens a popup window using the URL from the updated field. However, it seems that there is a typo in the client-side code where the URL variable is referred to as "atLink" instead of "urlLink". This could cause the redirection to fail on the first click but work on the second click when the field is updated with the correct URL.
To fix the issue, you can try the following changes:
- In the first option, add client-side code to the function to execute the redirection on the client-side:
function getAssessment() {
var sysID = current.sys_id.toString();
var userID = gs.getUserID().toString();
gs.addInfoMessage('Risk sysID: ' + sysID);
gs.addInfoMessage('Risk userID: ' + userID);
var gr = new GlideRecord('asmt_assessment_instance');
gr.addEncodedQuery('state=ready^user=' + userID + '^sn_grc_item=' + sysID);
gr.query();
if (gr.next()) {
var link = new global.AssessmentUtils().getAssessmentInstanceURL(gr.sys_id);
var url = '' + link + '';
url += '&sysparm_view=default_view';
var urlLink = url.toString();
current.u_assessment_instance_link = urlLink;
current.update();
gs.addInfoMessage('Risk Link: ' + link);
gs.addInfoMessage('Risk URL: ' + url);
// Redirect to the assessment instance URL on the client-side
var redirectURL = 'window.location.href = "' + urlLink + '"';
gs.getSession().redirect(redirectURL);
}
}
- In the second option, fix the typo in the client-side code:
function openAssessment() {
var urlLink = g_form.getValue('u_assessment_instance_link');
g_navigation.openPopup(urlLink);
gsftSubmit(null, g_form.getFormElement(), 'take_assessment');
}
With these changes, both options should be able to retrieve the correct URL and redirect the user to the assessment instance page.
Please mark my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2023 11:11 AM
Hi @Syedmd08 ,
Thanks for your reply.
I've tried both options and the same issue remains.
1. I added the code but the redirection is not working
2. I updated the but the same issue is there where the link field gets updated after the redirection. Threfore the users essentially needs to click the UI action twice to get sent to the correct URL.
-RV
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2023 11:15 AM
If the first option is not working, and the second option is still causing the issue of the URL field getting updated after the redirection, you can try the following approach that combines server-side and client-side code:
- In the server-side script of the UI action, retrieve the URL of the assessment instance for the current user and current risk, and set it to a field on the current record:
function setAssessmentInstanceLink() {
var sysID = current.sys_id.toString();
var userID = gs.getUserID().toString();
var gr = new GlideRecord('asmt_assessment_instance');
gr.addEncodedQuery('state=ready^user=' + userID + '^sn_grc_item=' + sysID);
gr.query();
if (gr.next()) {
var link = new global.AssessmentUtils().getAssessmentInstanceURL(gr.sys_id);
var url = '' + link + '';
url += '&sysparm_view=default_view';
var urlLink = url.toString();
current.u_assessment_instance_link = urlLink;
current.update();
}
}
setAssessmentInstanceLink();
- In the client-side script of the UI action, retrieve the URL from the field on the current record, and redirect the user to the assessment instance page:
function openAssessment() {
var urlLink = g_form.getValue('u_assessment_instance_link');
if (urlLink) {
g_navigation.openPopup(urlLink);
gsftSubmit(null, g_form.getFormElement(), 'take_assessment');
} else {
alert('Assessment instance link not available');
}
}
With this approach, the URL of the assessment instance is retrieved and set to a field on the current record when the UI action is clicked, and the user is redirected to the assessment instance page on the client-side when the "openAssessment" function is called. If the URL field is not available, an alert message is displayed to the user.
Please note that if the issue persists, there might be other factors affecting the behavior of the UI action, such as access controls or user permissions.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2023 11:30 AM - edited 03-10-2023 11:35 AM
After the code update, it's just getting the alert message but it's not updating the url link field.