Change onSubmit Client Script to onChange
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2024 02:30 AM
Hi All,
I have got a working onSubmit Script for the Demand table, essentially when a few specified fields are left with a zero value and the Demand is submitted a pop up appears, sort of like a warning to make you aware.
I now need this to also trigger when the Demand moves into the "Qualified" State, I have tried a few things but am not able to get it working and I assumed that the easiest option would be to use a separate onChange script but as mentioned before had no luck with it, below is the onSubmit script currently in use:
function onSubmit() {
function mandatoryFieldValueLabels() {
var labels = '';
var fieldNames = ['financial_benefit','capital_outlay','operational_outlay','u_external_cost'];
var emptyFields = fieldNames.filter(function(field) {
return g_form.getIntValue(field) <= 0;
});
var fieldLabels = emptyFields.map(function(fieldName) {
g_form.hideFieldMsg(fieldName);
g_form.showFieldMsg(fieldName, 'Check valid zero value', 'error');
return g_form.getLabelOf(fieldName);
});
return fieldLabels.join('\n').trim();
}
if (g_form.getActionName() == 'sysverb_insert') { // Was 'qualify' changed with STRY0018402
var zeroFieldValues = mandatoryFieldValueLabels();
if (zeroFieldValues.length > 0) {
var result = confirm('The following finance fields have a zero value, please confirm you wish to proceed: \n\n' + mandatoryFieldValueLabels());
if (!result) {
return false;
}
}
}
}
Any help would be greatly appreciated.
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2024 03:49 AM
Hi @patrykprejs ,
You should definitely have two separate scripts, one for OnChange and other for OnSubmit.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
if (g_form.getValue('state') === 'Qualified') {
var zeroFieldValues = mandatoryFieldValueLabels(); // Reuse your existing function
if (zeroFieldValues.length > 0) {
var result = confirm('The following finance fields have a zero value. Do you wish to proceed?\n\n' + zeroFieldValues);
if (!result) {
g_form.setValue('state', oldValue); // Roll back the state change
}
}
}
}
function mandatoryFieldValueLabels() {
// Your existing logic here
// ...
}
If my response has resolved your query, please consider giving it a thumbs up and marking it as the correct answer!
Thanks & Regards,
Sanjay Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2024 07:04 AM - edited 06-04-2024 07:05 AM
Hi @Community Alums,
Thank you for the above suggestion, when I tried to implement it is still not doing anything. Am I missing something?
(Apologies, I am still learning on the scripting aspect of my knowledge)
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
if (g_form.getValue('state') === 'qualified') {
var zeroFieldValues = mandatoryFieldValueLabels(); // Reuse your existing function
if (zeroFieldValues.length > 0) {
var result = confirm('The following finance fields have a zero value. Do you wish to proceed?\n\n' + zeroFieldValues);
if (!result) {
g_form.setValue('state', oldValue); // Roll back the state change
}
}
}
}
function mandatoryFieldValueLabels() {
// Your existing logic here
// ...
var labels = '';
var fieldNames = ['financial_benefit','capital_outlay','operational_outlay','u_external_cost'];
var emptyFields = fieldNames.filter(function(field) {
return g_form.getIntValue(field) <= 0;
});
var fieldLabels = emptyFields.map(function(fieldName) {
g_form.hideFieldMsg(fieldName);
g_form.showFieldMsg(fieldName, 'Check valid zero value', 'error');
return g_form.getLabelOf(fieldName);
});
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2024 07:31 AM
Hi @patrykprejs ,
can you please share both the client scripts so that I can take a look?
Thanks,
Sanjay kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2024 01:05 AM
Hi @Community Alums,
Of course, thank you for looking over this both scripts are below:
onSubmit Script:
function onSubmit() {
function mandatoryFieldValueLabels() {
var labels = '';
var fieldNames = ['financial_benefit','capital_outlay','operational_outlay','u_external_cost'];
var emptyFields = fieldNames.filter(function(field) {
return g_form.getIntValue(field) <= 0;
});
var fieldLabels = emptyFields.map(function(fieldName) {
g_form.hideFieldMsg(fieldName);
g_form.showFieldMsg(fieldName, 'Check valid zero value', 'error');
return g_form.getLabelOf(fieldName);
});
return fieldLabels.join('\n').trim();
}
if (g_form.getActionName() == 'sysverb_insert') { // Was 'qualify' changed with STRY0018402
var zeroFieldValues = mandatoryFieldValueLabels();
if (zeroFieldValues.length > 0) {
var result = confirm('The following finance fields have a zero value, please confirm you wish to proceed: \n\n' + mandatoryFieldValueLabels());
if (!result) {
return false;
}
}
}
}
onChange Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
if (g_form.getValue('state') === 'qualified') {
var zeroFieldValues = mandatoryFieldValueLabels(); // Reuse your existing function
if (zeroFieldValues.length > 0) {
var result = confirm('The following finance fields have a zero value. Do you wish to proceed?\n\n' + zeroFieldValues);
if (!result) {
g_form.setValue('state', oldValue); // Roll back the state change
}
}
}
}
function mandatoryFieldValueLabels() {
// Your existing logic here
// ...
var labels = '';
var fieldNames = ['financial_benefit','capital_outlay','operational_outlay','u_external_cost'];
var emptyFields = fieldNames.filter(function(field) {
return g_form.getIntValue(field) <= 0;
});
var fieldLabels = emptyFields.map(function(fieldName) {
g_form.hideFieldMsg(fieldName);
g_form.showFieldMsg(fieldName, 'Check valid zero value', 'error');
return g_form.getLabelOf(fieldName);
});
return fieldLabels.join('\n').trim();
}
Thank you.