The CreatorCon Call for Content is officially open! Get started here.

Script Include returning null?

Sid_Takali
Kilo Patron

Hi all, 

The following Script returning null value. I need consumer sys_id. Thanks in advance 

Script Include - 

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

    ConsumerS: function() {
        var g = this.getParameter('syparm_cons');
        var CONsumer = '';
        gs.info("Include::::" + g);
        var gr1 = new GlideRecord('sn_customerservice_product_service_support');
        gr1.addQuery('sys_id', g);
        gr1.query();
        if (gr1.next()) {
            CONsumer = gr1.consumer;
        }
        return CONsumer;

 },

 

Client Script - 

        var ans = '';
        var con = g_form.getValue('parent');

        var ga1 = new GlideAjax('sn_customerservice.customerPhones');
        ga1.addParam('syparm_name', 'ConsumerS');
        ga1.addParam('syparm_cons', con);
        ga1.getXML(HelloWorld);

        function HelloWorld(response) {
            ans = response.responseXML.documentElement.getAttribute("answer");
            alert(ans);
        }

10 REPLIES 10

Hi,

Share screenshots of both script include and client scripts

Regards
Ankur

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

Srinivas reddy3
Mega Expert

hi

I found the issue in client script, refer corrected script below

Client Script - 

 ga1.addParam('sysparm_name', 'ConsumerS');
 ga1.addParam('sysparm_cons', con);

also if possible use getXMLAnswer instead getXML, will get the answer alone not entire record

ga1.getXMLAnswer(HelloWorld);

        function HelloWorld(response) {
            var ans = response;
            alert(ans);
        }

try to check the script include is scope if your working on the same scope or calling from different scope.

Feel free to mark helpful.

Abhijit4
Mega Sage

You are unnecessary making things complicated by writing glideAjax to just get consumer sys_id. Rather you can use getReference method with callback function. Use below script in client script and it will do job.

var con= g_form.getReference('parent', getConsumer); 

function getConsumer(con) {
alert('consumer sys id = ' + con.consumer);
}

Let me know if you have any further queries.

Please mark this as Correct or Helpful if it helps.

Regards,
Abhijit
Community Rising Star 2022

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

Hi @Abhijit 

he g_form.getReference() method requests the whole Record to the server. It may cause slowness to your Client Side action.

Best way to call from Server data is using GlideAjax API.

Best Regards
Aman Kumar

Hey Aman,

Yes, make sense.

@Siddharam if you need more field from that record then you can use getReference, if you need only one value then its better to go with GlideAjax.

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP