How to make multiple field references

Arthur Sanchez
Giga Guru

Captura de tela 2024-04-18 151711.pngThe Area field has the options:

ti and seg: IT in the field system: APP, BRM, CAPTA, GPP

SEG in the system field: SMS, IMS, ACS

 

Now the problem is, I need to reference it multiple times, so when the Area field is selected the following are presented in the Service Type field:

 

TI

  1. Configuration
  2. Maintenance
  3. Customization
  4. Installation
  5. Consulting
  6. Backup and Recovery
  7. Training
  8. Request

 

SEG

  1. Configuration
  2. Maintenance
  3. Backup and Recovery
  4. Training
  5. Request
  6. Incident

some fields are repeated and so I am unable to reference them, the first reference I did this way

Captura de tela 2024-04-18 152520.png

 

1 ACCEPTED SOLUTION

Start with creating a new Script Include; for that open module "System Definition ↳ Script Includes":

6.png

 Make sure there is no script include named "Tickets" already (in scope Tickets Register):

7.png

Start the creation of a new record by clicking New.

In the new Script Include record set fields:

  • Name to Tickets
  • Script to
    var Tickets = Class.create();
    
    Tickets.prototype = {
    
    	'initialize': function () {
    		this._dependentValue = typeof dependent_value == 'undefined' ? '' : dependent_value;
    		this._sessionLanguage = 'en'; // gs.getSession().getLanguage();
    	},
    
    	'getSericeTypesForArea': function () {
    		return this._getChoiceList();
    	},
    
    	'type': 'Tickets',
    
    	'_addRecordToChoice': function (glideChoiceList, record) {
    		glideChoiceList.add(record.value, record.label);
    		return glideChoiceList;
    	},
    
    	'_getChoiceList': function () {
    		return new GlideQuery('sys_choice')
    			.where('name', 'x_1316384_tickets_tickets')
    			.where('element', 'service_type')
    			.where('language', this._sessionLanguage)
    			.where('inactive', false)
    			.where('dependent_value', 'CONTAINS', this._dependentValue)
    			.limit(999)
    			.orderBy('sequence')
    			.select(['label', 'value'])
    			.reduce(this._addRecordToChoice, new GlideChoiceList());
    	},
    
    };​

Save the record.

Open up the dictionary record for field "Service type"; switch view to Advanced, otherwise field Attributes will not be displayed:8.png

Modify field Attributes by adding text:

choice_script=new Tickets().getSericeTypesForArea()

 

 

like this:9.png

If there is some text in there already, place a comma between the existing and the new text.
E.g: if it already contains:

edge_encryption_enabled=true

like this:

10.png

the value for Attributes will become:

edge_encryption_enabled=true,choice_script=new Tickets().getSericeTypesForArea()

like this:11.png

That's all there is to it.

Now for each choice for field "Service Type" you have to update field "Dependent value".

In case of "Service Type" choices that should only show for "Area" ti, the value in "Dependent value" should be ti (not ti,seg).

In case of "Service Type" choices that should only show for "Area" seg, the value in "Dependent value" should be seg (not ti,seg).

In case of "Service Type" choices that should show for both "Area" choices, the value in "Dependent value" should be ti,seg as is now.

Of course should you add more "Area" choices, you would just add it to the "Dependent value" as needed.

 

View solution in original post

15 REPLIES 15

-O-
Kilo Patron
Kilo Patron

You can use dictionary attribute choice_script - which is kinda' like a reference qualifier for choice lists.

I don't call it an equivalent because in a reference qualifier one has access to the whole current record, but in a choice script one has access only to global variable dependent_value.

 

That said here's how you can do it:

  1. Make dependent_value a list (of dependent choices, of course):
    1.png
  2. Modify script include global.Incident by including following functions - there might already be stuff in there, in which case you can't just replace the contents, you need to mix in the new parts:
    var Incident = Class.create();
    
    Incident.prototype = Object.extendsObject(IncidentSNC, {
    
    	'initialize': function (incidentGr) {
    		IncidentSNC.prototype.initialize.call(this, incidentGr);
    
    		this._u_dependentValue = typeof dependent_value == 'undefined' ? '' : dependent_value;
    		this._u_sessionLanguage = gs.getSession().getLanguage();
    	},
    
    	'u_getSericeTypesForArea': function () {
    		return this._u_getEmptyList() || this._u_getLoadedList();
    	},
    
    	'type': 'Incident',
    
    	'_u_addRecordToChoice': function (glideChoiceList, record) {
    		glideChoiceList.add(record.value, record.label);
    		return glideChoiceList;
    	},
    
    	'_u_getEmptyList': function () {
    		if (this._u_dependentValue == '')
    			return new GlideChoiceList();
    	},
    
    	'_u_getLoadedList': function () {
    		return new GlideQuery('sys_choice')
    			.where('name', 'incident')
    			.where('element', 'u_service_type')
    			.where('language', this._u_sessionLanguage)
    			.where('inactive', false)
    			.where('dependent_value', 'CONTAINS', this._u_dependentValue)
    			.limit(999)
    			.orderBy('sequence')
    			.select(['label', 'value'])
    			.reduce(this._u_addRecordToChoice, new GlideChoiceList());
    	},
    
    });​

    Note that as is, this will show no choices in lists!
    If you want all choices to be shown when no dependency is selected (e.g. in lists) you need to modify function u_getSericeTypesForArea to be:

    	'u_getSericeTypesForArea': function () {
    		return this._u_getLoadedList();
    	},

    Also I'm assuming you will have the list defined for all available languages.
    If you don't do that and only defined the list for language code en, you need to modify the query to filter language not to the session language, but to code en.
    I'm also assuming the name of the field is u_service_type; if this is not the case, adjust accordingly.
    Another assumption is that the list is defined for table incident; if this is not the case, adjust accordingly.

  3. Add the dictionary attribute to field "Service type":2.png

     or, in text:

    choice_script=new global.Incident().u_getSericeTypesForArea()

I suppose that's about it.

 

Here's how it looks at my end with a prototype I have created:

  • when no area is selected:3.png

     

  • when SEG is selected:4.png

     

  • when TI is selected:5.png

     

Bro, it seems like your idea works, the problem is that I don't understand how to make it work

my service type:

Captura de tela 2024-04-19 095829.png

 

my table is: x_1316384_tickets_tickets
I already put the values of dependent fields in my service type, now what?

I didn't understand what you did and how you did it

My application looks like this:Captura de tela 2024-04-19 100335.png

You need to add the attribute (into field Attribute).

Dude give me a step by step guide on how to do this, I don't even know where to put this script and how to call it