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

Change line 7 to

		gr.addEncodedQuery("student_id=" + studentid + "^courseid=" + courseid);

I'm able to fix it error line 7. But when I clicked on the submit button. It still not giving me the alert. If the value of the student_id & courseid fields already are existing the table. It should give the alert for duplicate record. 

James Chun
Kilo Patron

Hey @huyjor,

 

Try the following, if it still doesn't work, I recommend adding more logs and doing some debugging:

 

Script Include:

var SNO_AC_ku_duplicate_enrollment = Class.create();
SNO_AC_ku_duplicate_enrollment.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
	initialize: function() {},
    checkPrerequisite: function() {

		gs.info('[Testing Client Script] Starting of checkPrerequisite');

        var studentid = this.getParameter('sysparm_studentid');
		
		gs.info('[Testing Client Script] studentid is ' + studentid);

        var courseid = this.getParameter('sysparm_courseid');

		gs.info('[Testing Client Script] courseid is ' + courseid);

        var courseEnrollment = new GlideRecord('x_kb_ku_enrollment');
        courseEnrollment.addQuery('studentid', studentid); //studentid field is on ku_enrollment table
        courseEnrollment.addQuery('courseid', courseid); // courseid field is on ku_enrollment table

		gs.info('[Testing Client Script] query is ' + courseEnrollment.getEncodedQuery());

        courseEnrollment.query();

		gs.info('[Testing Client Script] has next is ' + courseEnrollment.hasNext());

		return courseEnrollment.hasNext();

    },
    type: 'SNO_AC_ku_duplicate_enrollment'
});

 

Client Script:

function onSubmit() {
    //Type appropriate comment here, and begin script below
    var dupEnroll = new GlideAjax('SNO_AC_ku_duplicate_enrollment'); //typo here
    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 answer = response.responseXML.documentElement.getAttribute("answer");
        if(answer == 'true' || answer == true)
		{
			alert('True returned');
		}else
		{
			alert('False returned');
		}
    }
}

 

Another thing to note is that you won't be able to prevent submission using AJAX.

There are heaps of workarounds for these (e.g. creating a hidden variable and setting it true/false via onChange) or also this - https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0783579

 

Cheers

Sohithanjan G
Kilo Sage
Kilo Sage

Hi @huyjor , 

 

  1. onSubmit Ajax call will not prevent Form submission
  2. Try to restrict form submisssion either from client side (client script) or server side (before BR)

🙂

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)