Need help onSubmit catalog client script, if condition is true then prevent submitting the request

Sonu Singh
Mega Expert

Hi All,

I have 2 fields(1. Reference Field & 2. Macro with Widget - Multi Line text) on the form which are depended on a drop down field(any one among the fields are displayed on the form). Multi Line text is a readOnly as its value is populated from the macro variable(Widget).

Now i need to add a validation step for the macro with Widget - multi line text variable to check whether Multi line text is empty or not.

Condition: 

result[0] is true ---  g_form.setDisplay('macro_widget', true); 
                       g_form.setDisplay('multi_line_texts', true); 
                       g_form.setDisplay('reference_field', false);


result[1] is true ---  g_form.setDisplay('macro_widget', false); 
                       g_form.setDisplay('multi_line_texts', false); 
                       g_form.setDisplay('reference_field', true);

I have written an onSubmit script:

with the below script i am getting alert but request is getting submitted. 

function onSubmit() {
    //     if (g_form.getValue('multi_line_text') == '') {
    //         alert('Please select something from the macro or go with the Reference Option');
    //         return false;
    //     }
    var ga = new GlideAjax('ToggleReferenceMacro');
    ga.addParam('sysparm_name', 'setDisplayForRefOrMacro');
    ga.addParam('sysparm_cat_item', g_form.getUniqueValue());
    ga.addParam('sysparm_cat_item_env', g_form.getValue('environment_new'));
    ga.getXML(setDisplayForRefOrMacro);

    function setDisplayForRefOrMacro(response) {
        var answer2 = response.responseXML.documentElement.getAttribute('answer');
        var result = answer2.split(";");
        var selectedEnt;
        if (result[0] == 'true' && result[1] == 'false') {
            if (g_form.getValue('multi_line_texts') == '') {
                alert('Please select something from the macro or go with the Reference Option');
                return false;
            }
        } else if (result[0] == 'false' && result[1] == 'true') {
            if (g_form.getValue('reference_field') == '') {
                alert('Please Select a value from refernce field');
                return false;
            }
        } else if (result[0] == 'false' && result[1] == 'false') {
           return true;
        } else if (result[0] == 'true' && result[1] == 'true') {
            if (g_form.getValue('multi_line_texts') == '') {
                alert('Please select something from the macro or go with the Reference Option');
                return false;
            } else if (g_form.getValue('reference_field') == '') {
                alert('Please Select a value from refernce field');
                return false;
            }
        }
    }
}

 

Thanks.

 

1 ACCEPTED SOLUTION

By this below script i have solved the issue.. 

function onSubmit() {
    if (g_form.getValue('multi_line_text') == '' && g_form.getValue('reference') == '') {
        alert('Please select something from the macro or go with the Reference Option');
        return false;
     }
}

 

i was doing too many thing which were of no use.

 

Thanks.

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Sonu,

since getXML() is asynchronous GlideAjax it might be submitting the form by the time ajax script include function returns the result.

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi @Ankur Bawiskar,

 

if i am trying this then i am getting response as null.

ga.getXMLWait();

alert(ga.getAnswer());

Can't we achieve in anyother way.

 

Thanks

Hi Sonu,

getXMLWait() won't work in portal as it is not allowed

Any specific reason you are using multi-line text using variable + widget when the variable type is already available OOB

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

i have 3 different variable i.e (1. Macro with Widget 2. Multi Line text) & 3. Reference. 

Multi-line text is readOnly and i am populating the data from the macro variable.

and other one is reference type so i have added an UI policy to do validated .

find_real_file.png