Using return false in Glide Ajax

JohnnySnow
Kilo Sage

Hi Team

 

We have a requirement to make the attachment mandatory in Service catalog on certain conditions. We have a custom table which holds the value if a particular request needs attachment or not. Below is the table structure for eg.

Variable Group Catalog Item Choice 1 Choice 2 Requires Attachment
Catalog1 Mycatalogitem1 Wipro DEF TRUE
Catalog1 Mycatalogitem1 TCS ABC FALSE
Catalog1 Mycatalogitem1 Infosys EFG TRUE

We have requestor information variable set on the catalog form which has fields like 

- requested for

-location

-email etc.

In location table we have a field called 'company'. We need to get that details to build the query. Hence I have created below onSubmit catalog client script.

//Below is the script include
SI Name: VListUtils
 isAttachmentMandatory: function() {
        var grCvar = new GlideRecord('customtablename');
        grCvar.addEncodedQuery(this.getParameter('sysparm_query'));
        grCvar.query();

        if (grCvar.next()) {
            if (grCvar.attachment_required== true) {
                return true;
            } else
                return false;
        }
    }

//Client script

function onSubmit() {
    //Type appropriate comment here, and begin script below

    //Get Company Details
    var comp;
    var loc = g_form.getReference('location', callbk);

    function callbk(loc) {
        var val = loc.company.toString();
        if (val == '5d608b211be8c1108622ece0604bcbf0')
            comp = 'Wipro';
        else
            comp = 'TECHM';

        var query = 'u_variable_group=catalog1^u_catalog_item=e7e89236dbc99dd44d1c633fd3961966^u_choice_1=' + comp + '^u_choice_2=' + g_form.getDisplayValue('req_type');

        var result = checkAttachment(query);
    }

}

function checkAttachment(val) {
    var ga = new GlideAjax('CatalogVariablesListUtil');
    ga.addParam('sysparm_name', 'isAttachmentMandatory');
    ga.addParam('sysparm_query', val);
    ga.getXMLAnswer(function returnVal(response) {

        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer == 'true') {
            if (this.document.getElementsByClassName('get-attachment').length == 0) {
                alert("Please attach a file");
                return;
            }
            g_scratchpad._ajaxChecked = true;

        }
	 
    });

return false;
}

I referred below link to modify my code, but the validation is not working properly and neither gives the pop up to attach a file.

 

Can some one help me to resolve the issue?

Thanks
Johnny

Please mark this response as correct or helpful if it assisted you with your question.
1 ACCEPTED SOLUTION

function onSubmit() {
    //Type appropriate comment here, and begin script below


    var comp;
    var val;
    var loc = g_form.getReference('location', callbk);
    if (val == '5d608b211be8c1108622ece0604bcbf0')
        comp = 'Wipro';
    else
        comp = 'TechM';

    var query = 'u_variable_group=MicroFocusALMRequest^u_catalog_item=e7e89236dbc99dd44d1c633fd3961966^u_choice_1=' + comp + '^u_choice_2=' + g_form.getDisplayValue('req_type');

    if (g_scratchpad.isFormValid)
        return true;

    var ga = new GlideAjax('CatalogVariablesListUtil');
    ga.addParam('sysparm_name', 'isAttachmentMandatory');
    ga.addParam('sysparm_query', query);
    ga.getXMLAnswer(setAnswer);
    return false; //Stopping the submission

    function setAnswer(answer) {
        if (answer == 'true' && this.document.getElementsByClassName('get-attachment').length == 0) {
            alert("Please attach a file");
            return false;
        }
        var actionName = g_form.getActionName();
        g_scratchpad.isFormValid = true;
        g_form.submit(actionName);//resubmitting when Form is valid.


    }
}

function callbk(loc) {
    val = loc.company.toString();
}
Thanks
Johnny

Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

3 REPLIES 3

JohnnySnow
Kilo Sage

Sharing the link which I referred..https://community.servicenow.com/community?id=community_blog&sys_id=dc49feeadbdd7f0c54250b55ca96191d&view_source=searchResult

Thanks
Johnny

Please mark this response as correct or helpful if it assisted you with your question.

function onSubmit() {
    //Type appropriate comment here, and begin script below


    var comp;
    var val;
    var loc = g_form.getReference('location', callbk);
    if (val == '5d608b211be8c1108622ece0604bcbf0')
        comp = 'Wipro';
    else
        comp = 'TechM';

    var query = 'u_variable_group=MicroFocusALMRequest^u_catalog_item=e7e89236dbc99dd44d1c633fd3961966^u_choice_1=' + comp + '^u_choice_2=' + g_form.getDisplayValue('req_type');

    if (g_scratchpad.isFormValid)
        return true;

    var ga = new GlideAjax('CatalogVariablesListUtil');
    ga.addParam('sysparm_name', 'isAttachmentMandatory');
    ga.addParam('sysparm_query', query);
    ga.getXMLAnswer(setAnswer);
    return false; //Stopping the submission

    function setAnswer(answer) {
        if (answer == 'true' && this.document.getElementsByClassName('get-attachment').length == 0) {
            alert("Please attach a file");
            return false;
        }
        var actionName = g_form.getActionName();
        g_scratchpad.isFormValid = true;
        g_form.submit(actionName);//resubmitting when Form is valid.


    }
}

function callbk(loc) {
    val = loc.company.toString();
}
Thanks
Johnny

Please mark this response as correct or helpful if it assisted you with your question.

sanket16
Giga Guru

If you are using cart for requesting the same item for different users where some users are allowed to submit while some are restricted.

In this case,the g_scratchpad will be set true for first passed ,these this will allow to add the item for the non eligible user also.

 

To Restrict this, the g_scratchpad need to be reset after adding the first item in cart.

function onSubmit() {
//Type appropriate comment here, and begin script below


var comp;
var val;
var loc = g_form.getReference('location', callbk);
if (val == '5d608b211be8c1108622ece0604bcbf0')
comp = 'Wipro';
else
comp = 'TechM';

var query = 'u_variable_group=MicroFocusALMRequest^u_catalog_item=e7e89236dbc99dd44d1c633fd3961966^u_choice_1=' + comp + '^u_choice_2=' + g_form.getDisplayValue('req_type');

if (g_scratchpad.isFormValid)
g_scratchpad.isFormValid = false;
return true;

var ga = new GlideAjax('CatalogVariablesListUtil');
ga.addParam('sysparm_name', 'isAttachmentMandatory');
ga.addParam('sysparm_query', query);
ga.getXMLAnswer(setAnswer);
return false; //Stopping the submission

function setAnswer(answer) {
if (answer == 'true' && this.document.getElementsByClassName('get-attachment').length == 0) {
alert("Please attach a file");
return false;
}
var actionName = g_form.getActionName();
g_scratchpad.isFormValid = true;
g_form.submit(actionName); //resubmitting when Form is valid.


}
}

function callbk(loc) {
val = loc.company.toString();
}

 

Please find g_scratchpad reseted in above code highlighted.

 

Thanks,
Sanket