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

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.

@Sonu Singh 

So did you keep the Macro variable?

Regards
Ankur

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

@Ankur Bawiskar 

Yes

Sudhanshu Talw1
Tera Guru

Hi instead of this create a checkbox type variable & call the script include onChange & on the basis of return result make it true/false.

Check the checkbox variable onSubmit validation & show appropriate message as with onSubmit script include call it cause the issue.

Or other option is to use getXmlWait()

Thanks

Sudhanshu