- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2024 07:20 AM
Hi,
I want to show fields based on loggedin user.
The info msg comming correct but it is not setting(visible/mandetory) the field as per the code.
Script include:
var getUserDetails = Class.create();
getUserDetails.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getUserInformation: function() {
var user_sys_id = this.getParameter('sysparm_user_id');
var user = new GlideRecord('customer_contact');
user.addQuery("sys_id",user_sys_id);
user.query();
if(user.next())
return false;
else
return true;
},
type: 'getUserDetails'
});
Client script:
function onLoad() {
//Type appropriate comment here, and begin script below
var user = g_user.userID;
var ga = new GlideAjax('getUserDetails');
ga.addParam('sysparm_name', 'getUserInformation');
ga.addParam('sysparm_user_id', user);
ga.getXML(parseUserResponse);
function parseUserResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.addInfoMessage(answer);
if(answer == true){
g_form.setVisible("requested_for", false);
g_form.setMandatory("requested_for", false);
g_form.setVisible("requested_for1", true);
g_form.setVisible("company_name", true);
g_form.setVisible("company_number", true);
g_form.setMandatory("company_name", true);
g_form.setMandatory("company_number", true);
g_form.setMandatory("requested_for1", true);
}
if(answer ==false)
g_form.setVisible("requested_for", true);
g_form.setMandatory("requested_for", true);
g_form.setVisible("requested_for1", false);
g_form.setVisible("company_name", false);
g_form.setVisible("company_number", false);
g_form.setMandatory("company_name", false);
g_form.setMandatory("company_number", false);
g_form.setMandatory("requested_for1", false);
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2024 07:32 AM
Hi @Rosy14
In Client Script :
please modify like this :
You need to add quotations for both answer for true and false
if(answer == 'true'){
Please change for answer =false
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2024 07:35 AM
Hello @Rosy14 ,
Please modify your if condition and make it -
if(answer == "true");
If my answer solves your issue please mark it as Accepted ✔️ and Helpful 👍 based on impact.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2024 08:05 AM
There are missing {}s in your second if statement - that's why the rest of the code runs anyway and makes the fields non-mandatory again.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2024 07:31 AM - edited 07-08-2024 07:32 AM
Hi Rosy,
The below script will never work as you are comparing user sys id to a record in customer_contact table.
var user_sys_id = this.getParameter('sysparm_user_id');
var user = new GlideRecord('customer_contact');
user.addQuery("sys_id",user_sys_id);
user.query();
if(user.next())
return false;
else
return true;
Do you want to check a field on customer_contact table?
Also, For this scenario, instead of Glide Ajax I would suggest you use Display BR + Client script.
In Display BR check the logged in user and set a scratchpad variable.
In your onload client script check the Check the scratchpad variable and set the field.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2024 07:32 AM
Hi Rosy,
Try 'true' and 'false' instead of true/false in the if statements, that should help.
Cheers,
Lukasz
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2024 07:32 AM
Hi @Rosy14
In Client Script :
please modify like this :
You need to add quotations for both answer for true and false
if(answer == 'true'){
Please change for answer =false
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2024 07:35 AM
Hello @Rosy14 ,
Please modify your if condition and make it -
if(answer == "true");
If my answer solves your issue please mark it as Accepted ✔️ and Helpful 👍 based on impact.