how to check data present in one table to another

Are Kaveri
Tera Contributor

Hi Team,

 

we have following requirement.

we are working on Student Admin Form.

we created record producer and target table.

the Data related to Student is stored in Student reference table.

The Student name and Student Roll Number are populating from User table(User name and employee number).

Student class, Student section, Student pass, Student Ranks are stored in Student reference table

The common fields in User table and Student reference table are Student name and Student Roll number.

 

I am facing issue for some of the Student users.

the reference table has not having data of some students when they impersonate they need popup saying Data is missing.

 

I have written below client script and script include but not working when the data of the student is not present on Student referene table.

Onchange of Student name

function onChange(control, oldValue, newValue, isLoading) {
    if (newValue == '') {
        return;
    }

    var ga = new GlideAjax('x_snow_student.StudentUtils');
    ga.addParam('sysparm_name', 'checkStudentData');
    ga.addParam('sysparm_userid', newValue);
    ga.getXMLAnswer(UserDetails);
}

function UserDetails(response) {
    var ans = response;
    var result = JSON.parse(ans);
    g_form.setValue('class', result.Class);
    g_form.setValue('section', result.Section);
if(result.Class == '' || result.Section == '')
{ 
   alert(' Please contact Admin department for further assistance.');
}
}

 

Script include

checkStudentData: function() {

        var userSysID = this.getParameter('sysparm_userid');
 
        var x = '';
        var studentDetails = {};

        var userstu = new GlideRecord('sys_user');
        userstu.addQuery('sys_id', userSysID);
        userstu.query();
        if (userstu.next()) {
            x = userstu.employee_number;
            gs.info("userSysID_2: " + x);
        }

        var userRec = new GlideRecord('x_snow_student_reference_data');
        userRec.addQuery('roll_no', x);
       userRec.query();
       if(userRec.next()){
            gs.info("userSysID_3: " + userRec.roll_no);
            studentDetails.Class = userRec.getValue('class');
            studentDetails.Section = userRec.getValue('section');
            studentDetails.Rank = userRec.getValue('rank');
}
return JSON.stringify(studentDetails);
},

 

 

My problem is when reference data was not present the popup is not coming .

 

Please help me

1 ACCEPTED SOLUTION

Amit Gujarathi
Giga Sage
Giga Sage

HI @Are Kaveri ,
I trust you are doing great.
The client script should properly handle the response when the student's data is missing:

function onChange(control, oldValue, newValue, isLoading) {
    if (newValue === '') {
        return;
    }

    var ga = new GlideAjax('x_snow_student.StudentUtils');
    ga.addParam('sysparm_name', 'checkStudentData');
    ga.addParam('sysparm_userid', newValue);
    ga.getXMLAnswer(UserDetails);
}

function UserDetails(response) {
    if (!response) {
        alert('Data is missing for this student. Please contact the Admin department for further assistance.');
        return;
    }

    var result = JSON.parse(response);
    g_form.setValue('class', result.Class);
    g_form.setValue('section', result.Section);

    if (result.Class === '' || result.Section === '') {
        alert('Some data is missing for this student. Please contact the Admin department for further assistance.');
    }
}

The script include should return null or a similar indicator when the student data is not found:

var StudentUtils = Class.create();
StudentUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    checkStudentData: function() {
        var userSysID = this.getParameter('sysparm_userid');
        var studentDetails = {};

        var userstu = new GlideRecord('sys_user');
        userstu.addQuery('sys_id', userSysID);
        userstu.query();

        if (!userstu.next()) {
            return null;  // No user found
        }

        var rollNo = userstu.employee_number;
        gs.info("User Roll Number: " + rollNo);

        var userRec = new GlideRecord('x_snow_student_reference_data');
        userRec.addQuery('roll_no', rollNo);
        userRec.query();

        if (!userRec.next()) {
            return null;  // No student reference data found
        }

        studentDetails.Class = userRec.getValue('class');
        studentDetails.Section = userRec.getValue('section');
        studentDetails.Rank = userRec.getValue('rank');

        return JSON.stringify(studentDetails);
    },

    type: 'StudentUtils'
});

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



View solution in original post

2 REPLIES 2

Prince Arora
Tera Sage
Tera Sage

@Are Kaveri 

 

Is the roll number present in reference table, where the another metadata is not there?

Can you also add alert as mentioned below and track the result in case of data not present

 

 var ans = response;
alert(ans);

  

Amit Gujarathi
Giga Sage
Giga Sage

HI @Are Kaveri ,
I trust you are doing great.
The client script should properly handle the response when the student's data is missing:

function onChange(control, oldValue, newValue, isLoading) {
    if (newValue === '') {
        return;
    }

    var ga = new GlideAjax('x_snow_student.StudentUtils');
    ga.addParam('sysparm_name', 'checkStudentData');
    ga.addParam('sysparm_userid', newValue);
    ga.getXMLAnswer(UserDetails);
}

function UserDetails(response) {
    if (!response) {
        alert('Data is missing for this student. Please contact the Admin department for further assistance.');
        return;
    }

    var result = JSON.parse(response);
    g_form.setValue('class', result.Class);
    g_form.setValue('section', result.Section);

    if (result.Class === '' || result.Section === '') {
        alert('Some data is missing for this student. Please contact the Admin department for further assistance.');
    }
}

The script include should return null or a similar indicator when the student data is not found:

var StudentUtils = Class.create();
StudentUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    checkStudentData: function() {
        var userSysID = this.getParameter('sysparm_userid');
        var studentDetails = {};

        var userstu = new GlideRecord('sys_user');
        userstu.addQuery('sys_id', userSysID);
        userstu.query();

        if (!userstu.next()) {
            return null;  // No user found
        }

        var rollNo = userstu.employee_number;
        gs.info("User Roll Number: " + rollNo);

        var userRec = new GlideRecord('x_snow_student_reference_data');
        userRec.addQuery('roll_no', rollNo);
        userRec.query();

        if (!userRec.next()) {
            return null;  // No student reference data found
        }

        studentDetails.Class = userRec.getValue('class');
        studentDetails.Section = userRec.getValue('section');
        studentDetails.Rank = userRec.getValue('rank');

        return JSON.stringify(studentDetails);
    },

    type: 'StudentUtils'
});

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi