UI Action | Set Mandatory to false

Spaceballs
Kilo Sage

Hi,

I am currently stuck on getting this code work. 

 

When a Save Button is clicked, I want the fields mandatory set to false and prompt for confirmation. 

 

When is cancelled, the fields should set it back to mandatory or new record. 

 

When is Ok, set the form back to new

 

Can you help revise the code for me?

 

UI Action Script

Spaceballs_0-1675195550284.png

Spaceballs_1-1675195580313.png

 

function confirmationdialog() {
    var answer = confirm('Ready to save?');
    if (answer == true) {
        gsftSubmit(null, g_form.getFormElement(), 'sys_update_value');
    } else {
        var fields = g_form.getEditableFields();
        for (var x = 0; x > fields.length; x++) {
            g_form.setMandatory(fields[x], false);
        }
        return false;
    }
}
if (typeof window == 'undefined')
    priorityUpdate();

function priorityUpdate() {
    current.update();
    action.setRedirectURL(current);
}

Expected result

Spaceballs_2-1675195638912.png

Spaceballs_4-1675195854339.png

 

 

1 ACCEPTED SOLUTION

The revised the code, I'm able to save the form without having to resolve the mandatory field. But the Mandatory asterisk symbol still visible when the Save button is clicked.

 

I would like the asterisk symbol is set to not visible when save button is clicked. 

 

function confirmationdialog() {

    // Get the reference to the current form
    var form = g_form;

    // Get an array of all the editable fields on the form
    var editableFields = form.getEditableFields();
    
	// Loop through the array of editable fields
    for (var i = 0; i < editableFields.length; i++) {
        
		// Set the current field as not mandatory
        form.setMandatory(editableFields[i], false);
    }
    // Prompt the user for confirmation
    var answer = confirm("Are you sure you want to save this form?");
	
    // If the user clicked "OK"
    if (answer) {

        // Save the form
        form.save();

        // Show a success message
        g_form.addInfoMessage("Form saved successfully");
    }
    
	// If the user clicked "Cancel"
    else {
        
		// Loop through the array of editable fields again
        for (var x = 0; x < editableFields.length; x++) {

            // Set the current field back to mandatory
            form.setMandatory(editableFields[x], true);
        }
        // Show an error message
        g_form.addErrorMessage("Save operation cancelled. Editable fields are now mandatory again.");
    }
}

 

View solution in original post

9 REPLIES 9

Brian Lancaster
Tera Sage

How is the field being set to mandatory?

The mandatories are set to true in the table

Sounds like you have mandatory to true on the dictionary level. This means you can never make it false unless it is filled in and/or you have some other conditions to set it to false. A record save if you clear the field will not work. Does the field value clear after save? I need more info on why you want it not to be mandatory. Usually once a field is filled it the mandatory asterisk will go gray and the field had meat the requirements for being filled in.

The revised the code, I'm able to save the form without having to resolve the mandatory field. But the Mandatory asterisk symbol still visible when the Save button is clicked.

 

I would like the asterisk symbol is set to not visible when save button is clicked. 

 

function confirmationdialog() {

    // Get the reference to the current form
    var form = g_form;

    // Get an array of all the editable fields on the form
    var editableFields = form.getEditableFields();
    
	// Loop through the array of editable fields
    for (var i = 0; i < editableFields.length; i++) {
        
		// Set the current field as not mandatory
        form.setMandatory(editableFields[i], false);
    }
    // Prompt the user for confirmation
    var answer = confirm("Are you sure you want to save this form?");
	
    // If the user clicked "OK"
    if (answer) {

        // Save the form
        form.save();

        // Show a success message
        g_form.addInfoMessage("Form saved successfully");
    }
    
	// If the user clicked "Cancel"
    else {
        
		// Loop through the array of editable fields again
        for (var x = 0; x < editableFields.length; x++) {

            // Set the current field back to mandatory
            form.setMandatory(editableFields[x], true);
        }
        // Show an error message
        g_form.addErrorMessage("Save operation cancelled. Editable fields are now mandatory again.");
    }
}