Script include function not working when called from client side

Khalnayak
Tera Guru

Hi all,

I have a record producer on which I want to show the assignment group the HR case will be assigned to.

SO I have a script include within which I am calling a function and then using a onchange client side script to call the script include and display the group in the item.

This is the code for the getAG function in the script include.

getAG: function(){
		//var current = this.getParameter('sysparm_current');
		gs.debug('HR AG');
		var subjectPerson = this.getParameter('sysparm_req_for');
		var prodID = this.getParameter('sysparm_prodID');
		var contactType = 'Self Service';
		//var source = '[producer.source]';
		var AG = '';


		var grHrService = new GlideRecord('sn_hr_core_service');
		grHrService.get('producer', prodId);

		var currentServiceTable  = grHrService.getValue('service_table');
		var currentTopicCategory = grHrService.topic_detail.topic_category.getDisplayValue();
		var currentTopicDetail   = grHrService.topic_detail.getDisplayValue();
		var currentHRServiceName = grHrService.getDisplayValue('name');


				var grSubjectPerson = new GlideRecord('sys_user');
				grSubjectPerson.get(subjectPerson);

				var currentLocation = grSubjectPerson.getValue('location');

				var grCase = new GlideRecord(currentServiceTable);
				grCase.setValue('hr_service',     HrService);
				grCase.setValue('contact_type',   contactType);
				grCase.setValue('source',         source);
				grCase.setValue('subject_person', subjectPerson);
				grCase.setValue('location',       currentLocation);


				var grAssignmentRule = new GlideRecord('sysrule_assignment');
				grAssignmentRule.addActiveQuery();
				grAssignmentRule.addQuery('table', '=', currentServiceTable);
				grAssignmentRule.orderBy('order');
				grAssignmentRule.query();

				var filter;
				var found = false;

				while ( grAssignmentRule.next() && found == false ) {

					filter = grAssignmentRule.getValue('condition');

					if ( filter == '' ) {
						continue;
					}

					found = GlideFilter.checkRecord(grCase, filter, true);

					if ( found ) {
						gs.info('HR AG match: ' + grAssignmentRule.getDisplayValue('group'));
						gs.log('HR AG match: ' + group);
						AG = grAssignmentRule.group;
						return AG;
					}

				}

				if ( found == false) {
					gs.info('HR AG: no match');
				}},

 

and then this is the client script that is calling the script include function:

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
	
	var ga = new GlideAjax('sn_hr_core.HRCaseAccessRP');
	ga.addParam('sysparm_name', 'getAG');
	ga.addParam('sysparm_req_for', producer.requested_for);
	ga.addParam('sysparm_prodID', g_form.getUniqueValue());
	
	ga.getXMLAnswer(getResponse);

	function getResponse(response){
		var answer = JSON.parse(response);
		g_form.addInfoMessage(answer.AG);
		g_form.setValue('assignment_group', answer.AG);
	}


   //Type appropriate comment here, and begin script below
   
}

 

so the onchange client script works in the sense it displays the showfield message banner so the blue bit under the field. But there is no data or group shown in the field message. it is blank.

find_real_file.png

I have added some gs.logs within the script include function I am calling but they are not displaying in the logs at all.

So looks like script include is not being called.

The script include is in the HR core app scope and the client script is in the employee center core scope.

The script include caller access is set to none so that should not restrict and accessible from is all app scopes.

Can someone help me on this please.

 

1 ACCEPTED SOLUTION

Kailash Bhange
Kilo Sage
Kilo Sage

Hi Khalnayak,

I suppose your code is failing at below highlighted lines as the variable defined is different one here, please make corrections.

 

find_real_file.png

 

Hope this helps.
If my answer resolves your issue, please mark my answer as ✅ Correct & Helpful based on the validations.

Thank You!
Regards,
Kailash

View solution in original post

17 REPLIES 17

Maik Skoddow
Tera Patron
Tera Patron

Hi

please share the complete code of the Script Include. 

Kind regards
Maik

@Maik Skoddow see below

