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

Dan H
Tera Guru

Hi,

Try adding .toString() in the script include after you assign the value.

 

AG = grAssignmentRule.getDisplayValue('group').toString();

 

Hope this helps.

 

Please mark my answer as Correct/Helpful based on impact

Regards,

Dan H

 

Hi @Dan H that did not help unfortunately

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Is the script include client callable?

what comes in alert for response?

why are you using JSON parser when you are returning only 1 value?

is your assignment_group field/variable a string or reference?

If string then use this

AG = grAssignmentRule.getDisplayValue('group');

If reference then use this

AG = grAssignmentRule.group;

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){
        g_form.showFieldMsg('assignment_group',response,'info');
    }
}

Regards
Ankur

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

HI @Ankur Bawiskar hope your well. You have been missed.

I get null in alert in the console.

the assignment group is a reference field.

I updated the script as suggested but still getting same issue.

In the logs getting undefined when I do gs.info for the variable AG and in the client side getting null

So something is not right.
See below my updated code.

Script include:

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

client script:

	function getResponse(response){
		g_form.showFieldMsg('assignment_group',response,'info');
	
	
	}