Onsubmit client script not working in Service Portal

reddy8055
Tera Contributor

Hi,

Client Script onsubmit is not working in service portal and its working fine in CMS view.

 

function onSubmit() {
//Type appropriate comment here, and begin script below
var today = new Date();
var domain = g_form.getValue('domain');
var mrvs = g_form.getValue('vta_training_details'); // internal name of your MRVS
var total = 0;
// alert(mrvs);
var obj = JSON.parse(mrvs);
if ((obj.length == 0) && (domain == 'AD.COM')) {
g_form.addErrorMessage("No Certifications");
return false;
} else if (obj.length < 4 && (domain == 'nonprod.int') || (domain == 'int')) {
g_form.addErrorMessage("Please complete remaining courses");
return false;
} else {
for (var i = 0; i < obj.length; i++) {
var startDTe = obj[i].course_completed_date; // name of the MRVS variable
total = total + parseInt(obj[i]).course_completed_date; // give here the variable name for proportion
var startDateNum = getDateFromFormat(startDTe, g_user_date_format);
var coursedate = today - startDateNum;
//alert(coursedate);
var dayDifference = coursedate / (1000 * 60 * 60 * 24);
//alert(dayDifference);
if (dayDifference > 365) {
g_form.addErrorMessage("Course Completed Date should not be more than 365 days");
return false;
}
}
}
}

 

Thanks,

9 REPLIES 9

chrisperry
Giga Sage

Hi there,


Please ensure that the UI Type field on your onSubmit client script is set to All:

chrisperry_0-1671214437033.png

If this answer is helpful please mark correct and helpful!

Regards,
Chris Perry

Hi,

Yes, UI type is set to ALL. But its still not working.

Thanks,

Ok -- try re-writing your script as below:

 

function onSubmit() {
    //Type appropriate comment here, and begin script below
    if (g_scratchpad.isFormValid) {
        return true;
    }

    var today = new Date();
    var domain = g_form.getValue('domain');
    var mrvs = g_form.getValue('vta_training_details'); // internal name of your MRVS
    var total = 0;
    // alert(mrvs);
    var obj = JSON.parse(mrvs);
    if ((obj.length == 0) && (domain == 'AD.COM')) {
        g_form.addErrorMessage("No Certifications");
        return false;
    } else if (obj.length < 4 && (domain == 'nonprod.int') || (domain == 'int')) {
        g_form.addErrorMessage("Please complete remaining courses");
        return false;
    } else {
        for (var i = 0; i < obj.length; i++) {
            var startDTe = obj[i].course_completed_date; // name of the MRVS variable
            total = total + parseInt(obj[i]).course_completed_date; // give here the variable name for proportion
            var startDateNum = getDateFromFormat(startDTe, g_user_date_format);
            var coursedate = today - startDateNum;
            //alert(coursedate);
            var dayDifference = coursedate / (1000 * 60 * 60 * 24);
            //alert(dayDifference);
            if (dayDifference > 365) {
                g_form.addErrorMessage("Course Completed Date should not be more than 365 days");
                return false;
            }
        }
    }
    g_scratchpad.isFormValid = true;
    g_form.submit(g_form.getActionName());
}
If this answer is helpful please mark correct and helpful!

Regards,
Chris Perry

Tried this, its not working