var HRCaseAccessRP = Class.create();
HRCaseAccessRP.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
	
	getHrServiceId: function() {
		var idRecordProducer = this.getParameter('sysparm_idRecordProducer');
		
		var response = {};
		
		var hrServicesUtil = new sn_hr_core.hr_ServicesUtil();
		response.idHrService = hrServicesUtil.getServiceSysIdByProducerId(idRecordProducer);
		
		return JSON.stringify(response);
	},
	
	getUserHrAttributes: function() {
		var idUser      = this.getParameter('sysparm_idUser');
		var detailsFor  = this.getParameter('sysparm_detailsFor');
		var idHrService = this.getParameter('sysparm_idHrService');

		var response = {};
		
		if ( detailsFor == 'opened_by' ) {
			response.isLineManager       = this.checkIsLineManager(idUser);
			response.isRedStatusUser     = this.checkIsRedStatusUser(idUser);
		}
		
		var grUser = this.getUser(idUser);
		
		response.isHrTeamMember      = this.checkIsHrTeamMember(grUser);
		response.isHrBusinessPartner = this.checkIsHrBusinessPartner(grUser);
		
		var hrCaseCreation = new sn_hr_core.hr_CaseCreation();
		response.isHrServiceValid = hrCaseCreation.hasAccessForService(idUser, true, idHrService);
		
		var hrCaseAccess = new sn_hr_core.HRCaseAccess();
		var restrictedCountries = hrCaseAccess.restrictedCountries();
		var userCountry = grUser.location.country.toString();
		var isCountryRestricted = ( restrictedCountries.indexOf(userCountry) > -1 );
		
		if ( isCountryRestricted ) {
			response.countryIfRestricted = userCountry;
		} else {
			response.countryIfRestricted = '';
		}
		
		return JSON.stringify(response);
	},
	
	checkIsLineManager: function(idUser) {
		var isLineManager = false;
		
		var grUser = new GlideRecord('sys_user');
		grUser.addActiveQuery();
		grUser.addQuery('manager', idUser);
		grUser.setLimit(1);
		grUser.query();
		
		if ( grUser.next() ) {
			isLineManager = true;
		}
		
		return isLineManager;
	},
	
	checkIsHrTeamMember: function(grUser) {
		var isHrTeamMember = false;
		
		if ( grUser.department.id != '45855953' ) { // HR Business Partnering
		
			if ( grUser.department.parent.id == '45855240' ) { // Human Resources
				isHrTeamMember = true;
			}
			
		}
		
		return isHrTeamMember;
	},
	
	checkIsHrBusinessPartner: function(grUser) {
		var isHrBusinessPartner = false;
		
		if ( grUser.department.id == '45855953' ) { // HR Business Partnering
			isHrBusinessPartner = true;
		}
		
		return isHrBusinessPartner;
	},
	
	checkIsRedStatusUser: function(idUser) {
		var isRedStatusUser = false;
		
		var idGroupTypeHrRedStatusProject = gs.getProperty('sn_hr_core.GroupTypeHRRed');
		
		var grRedStatus = new GlideRecord('sys_user_grmember');
		grRedStatus.addQuery('user', idUser);
		grRedStatus.addQuery('group.type', 'CONTAINS', idGroupTypeHrRedStatusProject);
		grRedStatus.addQuery('group.active', 'true');
		grRedStatus.setLimit(1);
		grRedStatus.query();
		
		if ( grRedStatus.next() ) {
			isRedStatusUser = true;
		}
		
		return isRedStatusUser;
	},
	
	getUser: function(idUser) {
		var grUser = new GlideRecord('sys_user');
		grUser.get(idUser);
		return grUser;
	},
	
	getAG: function(){
		//var current = this.getParameter('sysparm_current');
		gs.debug('HR AG');
		var subjectPerson = this.getParameter('sysparm_req_for');
		var prodID = this.getParameter('sysparm_prodID');
		var contactType = 'Self Service';
		//var source = '[producer.source]';
		var AG = '';


		var grHrService = new GlideRecord('sn_hr_core_service');
		grHrService.get('producer', prodId);

		var currentServiceTable  = grHrService.getValue('service_table');
		var currentTopicCategory = grHrService.topic_detail.topic_category.getDisplayValue();
		var currentTopicDetail   = grHrService.topic_detail.getDisplayValue();
		var currentHRServiceName = grHrService.getDisplayValue('name');


				var grSubjectPerson = new GlideRecord('sys_user');
				grSubjectPerson.get(subjectPerson);

				var currentLocation = grSubjectPerson.getValue('location');

				var grCase = new GlideRecord(currentServiceTable);
				grCase.setValue('hr_service',     HrService);
				grCase.setValue('contact_type',   contactType);
				grCase.setValue('source',         source);
				grCase.setValue('subject_person', subjectPerson);
				grCase.setValue('location',       currentLocation);


				var grAssignmentRule = new GlideRecord('sysrule_assignment');
				grAssignmentRule.addActiveQuery();
				grAssignmentRule.addQuery('table', '=', currentServiceTable);
				grAssignmentRule.orderBy('order');
				grAssignmentRule.query();

				var filter;
				var found = false;
gs.log('HR AG: Outside of while loop');
				while ( grAssignmentRule.next() && found == false ) {
gs.log('HR AG: Inside of while loop');
					filter = grAssignmentRule.getValue('condition');

					if ( filter == '' ) {
						continue;
					}

					found = GlideFilter.checkRecord(grCase, filter, true);

					if ( found ) {
						gs.info('HR AG match: ' + grAssignmentRule.getDisplayValue('group'));
						gs.log('HR AG match: ' + group);
						AG = grAssignmentRule.group;
						return AG;
					}

				}

				if ( found == false) {
					gs.info('HR AG: no match');
				}},

	
	//return true if user is a member of a red status project
	userinRedStatus: function() {
		
		var idGroupTypeHrRedStatusProject = gs.getProperty('sn_hr_core.GroupTypeHRRed');
		
		var grRedStatus = new GlideRecord('sys_user_grmember');
		grRedStatus.addQuery('user', gs.getUserID());
		grRedStatus.addQuery('group.type', 'CONTAINS', idGroupTypeHrRedStatusProject);
		grRedStatus.addQuery('group.active', 'true');
		grRedStatus.setLimit(1);
		grRedStatus.query();
		
		if (grRedStatus.next() ) {
			gs.info('HR Red Group: true');
			return true;
		}
		else {
			gs.info('HR Red Group: false');
			return false;
		}},
	
	
	
    type: 'HRCaseAccessRP'
});

Hi @Khalnayak 

Thanks for the complete code.

On the one hand I see a lot of gs.info() logging and I would like to know whether you could see in which part of your script the processing is stuck.

On the other hand you have no try-catch-blocks and therefore you cannot know whether any implementation errors break the processing.

And you have issues in your code. @Kailash Bhange pointed out one error and another error is

grCase.setValue('hr_service',     HrService);

as the variable HrService was not declared before.

You should first improve your code, remove the programming errors and perform debug sessions to fix the issues. Then you can ask the Community.

Kind regards
Maik

Thanks @Maik Skoddow can you help me with how I can add try catch blocks?

also what is best way to debug my code