Execute postvalid - Online Form
JS Hook¶
Developers can enhance the Merci Online Entry System by executing custom JavaScript code whenever a user interacts with specific fields during master data entry. This allows for real-time data fetching, validation, or auto-filling of fields based on user input.
Use Case¶
To auto-fill the code
field based on the selected account
and type
, you can hook into the form field events using a postvalid
function.
Code Implementation¶
Place the following function inside your {project}_core.js
file:
function fasmast_type0_postvalid() {
let account = $('#account0').val(); // Get value from 'account' input field
let type = $('#type0').val(); // Get value from 'type' dropdown or input field
// Only proceed if type is SALE_PTY or PURCH_PTY
if (type == 'SALE_PTY' || type == 'PURCH_PTY') {
// Make a POST request to the backend to fetch code based on account & type
$.ajax({
type: 'POST',
url: '/project/ws.php',
data: { func: 'getAcntCode', account, type },
success: function(data) {
let { code } = JSON.parse(data); // Parse returned JSON
$('#code0').val(code); // Set the 'code' field with returned value
}
});
}
}
Notes¶
fasmast_type0_postvalid()
is automatically triggered when thetype
field loses focus (blur event) in the online entry form.- The
type0
suffix refers to the HTML element's name used by Merci internally for the online form's field in the form. - This technique is useful for auto-population, validation, or dependent dropdowns based on user input.
Important Tips as per example¶
- Make sure
#account0
,#type0
, and#code0
are correct selectors matching the HTML input IDs. - Ensure your backend PHP (
ws.php
) has thegetAcntCode
function implemented and returns proper JSON like:{ "code": "XYZ123" }
- Test thoroughly on staging before deploying to production.