Script Include help

huyjor
Tera Contributor

Hello Team

I'm trying to write a script include & client script to validate the duplicate enrollment record. If it is duplicated, it should prompt for an error when I click on submit button. This is on catalog item. I tried to test it but when i clicked on submit, nothing happened. The script seems to be ok. I'm not sure where is my mistake at. Thanks for your help 

Script include below:

var SNO_AC_ku_enroll_validation = Class.create();
SNO_AC_ku_enroll_validation.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
initialize: function() {},
isAlreadyRegistered: function(studentid, courseId) {
var courseEnrollment = new GlideRecord('x_kb_ku_enrollment');
courseEnrollment.addQuery('studentid', username); //studentid field is on ku_enrollment table
courseEnrollment.addQuery('courseid', courseid); // courseid field is on ku_enrollment table
courseEnrollment.query();
return courseEnrollment.hasNext();

},
type: 'SNO_AC_ku_enroll_validation'
});

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

Catalog Client Script: 

function onSubmit() {
var studentId = g_form.getValue('studentid');
var courseid = g_form.getValue('courseid');

var enrollment_validate = new SNO_KU_enroll_validation();
var isDuplicate = enrollment_validate.isAlreadyRegistered(studentid, courseid);

if (isDuplicate) {
alert("You are already enrolled in this course");
return false;
}
return true;
}

8 REPLIES 8

Sumanth16
Kilo Patron

Hi @huyjor ,

Please update script according to your requirement :

 

//Client Script
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var dup = new GlideAjax('global.duplicate_time_sheet');
    dup.addParam('sysparm_name', 'checkPrerequisite');
    dup.addParam('sysparm_week', g_form.getDisplayValue('week_starts_on'));
    dup.addParam('sysparm_task', g_form.getValue('task'));
    dup.addParam('sysparm_user', g_form.getValue('user'));

    dup.getXMLAnswer(getResponse);

    function getResponse(response) {
        var res = response;
        if (res == 'duplicate') {
            alert('Duplicate dont submit');
        } else if (res == 'not_duplicate') {
            alert('Submit the form');
        }

        //Type appropriate comment here, and begin script below

    }
}


//Script Include
var duplicate_time_sheet = Class.create();
duplicate_time_sheet.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    checkPrerequisite: function() {
        var time = this.getParameter('sysparm_week');
        var task = this.getParameter('sysparm_task');
        var user = this.getParameter('sysparm_user');
        var sd = "week_starts_onON" + time + "@javascript:gs.dateGenerate('" + time + "','start')@javascript:gs.dateGenerate('" + time + "','end')";
        var gr = new GlideRecord('time_card');

        gr.addEncodedQuery(sd + "^task=" + task + "^user=" + user);

        gr.query();
        if (gr.next()) {
            //alert('duplicate Entry');
            return 'duplicate';
        } else {
            return 'not_duplicate';
        }
    },
    type: 'duplicate_time_sheet'
});

 

 

If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!

 

Thanks & Regards,

Sumanth Meda

 

huyjor
Tera Contributor

Thanks for helping me. It still not doing anything. For the gliderecord in the script include. I don't know if i query it is right or not. I just want to query those 2 fields studentid & courseid. Do i have to set the value as well. 

 

Thanks 

 

Script Include

var SNO_AC_ku_duplicate_enrollment = Class.create();
SNO_AC_ku_duplicate_enrollment.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    checkPrerequisite: function() {
        var studentid = this.getParameter('sysparm_studentid');
        var courseid = this.getParameter('sysparm_courseid');
        var gr = new GlideRecord('x_74571_kb_univ_ku_enrollment');
        gr.addQuery('studentid');
        gr.addQuery('courseid');
        gr.query();
        if(gr.next())
{
return 'duplicate';
} else {
return 'not_duplicate';
}
 
    },
    type: 'SNO_AC_ku_duplicate_enrollment'
});
 
Client Script 

function onSubmit() {
//Type appropriate comment here, and begin script below
var dupEnroll = new GlideAjax('var SNO_AC_ku_duplicate_enrollment');
dupEnroll.addParam('sysparm_name', 'checkPrerequisite');
dupEnroll.addParam('sysparm_studentid', g_form.getValue('studentid'));
dupEnroll.addParam('sysparm_studentid', g_form.getValue('courseid'));

dupEnroll.getXMLAnswer(getResponse);

function getResponse(response) {
var res = response;
if (res == 'duplicate') {
alert('You already registred for this course. Please register another course');
} else if (res == 'not_duplicate') {
alert('Please go head submit the form');
}
}
}

Hi @huyjor ,

 

Change the query like below:

 

 

        gr.addEncodedQuery( "student_id="+student id+^+"courseid=" + courseid);//change the field names in the encodedquery according to your field names on table x_74571_kb_univ_ku_enrollment

 

 

If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!

 

Thanks & Regards,

Sumanth Meda

 

huyjor
Tera Contributor

ok, i did change it but i'm getting the error below. 

The 2 field names that i have on 

on table x_74571_kb_univ_ku_enrollment

student_id

courseid

 

huyjor_0-1710185929555.png