Visibility of Variable on Record Producer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2024 05:52 AM - edited 09-05-2024 09:31 PM
We have a record producer on our CSM portal titled "Create a Case". I’ve added a variable called 'Product', which references the Sold Products table (sn_install_base_sold_product_list). However, I only want this variable to be visible under specific conditions.
On the Account form, there is a field called 'Account Team' with several choice options.
If the 'Account Team' field has either Choice A or Choice B selected, I want the 'Product' variable on the "Create a Case" record producer to be visible to the customer.
If neither of these choices is selected, the 'Product' variable should remain hidden.
Essentially, when a customer accesses the 'Create a Case' record producer, it should check the 'Account Team' field on their account and show or hide the 'Product' variable based on the selected choices.
Any guidance or suggestions on how to implement this would be greatly appreciated!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-08-2024 09:52 PM
I have tried to do this via a catalog client script but does not seem to be working:
function onLoad() {
// Define the variable name for the 'Product' field, which references a catalog variable
var productVar = 'sold_product'; // Name of the catalog variable that will be shown/hidden
// Create a GlideRecord object to query the 'customer_account' table
var grAccount = new GlideRecord('customer_account');
// Add a query to find the account associated with the current user (based on company ID)
grAccount.addQuery('sys_id', g_user.companyID); // 'g_user.companyID' retrieves the current user's associated company
grAccount.query(); // Execute the query on the 'customer_account' table
// Check if the query returned a valid account record
if (grAccount.next()) { // If a matching account record is found
// Retrieve the value of the 'Account Team' field from the account record
var accountTeam = grAccount.getValue('u_account_team'); // 'u_account_team' is the field containing the Account Team choice
// Check if the 'Account Team' field has either 'SOC' or 'Shared' selected
if (accountTeam == 'SOC' || accountTeam == 'Shared') {
// Show the 'Product' variable on the Record Producer if the condition is met
g_form.setDisplay(productVar, true); // Display the 'Product' variable
} else {
// Hide the 'Product' variable if the condition is not met
g_form.setDisplay(productVar, false); // Hide the 'Product' variable
}
} else {
// If no valid account record is found, hide the 'Product' variable by default
g_form.setDisplay(productVar, false); // Ensure 'Product' is hidden if no account is found
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-08-2024 11:25 PM - edited 09-08-2024 11:28 PM
GlideRecord not works in Client Script.
Please use GlideAjax if you need to query any table and get the data.
After getting response by using GlideAjax, check if the field is mandatory or not.
If yes, you have to make the field non-mandatory before hiding it.
g_form.setMandatory("sold_product", false);
g_form.setDisplay("sold_product", false);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-09-2024 09:58 PM
I have used GlideAjax and created both scripts but not woking and 'product; variable now remains hidden at all times no matter what 'account team' is selected:
Catalog Client Script:
function onLoad() {
// Define the variable name for the 'Product' field
var productVar = 'sold_product';
// Create a GlideAjax object to call the Script Include 'AccountTeamHelper'
var ga = new GlideAjax('AccountTeamHelper');
ga.addParam('sysparm_name', 'getAccountTeam'); // Call the 'getAccountTeam' method in the Script Include
// Send the request to the server and get the response
ga.getXMLAnswer(function(response) {
var accountTeam = response; // The Account Team value returned from the server
// Check if the field is mandatory and make it non-mandatory before hiding
if (g_form.isMandatory(productVar)) {
g_form.setMandatory(productVar, false); // Make the field non-mandatory
}
// Show or hide 'Product' variable based on 'Account Team' field
if (accountTeam == 'SOC' || accountTeam == 'Shared') {
g_form.setDisplay(productVar, true); // Show 'Product' variable
} else {
g_form.setDisplay(productVar, false); // Hide 'Product' variable
}
});
}
Script Include:
// Script Include for retrieving the Account Team field value
var AccountTeamHelper = Class.create();
AccountTeamHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, {
// Retrieve the 'Account Team' based on the current user's account
getAccountTeam: function() {
var result = '';
var companyID = gs.getUser().getCompanyID(); // Get the user's company ID
var grAccount = new GlideRecord('customer_account');
if (grAccount.get('sys_id', companyID)) {
result = grAccount.getValue('u_account_team'); // Get the 'Account Team' field value
}
return result; // Return the value back to the client-side
}
});