Auto populate reference field values based on another reference field

ncp
Mega Guru

All,

I have done a lot of search on the community and have not found an answer to what I'm looking for. There are many similar questions, but not what I'm looking for:

Scenario:

I have two reference fields StockRoom & Assets that are defined as catalog client variables task which is part of a workflow.

When someone picks the stockroom I want only the assets that are in that stockroom to show up in the assets reference field and then allowing them to pick that asset

When the stockroom changes I want the asset list also to change

So this is similar to many posts that I saw for advanced reference qualifiers, but I dont think advanced reference qualifiers work for an "onChange" correct?

I also saw a lot of mention about catalog "onChange" client script, but all those were referring to populating the second reference field with a specific value, I would like the functionality to see all the assets when the stockroom value changes, allowing the user to pick the asset for that stockroom

Has anyone done something similar?

Thanks

1 ACCEPTED SOLUTION

Rather than returning the string of sys_ids, why not return the filter like this:



return 'install_status=6^stockroom='+selected_stockroom+'^substatus=Available'; (You can always double check the filter language by going to the asset table, filter it how you want for a particular stockroom, status, substatus, then right click and choose copy query. You can paste that into your script include and then just replace the sys_id of the stockroom with your selected_stockroom).



Also, double check that the script include is client callable (checkbox on the form).


View solution in original post

10 REPLIES 10

kristenankeny
Tera Guru

You want to use a combination of advanced reference qualifier and onChange client script. The advanced reference qualifier should use a script include call to get the list of assets. This is an example of how we do that on a field where we pass to the script the current selections in two separate fields:


javascript:new filterAssetsAJAX().filterInstallLocations(current.variables.requested_for,current.variables.install_location)



This is the code within the client callable script include, to give you an idea of what you can pass back:


filterInstallLocations: function(user,type) {


  if (user == '')


  {


  return 'sys_idISEMPTY';


  }


  else{


  if(type == 'computer'){


  return 'model_category.name=Desktop^ORmodel_category.name=Laptop^install_status=1^assigned_to=' + user;


  }


  else if (type == 'vdi'){


  return 'model_category.name=VM^install_status=1^assigned_to=' + user;


  }


  }


  },



Then the onChange script would clear out their selection in the Asset field when the StockRoom changes, thus forcing them to look up a new asset.


Thanks Kristen, In your case is the purpose of the client script to only clear the values in the asset field? I tried the script include and for some reason it was populating the asset field with all the assets in the alm_hardware table, thats why my thought was that the advanced reference qualifier would not trigger when values in the stockroom change.


Right - we only use the client script to force them to change the asset selected because they have selected other values in the other fields. So, if they change "requested for", we clear out the asset field so that they need to search again to get the right records related to the new person. It's my understanding that if they've already selected an asset and then change the field it is dependent upon, it won't trigger any warnings.


For some reason its not working, so this is my reference qualifier on the Asset field on the catalog task:



javascript: new PopulateAssetforStockroom().getAssets(current.variables.ref_stockroom);


where ref_stockroom is the stockroom reference field



This is my script include:


var PopulateAssetforStockroom = Class.create();


PopulateAssetforStockroom.prototype = {


      initialize: function() {


      },



  getAssets: function(selected_stockroom){


  var asset = '';    


  if(!stockroom){


            return;


  }


  var gr_assets = new GlideRecord("alm_hardware");



            gr_assets.addQuery('stockroom',selected_stockroom);


            gr_assets.addQuery('install_status',6);


            gr_assets.addQuery('substatus', 'available');


            gr_assets.query();



            while (gr_assets.next()){


                      if (asset == ""){


                                asset=gr_assets.sys_id;


                      }


                    else{


                              asset=asset+','+gr_assets.sys_id;


                      }


            }


  return "sys_idIN"+asset;


  },


      type: 'PopulateAssetforStockroom'


};