Fetch values for business_service

vidhya_mouli
Giga Sage

When I choose a value for assignment_group, I want to fetch the values for business_service

 

Script Include:

 

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

    getServiceNames: function() {
		var supportGroupId = this.getParameter('support_group_id');
		// gs.addInfoMessage('af53b01dc30fca10e08896c9050131d7 KN, ' + supportGroupId);
        var serviceNames = [];	
        var gr = new GlideRecord('cmdb_ci_service');
        gr.addQuery('support_group', supportGroupId);
        gr.query();
        
        while (gr.next()) {
            serviceNames.push(gr.getValue('name'));
        }
	gs.addInfoMessage('script incl  ' + serviceNames);
        return JSON.stringify(serviceNames);
    },

type: 'GetServiceNamesBySupportGroup'
});

 

Client Script:

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

    
    var supportGroupId = newValue;
var ga = new GlideAjax('GetServiceNamesBySupportGroup');
    ga.addParam('sysparm_name', 'getServiceNames');
    ga.addParam('support_group_id', supportGroupId);
       ga.getXMLAnswer(function(response) {
        // Parse the response as a JSON string
        var jsonResponse = JSON.parse(response);
        
        if (jsonResponse.length > 0) {
            g_form.addInfoMessage('Service Names: ' + jsonResponse.join(', '));
            populateBusinessServiceField(jsonResponse);
        } else {
            g_form.addInfoMessage('No services found for the given support group.');
        }
    });

    function populateBusinessServiceField(serviceNames) {
        serviceNames.forEach(function(name) {
            g_form.addOption('business_service', name, name);
        });
    }


}

  

Script include return the values as expected. But I am not able to show those values in business_service field (this is a reference field). Any help?

2 REPLIES 2

swathisarang98
Giga Sage
Giga Sage

Hi @vidhya_mouli ,

 

as business_service is  a reference field you can not set the field value with name you have to return sysid from script include an if there are multiple values coming then you can not set the reference field you can set multiple values if it is List collector.

 

you have to write a advanced reference qualifier and script include to return the syids of business services 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang

Bert_c1
Kilo Patron

Hi vidhya,

 

I see 21 fields named busines_service in my PDI, only 3 are Strings. As stated by Swathi, if your business_service field is Reference field you need to return a sys_id. You need to specify which table the Client script is defined on.  Also, add some debug to the client script to see what value you have for 'servicenames' in the populateBusinessServiceField() function.

 

But I see you're trying to build a choice list for a field named 'business_service'. Again, what table is that defined on? What table and field is your client script defined for?