How to write script include for reference field and call that in catalog script include

Lakshmi laks
Tera Contributor

Hello,

I have a requirement , there a reference filed call "Requesting Business unit" which is from "Business_unit".
user can only select least level value from hierarchy from that table and call this script include to a catalog client script and  if least level is not selected user should get an error (idea portal).
i dont know how to write script include for reference filed and call that in catalog client script.
please help me on this
Thank you, 

4 REPLIES 4

SaiVasanth
Tera Guru

Hi @Lakshmi laks 

What do you want to do with that reference filed and which table you gave as reference value.

Thanks

Hi @SaiVasanth ,
I need to consider records name starts with "Business" from "Business_unit" table..
reference table is "Business_unit"

Shaik Imran ALI
Tera Expert

 

Hi @Lakshmi laks ,

Regarding your question about the "least level value," it refers to selecting the exact value name from the "business_unit" table. If that is the case, please follow the steps below:


Create an "onChange" catalog client script with GlideAjax:

function onChangeRequestingBusinessUnit() {
  var requestingBusinessUnit = g_form.getValue('requesting_business_unit');

  var ga = new GlideAjax('BusinessUnitUtilsAjax');
  ga.addParam('sysparm_name', 'validateBusinessUnit');
  ga.addParam('sysparm_business_unit', requestingBusinessUnit);
  ga.getXML(function(response) {
    var isValid = response.responseXML.documentElement.getAttribute("answer") === 'true';

    if (!isValid) {
      alert('Please select a business unit at the least level in the hierarchy.');
      g_form.clearValue('requesting_business_unit');
    }
  });
}


Create a new Script Include the name "BusinessUnitUtilsAjax":

var BusinessUnitUtilsAjax = Class.create();

BusinessUnitUtilsAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
  validateBusinessUnit: function() {
    var businessUnitSysId = this.getParameter('sysparm_business_unit');
    var isValid = false;

    var grBusinessUnit = new GlideRecord('business_unit');
    if (grBusinessUnit.get(businessUnitSysId)) {
      // Check if the selected business unit is at the least level in the hierarchy
      if (grBusinessUnit.getValue('is_least_level') === true) {
        isValid = true; // The selected business unit is at the least level
      }
    }

    return isValid.toString();
  },

  type: 'BusinessUnitUtilsAjax'
});


Please make sure to adjust the code according to your specific table and field names in the GlideRecord query ('business_unit') and the field used for determining the least level ('is_least_level').


Thanks,

Imran

Shaik Imran ALI
Tera Expert

Hi @Lakshmi laks ,

 Regarding your question about the "least level value," it refers to selecting the exact value name from the "business_unit" table. If that is the case, please follow the steps below:

1. Create an "onChange" catalog client script with GlideAjax:

  var requestingBusinessUnit = g_form.getValue('requesting_business_unit');

  var ga = new GlideAjax('BusinessUnitUtilsAjax');
  ga.addParam('sysparm_name', 'validateBusinessUnit');
  ga.addParam('sysparm_business_unit', requestingBusinessUnit);
  ga.getXML(function(response) {
    var isValid = response.responseXML.documentElement.getAttribute("answer") === 'true';

    if (!isValid) {
      alert('Please select a business unit at the least level in the hierarchy.');
      g_form.clearValue('requesting_business_unit');
    }
  }


2. Create a new Script Include with the name "BusinessUnitUtilsAjax"

var BusinessUnitUtilsAjax = Class.create();

BusinessUnitUtilsAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
  validateBusinessUnit: function() {
    var businessUnitSysId = this.getParameter('sysparm_business_unit');
    var isValid = false;

    var grBusinessUnit = new GlideRecord('business_unit');
    if (grBusinessUnit.get(businessUnitSysId)) {
      // Check if the selected business unit is at the least level in the hierarchy
      if (grBusinessUnit.getValue('is_least_level') === true) {
        isValid = true; // The selected business unit is at the least level
      }
    }

    return isValid.toString();
  },

  type: 'BusinessUnitUtilsAjax'
});

 

 Please make sure to adjust the code according to your specific table and field names in the GlideRecord query  ('business_unit') and the field used for determining the least level ('is_least_level').

 

Thanks,

Imran Ali