Script include variable value undefined help

Khalnayak
Tera Guru

Hi,

I have a script include which get the group from assignment rules. And then I need to display this group in the record producer form in portal.

The script include works because when I do a gs.info it is showing the group name. but issue I am having is when assigning the returned value to a variable/object it does not seem to store it.

When I do a gs.info on the variable it is saying undefined.

Also in client script not showing group name.

script include function:

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',     currentHRServiceName);
				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.getDisplayValue('group');
						return AG;
		
					}
				}

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

 

client script:

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', g_form.getValue('requested_for'));
	ga.addParam('sysparm_prodID', g_form.getUniqueValue());
	
	ga.getXMLAnswer(getResponse);

	function getResponse(response){
		var answer = JSON.parse(response);
		g_form.showFieldMsg('assignment_group',answer,'info');
	
	}

   
}

 

1 ACCEPTED SOLUTION

Hi,

you didn't share what came in alert for response?

use this

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', g_form.getValue('requested_for'));
    ga.addParam('sysparm_prodID', g_form.getUniqueValue());
    ga.getXMLAnswer(function(answer){
        alert(answer);
        g_form.showFieldMsg('assignment_group',answer,'info');
    });
}

Also share gs.info() for this

if ( found ) {
    AG = grAssignmentRule.group.name;

    gs.info("AG->" + AG);
    return AG;

}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

13 REPLIES 13

Hi,

if you are in scoped app don't use gs.info()

what came in logs?

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi @Ankur Bawiskar, the gs.info logs do appears in the logs when I check. I also get a 'HR Restricted access triggered' in the log but the other logs appear.

Wierd thing is, I just commented out all the gs.info logs in my script include and now I get a a javascript browser error on the record producer in portal.

 

Hi,

so what's coming in alert for response?

whats the browser console error?

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

okay so no error when I comment out gs.info, I get error when I comment out gs.log.

error in console is:

find_real_file.png

The unhandled exception in GlideAjax is the one that is new, the top has been there for a while.

 

And If I uncomment the gs.log then I can see the showfieldmessage but it says null rather than displaying group name.

find_real_file.png

and in the browser console I get null too for logging the response.

Hi,

don't use gs.log() in script include but use gs.info()

Now null means the script include function is not returning the correct field value or the field name is incorrect

use this

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

}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader