Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Help to combined scripts

JVINAY
Tera Contributor

Hi team,

 

I have written the following three scripts to restrict form submission based on different fields, but I want to combine them into a single script instead of using individual ones. Please help me understand how I can achieve this."

----------------------------

function onSubmit() {
var assignmentGrp = g_form.getValue('servicenow_assignment_group');
if (assignmentGrp == '' || assignmentGrp == undefined) {
g_form.showFieldMsg('servicenow_assignment_group', 'IT Functional Org must be updated within APM# before a CIR submission.', 'error');
return false; // Prevent submission
}
return true;
}

-------------------------------------------------------

function onSubmit() {
var appBunit = g_form.getValue('app_business_unit');
if (appBunit == '' || appBunit == undefined) {
g_form.showFieldMsg('app_business_unit', 'Business unit must be updated within APM# before a CIR submission.', 'error');
return false; // Prevent submission
}
return true;
}

-------------------------------------------------------------

function onSubmit() {
var drTier = g_form.getValue('dr_tier');
if (drTier == '' || drTier == undefined) {
g_form.showFieldMsg('dr_tier', 'Dr tier must be updated within APM# before a CIR submission.', 'error');
return false; // Prevent submission
}
return true;
}

 

 

Please help me how can i achieve this. 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@JVINAY 

try this

function onSubmit() {
    // Check Assignment Group
    var assignmentGrp = g_form.getValue('servicenow_assignment_group');
    if (!assignmentGrp) {
        g_form.showFieldMsg('servicenow_assignment_group', 'IT Functional Org must be updated within APM# before a CIR submission.', 'error');
        return false;
    }

    // Check App Business Unit
    var appBunit = g_form.getValue('app_business_unit');
    if (!appBunit) {
        g_form.showFieldMsg('app_business_unit', 'Business unit must be updated within APM# before a CIR submission.', 'error');
        return false;
    }

    // Check DR Tier
    var drTier = g_form.getValue('dr_tier');
    if (!drTier) {
        g_form.showFieldMsg('dr_tier', 'Dr tier must be updated within APM# before a CIR submission.', 'error');
        return false;
    }

    // All validations passed
    return true;
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@JVINAY 

try this

function onSubmit() {
    // Check Assignment Group
    var assignmentGrp = g_form.getValue('servicenow_assignment_group');
    if (!assignmentGrp) {
        g_form.showFieldMsg('servicenow_assignment_group', 'IT Functional Org must be updated within APM# before a CIR submission.', 'error');
        return false;
    }

    // Check App Business Unit
    var appBunit = g_form.getValue('app_business_unit');
    if (!appBunit) {
        g_form.showFieldMsg('app_business_unit', 'Business unit must be updated within APM# before a CIR submission.', 'error');
        return false;
    }

    // Check DR Tier
    var drTier = g_form.getValue('dr_tier');
    if (!drTier) {
        g_form.showFieldMsg('dr_tier', 'Dr tier must be updated within APM# before a CIR submission.', 'error');
        return false;
    }

    // All validations passed
    return true;
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Kirby R
Kilo Sage

@JVINAY ,try this.

 

function onSubmit() {
    var fields = ["servicenow_assignment_group","app_business_unit", "dr_tier"];
    var msgMap = {
        "servicenow_assignment_group": "IT Functional Org must be updated within APM# before a CIR submission.",
        "app_business_unit": "Business unit must be updated within APM# before a CIR submission.",
        "dr_tier":"Dr tier must be updated within APM# before a CIR submission."
    };  

    fields.forEach(function(f){
        var field = g_form.getValue(f);
        if (field == '' || field == undefined) {
            g_form.showFieldMsg(f, msgMap[f], 'error');
            return false; // Prevent submission
        }
        return true;
    }); 
}

 

Mark correct if you find this helpful.

nityabans27
Kilo Patron
function onSubmit() {
    var valid = true;

    var assignmentGrp = g_form.getValue('servicenow_assignment_group');
    if (!assignmentGrp) {
        g_form.showFieldMsg('servicenow_assignment_group', 'IT Functional Org must be updated within APM# before a CIR submission.', 'error');
        valid = false;
    }

    var appBunit = g_form.getValue('app_business_unit');
    if (!appBunit) {
        g_form.showFieldMsg('app_business_unit', 'Business unit must be updated within APM# before a CIR submission.', 'error');
        valid = false;
    }

    var drTier = g_form.getValue('dr_tier');
    if (!drTier) {
        g_form.showFieldMsg('dr_tier', 'Dr tier must be updated within APM# before a CIR submission.', 'error');
        valid = false;
    }

    return valid; // only submit if all fields are valid
}