Roles value visible based on Client values on catalog item

Manohararuna
Tera Contributor

Hi Everyone,

I have one requirement on Catalog item 

--In catalog item having two variable, Client and Roles and roles having the option Functional and Basis and Client Having the options DS4 100 and DS4 200 ,DCA 100. If i select Functional in Roles then DS4 100 and DS4 200 and DCA 100 visible only (others not)and i i select Basis in Roles then in Client only visible DS4 100, DS4 200(only others not visible)for this i want to on change client script in serviecnow.

--same time if i Development option in scape variable and  S4 option in System variable then DS4 100 and DS4 200 options only visible in client variable.

Please help me write on-change client script on this

 

4 REPLIES 4

Ankur Bawiskar
Tera Patron

@Manohararuna 

it's an easy requirement.

what did you start with and where are you stuck?

Hi Ankur,

Actuallay my end user trying to select multiple option.So i just change the variable type from Slect box to List Collector

1-reuirement-Landscape - Sandbox

Roles - it support  and functional support

Client - 300 and 450

 

2.requirement-Landscape - Development

Client - 100 and 200

3-Requirement-I f role having the option-

                                          DS4 100  DS4 200  DCA 100

  Functional                        Yes            Yes              Yes
Cross Functional                                    Yes           Yes

(CAR and S4) Y
Security                                Yes            Yes             Yes
Basis                                     Yes             Yes              Yes
Development                      Yes             Yes              Yes
Fiori Administration           Yes             Yes              Yes
CAR Configurator                                                        Yes

for this i written the client script and Script include. below

 

var Globalutils = Class.create();
Globalutils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getClientChoice: function() {
    var roles = this.getParameter('sysparm_roles');
    var clientChoices = [];
    return JSON.stringify(clientChoices);
},
    type: 'Globalutils'

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

var ga = new GlideAjax('Globalutils'); // Replace with your Script Include name
    ga.addParam('sysparm_name', 'getClientChoices'); // Replace with your Script Include function name
    ga.addParam('sysparm_roles', newValue); // Pass the selected roles
    ga.getXMLAnswer(function(answer) {
        var clientChoices = JSON.parse(answer);

        // Get the list collector elements for the Client variable
        var clientVarName = 'client'; // Replace with your Client variable name
        var leftBucket = gel(clientVarName + '_select_0');
        var rightBucket = gel(clientVarName + '_select_1');

        // Clear existing options
        clearOptions(leftBucket);
        clearOptions(rightBucket);

        // Populate the list collector
        for (var i = 0; i < clientChoices.length; i++) {
            var option = document.createElement("option");
            option.value = clientChoices[i].value;
            option.text = clientChoices[i].label;
            leftBucket.appendChild(option); // Add to the left bucket (available)
        }
    });
}

// Helper function to clear options
function clearOptions(selectBox) {
    while (selectBox.options.length > 0) {
        selectBox.remove(0);
    }
}  please correct the code as poet the requirement

 

SN_BR
Tera Contributor

Hi @Manohararuna 

In your case the best approach is create a Catalog Client Scrip > OnChange for Roles, Scape and System

Role

(function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || !newValue) return;

  var options = [];
  if (newValue === 'Functional') {
    options = ['DS4 100', 'DS4 200', 'DCA 100'];
  } else if (newValue === 'Basis') {
    options = ['DS4 100', 'DS4 200'];
  }

  g_form.clearOptions('client');
  g_form.addOption('client', '', '-- None --');
  options.forEach(function(opt) {
    g_form.addOption('client', opt, opt);
  });
})();

 

Scape and System

(function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading) return;

  var scape = g_form.getValue('scape');
  var system = g_form.getValue('system');

  if (scape === 'Development' && system === 'S4') {
    var options = ['DS4 100', 'DS4 200'];
    g_form.clearOptions('client');
    g_form.addOption('client', '', '-- None --');
    options.forEach(function(opt) {
      g_form.addOption('client', opt, opt);
    });
  }
})();

Rafael Batistot
Mega Patron

Hi @Manohararuna 

Create Catalog Client Script > onChange

Roles:

(function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || !newValue) return;

  var options = [];
  if (newValue === 'Functional') {
    options = ['DS4 100', 'DS4 200', 'DCA 100'];
  } else if (newValue === 'Basis') {
    options = ['DS4 100', 'DS4 200'];
  }

  g_form.clearOptions('client');
  g_form.addOption('client', '', '-- None --');
  options.forEach(function(opt) {
    g_form.addOption('client', opt, opt);
  });
})();


System

(function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading) return;

  var scape = g_form.getValue('scape');
  var system = g_form.getValue('system');

  if (scape === 'Development' && system === 'S4') {
    var options = ['DS4 100', 'DS4 200'];
    g_form.clearOptions('client');
    g_form.addOption('client', '', '-- None --');
    options.forEach(function(opt) {
      g_form.addOption('client', opt, opt);
    });
  }
})();