- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2024 07:20 AM
When the form in the portal is opened, this variable need to be setted to true or false based on this logic:
If the logged user has no records for table ALM Software List, set the variable to true
If the logged user has 1 or more records for table ALM Software List, set the variable to false
I tries with below script but it is always returning true value. Please help on this
Script Include:
Client script:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2024 08:15 AM
@Pushpanjali3 Issue is with the "userName" parameter which you are passing from client script. You are querying "x_stmin_alm_applic_alm_software_list" table by putting sys_id of user in "sys_created_by".
System stores "User ID" field of user table in "sys_created_by" field not the user's sys_id. Because of this invalid query it's ignoring the query and returning all record from "x_stmin_alm_applic_alm_software_list" table. Hence it is returning "true" everytime from your script include.
You should use "g_user.userName" method is client script instead of g_user.userID.
var userName = g_user.userName;
User below client script-
function onLoad() {
var userName = g_user.userName; //this will return value present in "User ID" field of User table
var gaMatchingRecord = new GlideAjax('CheckALMSoftwareList');
gaMatchingRecord.addParam('sysparm_name', 'hasRecordsForUser');
gaMatchingRecord.addParam('userId', userName);
gaMatchingRecord.getXMLAnswer(_handleResponse);
}
function _handleResponse(response) {
var answer = response;
if (answer === 'true') {
return;
} else if (answer === 'false') {
g_form.setValue('new_user', true);
g_form.save();
}
}
If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2024 07:38 AM
Hello
It seems like there might be a slight issue in your client script logic. Let's correct it. Since you want to set the variable new_user to true if the user has no records and false if the user has one or more records, you should handle the true case in the if block and the false case in the else block. Here's the corrected client script:
function _handleResponse(response) {
var answer = response;
if (answer === 'true') {
g_form.setValue('new_user', true);
g_form.save();
} else if (answer === 'false') {
g_form.setValue('new_user', false);
g_form.save();
}
}
With this correction, if the response is 'true', meaning the user has no records, it will set new_user to true. Otherwise, if the response is 'false', indicating that the user has one or more records, it will set new_user to false.
Thank You
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2024 07:47 AM
Use this, it will work
function _handleResponse(response) {
var answer = response;
if (answer === 'true') {
return;
} else {
g_form.setValue('new_user', true);
g_form.save();
}
}
If my answer helps you, make sure to hit the solution and helpful button.
Thanks and regards,
Saurabh Dubey.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2024 08:15 AM
@Pushpanjali3 Issue is with the "userName" parameter which you are passing from client script. You are querying "x_stmin_alm_applic_alm_software_list" table by putting sys_id of user in "sys_created_by".
System stores "User ID" field of user table in "sys_created_by" field not the user's sys_id. Because of this invalid query it's ignoring the query and returning all record from "x_stmin_alm_applic_alm_software_list" table. Hence it is returning "true" everytime from your script include.
You should use "g_user.userName" method is client script instead of g_user.userID.
var userName = g_user.userName;
User below client script-
function onLoad() {
var userName = g_user.userName; //this will return value present in "User ID" field of User table
var gaMatchingRecord = new GlideAjax('CheckALMSoftwareList');
gaMatchingRecord.addParam('sysparm_name', 'hasRecordsForUser');
gaMatchingRecord.addParam('userId', userName);
gaMatchingRecord.getXMLAnswer(_handleResponse);
}
function _handleResponse(response) {
var answer = response;
if (answer === 'true') {
return;
} else if (answer === 'false') {
g_form.setValue('new_user', true);
g_form.save();
}
}
If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!