GlideAjax to set a reference field in catalog item form

Abhishek Barik
Tera Contributor

Hi Team,

We have a requirement to set a reference field on load. We have done it through sys id using set value, but our client has suggested to use it via GlideAjax instead of hardcoded "sys_id". Hence, I have written a on load client script and script include as shown below but its not setting the reference field value and throwing an alert "[object HTMLCollection]" when I try to get the "answer" value.

Client Script:

function onLoad() {
    var gr = new GlideAjax('x_acal_ahc.AHC_getServiceProvider');
    gr.addParam('sysparm_name', 'getProviderName');
    gr.addParam('sysparm_serviceProvider', g_form.setValue('service_provider', 'lion'));
    gr.getXML(getResponse);
}

function getResponse(response) {
    var answer = response.responseXML.documentElement.getElementsByTagName('result');
    alert(answer); //getting "[object HTMLCollection]" instead of "lion"
        g_form.setValue('service_provider', answer);
}

Script Include:

var AHC_getServiceProvider = Class.create();
AHC_getServiceProvider.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    getProviderName: function() {
        var result = this.newItem("result");
        var service_provider = this.getParameter('sysparm_serviceProvider');
        var gr = new GlideRecord("u_zoo"); //zoo table to get value
        gr.get(service_provider);
        result.setAttribute("providerName", gr.getValue("sys_id")); //trying to return Lion sys_id
    },
    type: 'AHC_getServiceProvider'
});

Thanks in Advance,

Abhishek

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

why not use default value for this in the variable configuration?

you can store the sys_id of the record in system property as string and then call that in default value

Easier to maintain going forward

Just change the sys_id later on if required

javascript: var sysId = gs.getProperty('propertyName'); sysId;

Regards
Ankur

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

View solution in original post

8 REPLIES 8

Can you please share your updated scripts?

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

why not use default value for this in the variable configuration?

you can store the sys_id of the record in system property as string and then call that in default value

Easier to maintain going forward

Just change the sys_id later on if required

javascript: var sysId = gs.getProperty('propertyName'); sysId;

Regards
Ankur

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

@Abhishek Barik 

Thank you for marking my response as helpful.

If you follow the approach I shared you need not create client script and Script include and it would be easier to maintain and as per best practice.

If my response helped you please mark it correct to close the question so that it benefits future readers as well.

Regards
Ankur

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

Sure Ankur 🙂