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

You can't do that especially when you set mandatory in the dictionary. That is saying it is always mandatory. I'm not sure I understand the logic. What is the point of a mandatory field if you can just bypass it when clicking save. It doesn't seem like it is a mandatory field.

You cannot do that. Because you have mandatory at the dictionary level as soon as the form reloads from the asterisk will reappear. Whey do you even have a mandatory field the is technically not mandatory if you hit save?

Yes, based on the code I provided it allows to save without satisfying the fields. I performed this on multiple forms using the same code. It worked. 

Thank you

Still does not make sense to me. Why make the field mandatory if you can bypass it. Might as well just not have a mandatory field.

I just didn't include a code in there where it needs to update a field status to certain value each time the button is clicked.