How to make field mandatory based upon view

taylor13
Tera Contributor

Hello Team,

I have 2 fields I want to make mandatory based upon different different view. and also want to make mandatory on workspaces. How can I achieved that?

@Community Alums 

2 REPLIES 2

Saurav11
Kilo Patron
Kilo Patron

Hello,

 

Depending on how you are making the view mandatory, if it is UI policy or client script you will have the option  to write it for a specific view using the view field. Mark the global as false you will then see the field to write the view name:-

 

Client script:-

Saurav11_0-1686053582579.png

 

Ui policy:-

Saurav11_1-1686053636749.png

 

Please mark my answer as correct based on Impact.

Amit Gujarathi
Giga Sage
Giga Sage

HI @taylor13 ,
I trust you are doing great.

  1. Identify the view or workspace where you want to make fields mandatory.

  2. Locate the client script associated with that view or workspace. This can be done by navigating to "System Definition" -> "Client Scripts" and filtering by the appropriate table and view.

  3. Create a new client script or modify the existing one, and add the following code:

 

function onSubmit() {
  // Check the current view or workspace
  var currentView = g_form.getViewName();
  
  // Check if the mandatory fields should be enforced for this view or workspace
  if (currentView == 'your_view_name' || currentView == 'your_workspace_name') {
    // Get the values of the fields you want to make mandatory
    var field1Value = g_form.getValue('your_field1_sys_id');
    var field2Value = g_form.getValue('your_field2_sys_id');
    
    // Perform validation and display error messages if the fields are empty
    if (field1Value == '') {
      g_form.showFieldMsg('your_field1_sys_id', 'This field is mandatory', 'error');
      return false; // Prevent the form submission
    }
    
    if (field2Value == '') {
      g_form.showFieldMsg('your_field2_sys_id', 'This field is mandatory', 'error');
      return false; // Prevent the form submission
    }
  }
  
  // Allow the form submission if the fields are not mandatory or have values
  return true;
}

 

  • Replace 'your_view_name' with the actual name of the view where you want to enforce the mandatory fields. Similarly, replace 'your_workspace_name' with the actual name of the workspace.

  • Replace 'your_field1_sys_id' and 'your_field2_sys_id' with the actual sys_ids of the fields you want to make mandatory. You can find the sys_id of a field by inspecting its HTML element on the form.

  • Save the client script.

  •  

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi