Unable to perform GlideRecord in Scripts Include

Robin16
Kilo Explorer

I am a total newbie and I am learning to develop on Servicenow. I am creating a new app. It is a basic app that lets user apply for a job. The following is what I have done:

  1. Created a table called job to insert job listing
  2. Created a table called users to insert users. The field email is unique
  3. Create a M2M table that record which job has been applied to by users
  4. I have created a record producer that enters the value into the M2M table

What I am willing to do with the record producer:

  1. When an email is entered, a client script is called (onChange), it then checks for the email in the users database, if the user exists then populate the form automatically (with name, location and cv link).
  2. If the user still decides to change the user information then (onSubmit):
    1. Check for the email in the users table and then update with the new information if user exists
    2. if user doesnt exist create a new record for user and the M2M relation table
    3. if user exits, then check if the user has previously applied for a job. If applied then dont insert in the M2M field

Problems:

  1. The onChange responseXML returns null
  2. User record is never updated or created
  3. The relation field for User in the M2M remains empty but an entry is created with the job ID

The code:

The record producer client script for populating email field:

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

            //Type appropriate comment here, and begin script below
            var userEmail = newValue.toString().trim().toLowerCase();
            jslog('RobinFromCatalogClientScript:The user email is ' + userEmail);

            function validateEmail(email) {
                var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
                return re.test(email);
            }

            function XMLresponse(response) {
                var answer = response.responseXML.documentElement.getAttribute("answer");;
                if (answer.length > 1) {
                    g_form.addInfoMessage('Your email address already exists in our system. Submitting now will update your information. We have populated your exisiting information');
                    g_form.setValue('user_name', answer[0]);
                    g_form.setValue('user_location', answer[1]);
                    g_form.setValue('user_cv', answer[2]);
                }
            }
            if (validateEmail(userEmail)) {
                jslog('ClientScript: We are in the conditional');
                var ga = new GlideAjax('jobUtils');
                ga.addParam('sysparm_name', 'checkEmail');
                ga.addParam('sysparm_catalog_check_email', userEmail);
                ga.getXML(XMLresponse);
            }

        }

 

The record producer client script for submission

function onSubmit() {
   //Type appropriate comment here, and begin script below
	var email  = g_form.getValue('user_email');
	var location = g_form.getValue('user_location');
	var name = g_form.getValue('user_name');
	var cv = g_form.getValue('user_cv');
	var jobApplied = g_form.getValue('job');
	var ga = new GlideAjax('jobUtils');
        ga.addParam('sysparm_name', 'submitCatalogForm');
		ga.addParam('sysparm_catalog_submit_email', email);
		ga.addParam('sysparm_catalog_submit_name', name);
		ga.addParam('sysparm_catalog_submit_location', location);
		ga.addParam('sysparm_catalog_submit_cv', cv);
		ga.addParam('sysparm_catalog_submit_job', jobApplied);
		ga.getXML();	
}

 

The Script Include:

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

    createJob: function(){
		//var date = new GlideDateTime();
        var jobTitle = this.getParameter('sysparm_jobTitle');
        var jobDesription = this.getParameter('sysparm_jobDesription');
        var jobLocation = this.getParameter('sysparm_jobLocation');
        var jobSalary = this.getParameter('sysparm_jobSalary');
        var jobCompany = this.getParameter('sysparm_jobCompany');
        var jobContract = this.getParameter('sysparm_jobContract');
        //var startDate = date.setValue(this.getParameter('startDate'));
       // var endDate = date.setValue(this.getParameter('endDate'));
		var startDate = this.getParameter('sysparm_start_date');
        var endDate = this.getParameter('sysparm_end_date');
		

        // insert the job
        var newJob = new GlideRecord('x_402812_job_app_2_job');
        newJob.newRecord();
        newJob.u_job_title = jobTitle;
        newJob.u_job_description = jobDesription;
        newJob.u_job_location = jobLocation;
        newJob.u_job_salary = jobSalary;
        newJob.u_job_companyid = jobCompany;
        newJob.u_job_contract = jobContract;
        newJob.u_job_startdate = startDate;
        newJob.u_job_enddate = endDate;

        var newJobNumber = newJob.number.getDisplayValue();
        var sysID = newJob.insert();
        var link = newJob.getLink();
        var err = newJob.getLastErrorMessage();

        if (err !== null){
            return err;
        }else {
            return jobTitle + "|" + newJobNumber + "|" + link;
        }


    },
	
	checkEmail: function(){
		var emailToCheck = this.getParameter('sysparm_catalog_check_email');
		var userName;
		var userLocation;
		var userCv;
		//var responseString;
		console.log('Reached the util script.');
		var ur = new GlideRecord('x_402812_job_app_2_user'); //User Record
		var duplicate = ur.get('u_user_email', emailToCheck);
		if (duplicate){
			ur.addQuery('u_user_email','=', emailToCheck);
			ur.query();
			while(ur.next()){
				userName = ur.getValue('u_user_name');
				userLocation = ur.getValue('u_user_location');
				userCv = ur.getValue('u_user_cv');
			}
			jslog(userName + "|" + userLocation + "|" + userCv);
			return userName + "|" + userLocation + "|" + userCv;
			//return userName + "|" + userLocation + "|" + userCv;
			
			//return ur.getLastErrorMessage();
		}
		return ur.getLastErrorMessage();
	},
	
	submitCatalogForm: function(){
		var email = ga.getParameter('sysparm_catalog_submit_email', email);
		var name = ga.getParameter('sysparm_catalog_submit_name', name);
		var location = ga.getParameter('sysparm_catalog_submit_location', location);
		var cv = ga.getParameter('sysparm_catalog_submit_cv', cv);
		var jobApplied = ga.getParameter('sysparm_catalog_submit_job', jobApplied);
		var ur = new GlideRecord('x_402812_job_app_2_user'); //User Record
		var m2m = new GlideRecord('x_402812_job_app_2_m2m_users_jobs'); // many to many relationship record
		var duplicate = ur.get('u_user_email', email);
	if (!duplicate){
		ur.newRecord();
		ur.u_user_name = name;
		ur.u_user_location = location;
		ur.u_user_cv = cv;
		ur.u_user_email = email;
		var sysID = ur.insert();
        var link = ur.getLink();
		jslog('Your record has been successfully created. Please <a href ="'+ link + '">Click here</a> to view your record');
		
		
		m2m.newRecord();
		m2m.job = jobApplied;
		m2m.user = sysID;
		m2m.insert();
		jslog('You have successfully applied for this job.');
		
		return 'New Record Entered';
		
		
	} else {
		
		jslog('This email exists in our system and your data is updated with the new information');
		ur.u_user_name = name;
		ur.u_user_location = location;
		ur.u_user_cv = cv;
		ur.update();
		
		var userId = ur.getValue('sys-id');		
		var alreadyApplied = m2m.addQuery('user', userId);
		alreadyApplied.addQuery('job', jobApplied);
		if(alreadyApplied.hasNext()){
			jslog('You have already applied for this job');
			return 'Already applied for the job. User Updated';
		} else {
			m2m.newrecord();
			m2m.job = jobApplied;
			m2m.user = userID;
			m2m.insert();
			return 'User update and applied for the job';
		}
	}
	},

    type: 'jobUtils'
});

 

Any help is greatly appreciated.

 

Thanks in advance!!

1 ACCEPTED SOLUTION

Mike Patel
Tera Sage

for # 1

Lets change the SI to below

checkEmail: function(){
		var emailToCheck = this.getParameter('sysparm_catalog_check_email');
		var userName;
		var userLocation;
		var userCv;

		var ur = new GlideRecord('x_402812_job_app_2_user'); //User Record
		ur.addQuery('u_user_email','=', emailToCheck);
		ur.query();
		if(ur.next()){
			userName = ur.getValue('u_user_name');
			userLocation = ur.getValue('u_user_location');
			userCv = ur.getValue('u_user_cv');
			return userName + "|" + userLocation + "|" + userCv;
		}else{
			return "No reord found";
		}
	},

View solution in original post

2 REPLIES 2

Mike Patel
Tera Sage

for # 1

Lets change the SI to below

checkEmail: function(){
		var emailToCheck = this.getParameter('sysparm_catalog_check_email');
		var userName;
		var userLocation;
		var userCv;

		var ur = new GlideRecord('x_402812_job_app_2_user'); //User Record
		ur.addQuery('u_user_email','=', emailToCheck);
		ur.query();
		if(ur.next()){
			userName = ur.getValue('u_user_name');
			userLocation = ur.getValue('u_user_location');
			userCv = ur.getValue('u_user_cv');
			return userName + "|" + userLocation + "|" + userCv;
		}else{
			return "No reord found";
		}
	},

Thank you Mike!