How to Populare other fields based on value of Lookup select box field on catalog item form

Atik
Tera Contributor

Hello,

I have requirement where I have to populate one reference field - 'Owner' based on the Lookup select box field,

Actually, the lookup select box field referencing to one table where we have fields name, owned by, whenever user chooses the name I have to populate the 'form field - Owner' and that should be 'owned by' field value

 

I was trying by getReference method, and i guess it is not working

 

Please help me on this 

 

Thanks, 

Atik 

8 REPLIES 8

Stefan Georgiev
Tera Guru

Hello @Atik ,

you should you an GlideAjax call to a script include that you should create. IN the script include you need a function that take the value from your lookup select box and retuns the owned by field value. Then you call the GLideAjax from an on Change client script on the Lookup select box.

This is the short story write me if you need more help.Hope that this helps you!

If the provided information answers your question, please consider marking it as Helpful and Accepting the Solution so other community users can find it faster.

All the Best,
Stefan

Atik
Tera Contributor

@Stefan Georgiev ,

Can you help me with code please, if possible..

 

Thanks,

Atik

Hello @Atik,

here are some examples how how it works:
https://docs.servicenow.com/bundle/vancouver-api-reference/page/app-store/dev_portal/API_reference/G...

https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet-updated/ta-p/2...

 

The first thing you need to do is to create a Client Callable Script include, something like this from the example:

var asu_GetLocationData = Class.create(); // this is the name of the method you are going to use in the GlideAjax
asu_GetLocationData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCampus: function () { // this is the name of the function you are going to use
var buildingid = this.getParameter('sysparm_buildingid'); // this should be the name of the param you are going to give to the function <sys_id of the Lookup select box
    var loc = new GlideRecord('<you_table>'); // table from where the lookup selectbox is
    if (loc.get(buildingid)) {
return loc.owned_by +'' // or how your field is called, this is the value that you are going to set on Onwer
}
  }
});

Second you create an onChange client script on the Lookup Select box and call the GlideAjax with the new value of the field as in the example:
 

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

    var ga = new GlideAjax('asu_GetLocationData');
    ga.addParam('sysparm_name', 'getCampus');
    ga.addParam('sysparm_buildingid', newValue);
    ga.getXMLAnswer(updateCampus);
}

function updateCampus(answer) {
   
    if (answer) {
       
        g_form.setValue("owner", answer);
    }
}

 This should work, I haven't tested it, I have just used the example from the links I have added and modified it a bit. You can later change the names. Check the examples as well.Hope that this helps you!

If the provided information answers your question, please consider marking it as Helpful and Accepting the Solution so other community users can find it faster.

All the Best,
Stefan

Hi @Stefan Georgiev ,

This is what I exactly want, and it worked....Thank you so much