Auto Populate Support Group

vidhya_mouli
Giga Sage

I have to auto populate service_offering.support_group in my incident table when business_service is selected.

 

INC0010889-Incident-ServiceNow.png

 

Following is my script include (AutoPopulateSupportGroup):

 

var AutoPopulateSupportGroup = Class.create();
AutoPopulateSupportGroup.prototype = {
    initialize: function() {},

    populateSupportGroup: function(businessService) {
        // Create a GlideRecord query to find the support group
        var gr = new GlideRecord('service_offering');
        gr.addQuery('name', businessService);
        gr.query();

        if (gr.next()) {
            // Return the name of the support group
            return gr.getValue('support_group');
        } else {
            // Return an empty string if no matching support group is found
            return '';
        }
    },

    type: 'AutoPopulateSupportGroup'
};

 

And this is the client script:

 

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

    // Get the selected business service
    var businessService = newValue;

    // Create an instance of the Script Include
    var autoPopulate = new AutoPopulateSupportGroup();

    // Populate the 'support_group' field with the retrieved support group
    g_form.setValue('service_offering.support_group', autoPopulate.populateSupportGroup(businessService));
}

 

 

How ever I get this error when I choose the value for the  business_service .

 

onChange script error: ReferenceError: AutoPopulateSupportGroup is not defined function () { [native code] }

 

 

What am I doing wrong?

1 ACCEPTED SOLUTION

Siva Jyothi M
Mega Sage

Hi @vidhya_mouli,

Script Include:

 

var populateaddress = Class.create();
populateaddress.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    demo: function() {
        var id= this.getParameter('sysparm_company');
        var gr = new GlideRecord('service_offering');
        gr.addQuery('sys_id', id);
        gr.query();
        if (gr.next()) {
             var ans = gr.support_group;
            return ans;
        }
    },
type: 'populateaddress'
});

 

Client Script:

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ga = new GlideAjax('populateaddress');
    ga.addParam('sysparm_name', 'demo');
    var com = g_form.getValue('business_service');
    ga.addParam('sysparm_company', com);
    ga.getXML(callBackFunction);
    function callBackFunction(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
		g_form.setValue('support_group', answer);

    }
}

Please mark this answer as correct and helpful if it solves your issue.

 

Regards,

Siva Jyothi M.

 

 

 

View solution in original post

3 REPLIES 3

pablo_itguy_pl
Mega Guru

You are trying to communicate from Client Side to Server Side - please try using GlideAjax.

Sandeep Rajput
Tera Patron
Tera Patron

@vidhya_mouli First of all, you need to use GlideAjax to call server side script include inside the client script. 

 

var ga = new GlideAjax('HelloWorld'); 

 

Secondly, you need to specify the method name while making the Ajax call as follows.

 

ga.addParam('sysparm_name','helloWorld');

 

You need to specify the parameter to pass inside the helloWorld method as follows.

 

ga.addParam('sysparm_user_name',"Bob"); // Set parameter sysparm_user_name to 'Bob' 

 

Here is how you should process the response.

 

ga.getXML(HelloWorldParse);  /* Call HelloWorld.helloWorld() with the parameter sysparm_user_name set to 'Bob' 
      and use the callback function HelloWorldParse() to return the result when ready */

// the callback function for returning the result from the server-side code
function HelloWorldParse(response) {  
   var answer = response.responseXML.documentElement.getAttribute("answer"); 
    alert(answer);
}

 

 

Source: https://docs.servicenow.com/bundle/rome-application-development/page/app-store/dev_portal/API_refere...

 

Hope this helps.

Siva Jyothi M
Mega Sage

Hi @vidhya_mouli,

Script Include:

 

var populateaddress = Class.create();
populateaddress.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    demo: function() {
        var id= this.getParameter('sysparm_company');
        var gr = new GlideRecord('service_offering');
        gr.addQuery('sys_id', id);
        gr.query();
        if (gr.next()) {
             var ans = gr.support_group;
            return ans;
        }
    },
type: 'populateaddress'
});

 

Client Script:

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ga = new GlideAjax('populateaddress');
    ga.addParam('sysparm_name', 'demo');
    var com = g_form.getValue('business_service');
    ga.addParam('sysparm_company', com);
    ga.getXML(callBackFunction);
    function callBackFunction(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
		g_form.setValue('support_group', answer);

    }
}

Please mark this answer as correct and helpful if it solves your issue.

 

Regards,

Siva Jyothi M.