- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-02-2024 03:00 AM
Hello Maybe you can help me, newb to scripting. I'm trying to make the cause notes of the case form to be mandatory
below is my code;
function onSubmit() {
var submitAction = g_form.getActionName();
//If "Close Case" or "Propose Solution" actions are triggered
// Add check for account QRO
var account = g_form.getValue("account");
//alert('This is Company Field Value: ' + account);
// Only apply the script if the account is QRO
if (account === "3115ed14878c52100caced370cbb3565") {
// If "Close Case" or "Propose Solution" actions are triggered
if (submitAction == "close" || submitAction == "proposeSolution") {
if (submitAction == "close" && g_form.hasField("closed_by"))
g_form.setValue("closed_by", g_user.userID, g_user.userName);
if (submitAction == "proposeSolution" && g_form.hasField("resolved_by"))
g_form.setValue("resolved_by", g_user.userID, g_user.userName);
//Validate if Cause Notes are populated
if (g_form.hasField("cause") && g_form.getValue("cause") == "") {
if (submitAction == "proposeSolution") {
g_form.addErrorMessage(
getMessage("To propose a solution, you must put a cause notes"),
);
} else {
g_form.addErrorMessage(
getMessage(
"To close a case, you must select a resolution code and add resolution notes.",
),
);
}
if (typeof g_tabs2Sections !== "undefined") {
var tabIndex = g_tabs2Sections.findTabIndex("cause");
if (tabIndex > -1) {
g_tabs2Sections.setActive(tabIndex); //checks if the findTabIndex() function found the 'cause' tab (i.e., if the index is valid, meaning it is greater than -1).
g_tabs2Sections.markTabMandatoryByField("cause"); // sets the tab mandatory
}
}
g_form.hideFieldMsg("cause");
if (g_form.getValue("cause") == "") {
g_form.showFieldMsg(
"cause",
getMessage("Cause notes are required."),
"error",
false,
);
}
return false;
}
}
}
}
however its not working.
though, its getting the account value.
what i did is i make a new On Submit client script based on the default one "Validate Resolution Information (Case)"
Also please see screenshot for reference.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2024 05:58 PM
I Ended up modifying a UI - Action " Resolve Case"
and changing my client script altogether to
REVISED WORKING CODE FOR CLIENT SCRIPT
function onSubmit() {
// Get the current action name (e.g., 'close', 'proposeSolution')
var submitAction = g_form.getActionName();
// Define the Sys ID for the Account to check against
var accountSysId = '3115ed14878c52100caced370cbb3565';
// Get the current Account field value (Sys ID)
var account = g_form.getValue('account');
// Check if the Account matches the specified Sys ID and if the action is one of the specified actions
if (submitAction == "close" || submitAction == "proposeSolution" || submitAction == "proposeSolution_dd") {
// Get the values of Cause, Resolution Code, and Close Notes
var cause = g_form.getValue('cause');
var resolutionCode = g_form.getValue('resolution_code');
var closeNotes = g_form.getValue('close_notes');
// If the Account is the specific Sys ID
if (account == accountSysId) {
// Check if all the fields (Cause, Resolution Code, Close Notes) are filled out
var fieldsEmpty = false;
var emptyFields = [];
if (!cause || cause.trim() === '') {
fieldsEmpty = true;
emptyFields.push('Cause');
}
if (!resolutionCode || resolutionCode.trim() === '') {
fieldsEmpty = true;
emptyFields.push('Resolution Code');
}
if (!closeNotes || closeNotes.trim() === '') {
fieldsEmpty = true;
emptyFields.push('Close Notes');
}
if (fieldsEmpty) {
// Make all three fields mandatory
g_form.setMandatory('cause', true);
g_form.setMandatory('resolution_code', true);
g_form.setMandatory('close_notes', true);
// Show an error message for the empty fields
var message = 'The following fields must be filled out: ' + emptyFields.join(', ') + '.';
g_form.addErrorMessage(message);
// Return false to prevent form submission
return false;
}
} else {
// If Account is not equal to the specified Sys ID
var fieldsEmpty = false;
var emptyFields = [];
// Only check Resolution Code and Close Notes for mandatory status
if (!resolutionCode || resolutionCode.trim() === '') {
fieldsEmpty = true;
emptyFields.push('Resolution Code');
}
if (!closeNotes || closeNotes.trim() === '') {
fieldsEmpty = true;
emptyFields.push('Close Notes');
}
if (fieldsEmpty) {
// Make only Resolution Code and Close Notes mandatory
g_form.setMandatory('resolution_code', true);
g_form.setMandatory('close_notes', true);
// Show an error message for the empty fields
var message = 'The following fields must be filled out: ' + emptyFields.join(', ') + '.';
g_form.addErrorMessage(message);
// Return false to prevent form submission
return false;
}
}
}
// If conditions are not met, allow the submission
return true;
}
while inactivating the default client script named " Validate Resolution Information (Case) "
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-02-2024 05:22 AM
My question is why would you need to make a field mandatory by script? Is that because you want to have it conditionally?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-02-2024 08:27 AM
client requirement, need to ascertain overall impact to other clients, also it can be either for lessons learn or to aid in client conversation.
at the same time OOTB functions wont be able to provide the level of complexity for the conditions that's why.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-02-2024 09:10 AM
There is a Data Policy on the Incident [incident] table titled Make close info mandatory when resolved or closed. This is what sets the Resolution code and Close notes fields mandatory. You can clone this data policy and modify it to your needs. I found this by opening a Incident record and doing Configure > All and then looking through the tabs for what might mark these fields as mandatory.
As for the error messages that display in your screenshot -- I don't have those on my end, nor does my PDI have that onSubmit client script. To find out where those come from, I would recommend going into Studio and doing a code search for the text.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2024 05:58 PM
I Ended up modifying a UI - Action " Resolve Case"
and changing my client script altogether to
REVISED WORKING CODE FOR CLIENT SCRIPT
function onSubmit() {
// Get the current action name (e.g., 'close', 'proposeSolution')
var submitAction = g_form.getActionName();
// Define the Sys ID for the Account to check against
var accountSysId = '3115ed14878c52100caced370cbb3565';
// Get the current Account field value (Sys ID)
var account = g_form.getValue('account');
// Check if the Account matches the specified Sys ID and if the action is one of the specified actions
if (submitAction == "close" || submitAction == "proposeSolution" || submitAction == "proposeSolution_dd") {
// Get the values of Cause, Resolution Code, and Close Notes
var cause = g_form.getValue('cause');
var resolutionCode = g_form.getValue('resolution_code');
var closeNotes = g_form.getValue('close_notes');
// If the Account is the specific Sys ID
if (account == accountSysId) {
// Check if all the fields (Cause, Resolution Code, Close Notes) are filled out
var fieldsEmpty = false;
var emptyFields = [];
if (!cause || cause.trim() === '') {
fieldsEmpty = true;
emptyFields.push('Cause');
}
if (!resolutionCode || resolutionCode.trim() === '') {
fieldsEmpty = true;
emptyFields.push('Resolution Code');
}
if (!closeNotes || closeNotes.trim() === '') {
fieldsEmpty = true;
emptyFields.push('Close Notes');
}
if (fieldsEmpty) {
// Make all three fields mandatory
g_form.setMandatory('cause', true);
g_form.setMandatory('resolution_code', true);
g_form.setMandatory('close_notes', true);
// Show an error message for the empty fields
var message = 'The following fields must be filled out: ' + emptyFields.join(', ') + '.';
g_form.addErrorMessage(message);
// Return false to prevent form submission
return false;
}
} else {
// If Account is not equal to the specified Sys ID
var fieldsEmpty = false;
var emptyFields = [];
// Only check Resolution Code and Close Notes for mandatory status
if (!resolutionCode || resolutionCode.trim() === '') {
fieldsEmpty = true;
emptyFields.push('Resolution Code');
}
if (!closeNotes || closeNotes.trim() === '') {
fieldsEmpty = true;
emptyFields.push('Close Notes');
}
if (fieldsEmpty) {
// Make only Resolution Code and Close Notes mandatory
g_form.setMandatory('resolution_code', true);
g_form.setMandatory('close_notes', true);
// Show an error message for the empty fields
var message = 'The following fields must be filled out: ' + emptyFields.join(', ') + '.';
g_form.addErrorMessage(message);
// Return false to prevent form submission
return false;
}
}
}
// If conditions are not met, allow the submission
return true;
}
while inactivating the default client script named " Validate Resolution Information (Case) "