Need help auto-populating Configuration Item variable from selected Incident or Change

tilekarnilesh
Giga Guru


Hi All,

I am working on a catalog item where the user can select either an Incident or a Change Request ticket using catalog variables.

Requirement:

If the user selects an Incident, the Configuration Item field from that Incident should automatically populate in the catalog item’s configuration_item variable.

Similarly, if the user selects a Change Request, the Configuration Item from that Change should populate in the same configuration_item variable.

variables:

please_select_the_inc_ticket → reference to Incident table

please_select_the_chg_ticket → reference to Change Request table

configuration_item → reference to cmdb_ci


Thanks in advance 🙏

2 REPLIES 2

Bhimashankar H
Mega Sage

Hi @tilekarnilesh ,

 

You do it by calling a script include or using getReference in onChange client script.

 

Here I have explained using getReference method, so you don't need to write script include.

 

Create two onChange client script on inc ticket and chg ticket varibles.

1)Inc Ticket

function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading) return;
  // Clear CI when cleared
  if (!newValue) {
    g_form.clearValue('configuration_item');
    return;
  }

  // Get selected Incident’s CI
  g_form.getReference('please_select_the_inc_ticket', function(incident) {
    if (!incident) return;

    // Incident’s CI fields. Usually 'cmdb_ci' is the field on incident.
    var ciSysId = incident.cmdb_ci;                // sys_id
    var ciName  = incident.cmdb_ci.getDisplayValue
                  ? incident.cmdb_ci.getDisplayValue()
                  : '';                             // display name if available

    if (ciSysId) {
      // Provide both sys_id and display value to avoid extra round-trip
      g_form.setValue('configuration_item', ciSysId, ciName);
    } else {
      g_form.clearValue('configuration_item');
    }
  });
}

 

2)Chg Ticket

function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading) return;
  if (!newValue) {
    g_form.clearValue('configuration_item');
    return;
  }

  // Get selected Change Request’s CI
  g_form.getReference('please_select_the_chg_ticket', function(chg) {
    if (!chg) return;

    // Common Change fields are 'cmdb_ci' on change_request (or 'ci' on older/custom)
    var ciSysId = chg.cmdb_ci || chg.ci;           // adapt if your field is named differently
    var ciName  = '';

    // In many cases, getReference returns properties as strings; try display value fetch
    if (chg.cmdb_ci && chg.cmdb_ci.getDisplayValue)
      ciName = chg.cmdb_ci.getDisplayValue();

    if (ciSysId) {
      g_form.setValue('configuration_item', ciSysId, ciName);
    } else {
      g_form.clearValue('configuration_item');
    }
  });
}

 

If both selectors are visible, add logic to clear the other field on change to avoid confusion, or make them mutually exclusive with UI Policy. For example, when Incident changes, clear please_select_the_chg_ticket, and vice versa.

 

If you want to use script include refer below

Auto populate catalog item field from client script 

 

 

Thanks,
Bhimashankar H

 

-------------------------------------------------------------------------------------------------
If my response points you in the right directions, please consider marking it as 'Helpful' & 'Correct'. Thanks!

palanikumar
Mega Sage

Hi,

You can create a On Change Catalog Client script for incident and change variable and use the below script

 

// Note, this script is for incident . Create another one for Change and update the variable name

function onChange(control, oldValue, newValue, isLoading) {

      if (isLoading || newValue == '') {

              return;

      }

     

      var cmdbCI = getReferenceAdvancedPortal(g_form, "please_select_the_inc_ticket", "cmdb_ci");

      g_form.setValue("configuration_item", cmdbCI.cmdb_ci);

}

  

Thank you,
Palani