UI Policy to prevent Form Submission when Selected Fields are not Populated - from All views

WazzaJC
Tera Expert

UI Policy to prevent Form Submission when Selected Fields are not Populated - from All (Core UI, Service Portal etc) views

 

Hi guys,

 

How can I write a UI Policy which prevents a user from submitting a form (either via Core UI or Service Portal views), unless all/specific Fields are populated.

 

Please see my screenshots attached.

 

I have 3 Fields: Building (u_building), Floor (u_floor) and Space (u_space) as well as a Location Not Found? Checkbox.

These are not Mandatory Fields and I need to leave them as Not Mandatory on the Form itself.

 

However, I want that the User has to populate at least one of the fields Building, Floor, Space or if they check Location Not Found?, a new box pops up called Location Detail (u_location_detail) and they will have to at least complete that box.

 

Basically I don't want users submitting the Form, with blank Location data. It has to either have data in either one of the Building, Floor or Space fields or if Location Not Found? = True, the user then has to enter data in the Location Detail field, before a Form can be saved/submitted.

 

How can I set this up - is it via a UI Policy and what would that look like?

 

Many thanks guys - always appreciate the help.

3 ACCEPTED SOLUTIONS

Manmohan K
Tera Sage

Hi @WazzaJC 

 

You need to write a on submit client script with code like below

function onSubmit() {
  var building = g_form.getValue('u_building');
  var floor = g_form.getValue('u_floor');
  var space = g_form.getValue('u_space');
  var locationNotFound = g_form.getValue('u_location_not_found');
  var locationDetail = g_form.getValue('u_location_detail');

  if ((!building || !floor || !space) && (!locationNotFound || !locationDetail)) {
    alert('Please provide location information before submitting the form.');
    return false; 
  }
  
  return true; // Allow form submission
}

View solution in original post

@WazzaJC 

 

Try this then

 

function onSubmit() {
    var building = g_form.getValue('u_building');
    var floor = g_form.getValue('u_floor');
    var space = g_form.getValue('u_space');
    var locationNotFound = g_form.getValue('location_not_found');
    var locationDetail = g_form.getValue('u_location_detail');

    // Check if either Building is populated, or Building and Floor are populated,
    // or Building, Floor, and Space are populated, or Location Not Found with Location Detail filled
    if ((building && !floor && !space) ||
        (building && floor && !space) ||
        (building && floor && space) ||
        (locationNotFound && locationDetail)) {
        return true; // Allow form submission
    } else {
        // Display an error message to the user
        alert('Please provide at least one valid location data combination.');
        return false; // Prevent form submission
    }
}

View solution in original post

@WazzaJC 

 

You can create the same onSubmit Client Script on the Record Producer and it will work.

Just make sure to change the variable names in the code if they are different in the record producer

 

View solution in original post

8 REPLIES 8

Manmohan K
Tera Sage

Hi @WazzaJC 

 

You need to write a on submit client script with code like below

function onSubmit() {
  var building = g_form.getValue('u_building');
  var floor = g_form.getValue('u_floor');
  var space = g_form.getValue('u_space');
  var locationNotFound = g_form.getValue('u_location_not_found');
  var locationDetail = g_form.getValue('u_location_detail');

  if ((!building || !floor || !space) && (!locationNotFound || !locationDetail)) {
    alert('Please provide location information before submitting the form.');
    return false; 
  }
  
  return true; // Allow form submission
}

Hi Manmohan - thanks so much, this almost works, but not fully.

I need the script above to allow OR. So if Building is populated that is enough, or Building and Floor, or Building, Floor and Space, but I need it to allow the submit to go ahead, if just Building is filled in Or Building and Floor for example - how can I amend your above script for this scenario ?

Many thanks Manmohan.

@WazzaJC 

 

Try this then

 

function onSubmit() {
    var building = g_form.getValue('u_building');
    var floor = g_form.getValue('u_floor');
    var space = g_form.getValue('u_space');
    var locationNotFound = g_form.getValue('location_not_found');
    var locationDetail = g_form.getValue('u_location_detail');

    // Check if either Building is populated, or Building and Floor are populated,
    // or Building, Floor, and Space are populated, or Location Not Found with Location Detail filled
    if ((building && !floor && !space) ||
        (building && floor && !space) ||
        (building && floor && space) ||
        (locationNotFound && locationDetail)) {
        return true; // Allow form submission
    } else {
        // Display an error message to the user
        alert('Please provide at least one valid location data combination.');
        return false; // Prevent form submission
    }
}

This is perfect, excellent Manmohan, exactly what I needed !!

You are a star, and such a quick response, I am truly grateful.

This is working like a dream, thank you for this guidance.

Kindest Regards, Warwick