duplicate values in Question choice

shabbir9
Tera Contributor

Hello

from third party application we are getting enclave names list those names i need to display in select box variable on catalog item .. i have created a rest message & On load client script ,script include and using following script i am able to fetch those names and i am able to create the question choices in select box. now the problem is when ever i load the form question choices are increasing for example 10 names are there when ever i load the form another 10 names are adding i need to show just if any new name added to that names(10+new name) not every time 10+10+10........no duplicates can anyone help me out in this script

client Script::

function onLoad() {
//Type appropriate comment here, and begin script below
var ga = new GlideAjax('displayenclavenames');
ga.addParam('sysparm_name','getCategories');
ga.getXML(getCategoriesOutput);
}
function getCategoriesOutput(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var objJSON = JSON.parse(answer);
for(var loop = 0 ; loop < objJSON.categories.length;loop++)
{
alert(objJSON.categories[loop].Name);
g_form.addOption('enclave_name',objJSON.categories[loop].Value,objJSON.categories[loop].Name);
}
}

Script Include:;;

var displayenclavenames = Class.create();
displayenclavenames.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCategories:function() {
try {
var r = new sn_ws.RESTMessageV2('network Access', 'GET enclave Names');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var arr = responseBody.split(',');
for(i=0;i<=arr.length;i++){
var choice = new GlideRecord('question_choice');
choice.initialize();
choice.question='66b6071e1bd119503037a68ee54bcb1d';
choice.text=arr[i];
choice.value=arr[i];
choice.insert();
}
} catch (ex) {
var message = ex.message;
gs.log('exeption ' + message, 'sh2');
}
},
type: 'displayenclavenames'
});

1 ACCEPTED SOLUTION

Hi @shabbir9 

 

Could you please try this one

try {
  // Clear existing choices
  var choices = new GlideRecord('question_choice');
  choices.addQuery('question', '848e17541b6211503a95ed30604bcb0d');
  choices.deleteMultiple();
  
  // Add new choices
  var response = r.execute();
  var responseBody = response.getBody();
  gs.log('sitebuilding check1:' + responseBody);
  var httpStatus = response.getStatusCode();
  gs.log('status' + httpStatus, 'sh31');
  var arr = responseBody.split(',');
  var existingChoices = [];
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] && existingChoices.indexOf(arr[i]) == -1) {
      existingChoices.push(arr[i]);
      var newChoice = new GlideRecord('question_choice');
      newChoice.initialize();
      newChoice.question = '848e17541b6211503a95ed30604bcb0d';
      newChoice.text = arr[i];
      newChoice.value = arr[i];
      newChoice.insert();
    }
  }
} catch (ex) {
  gs.log('Error: ' + ex);
}

 

View solution in original post

7 REPLIES 7

SoniaShridhar13
Giga Guru

Hi @shabbir9  I tried below when I was facing duplicate issues 

You need to build your own logic to remove duplicates. Try below code.



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

 

if(newValue == oldValue){

 

return;

 

}

 

//remove all items from subcat drop down to start

 

// Used the g_form.clearOptions() function instead of g_form.removeOption() function

 

g_form.clearOptions('u_secondary_issue_category');



//build a new list of dependent options

 

var previousValue = '' ;

 

var gp = new GlideRecord('u_novitex_issue_dictionary');

 

gp.addQuery('u_primary_system', newValue);

 

gp.orderBy('u_sub_system');

 

gp.query();

 

while(gp.next()){

 

var currentValue = gp.u_sub_system ;

 

if(currentValue != previousValue){

 

g_form.addOption('u_secondary_issue_category', currentValue, currentValue);

 

}

 

previousValue = currentValue;

 

}

 

  }

 

Please mark helpful if it helps...

 

Thanks,

Sonia

Shaikh Mzhar
Tera Guru

Hi @shabbir9 

 

To avoid duplicates, you can add a check before inserting a new choice. You can maintain a list of existing choice texts and compare each new choice text with the list before inserting it. If the text already exists in the list, you can skip that choice. 

var existingChoices = [];

for(i=0;i<=arr.length;i++){
if(existingChoices.indexOf(arr[i]) == -1) {
existingChoices.push(arr[i]);
var choice = new GlideRecord('question_choice');
choice.initialize();
choice.question='66b6071e1bd119503037a68ee54bcb1d';
choice.text=arr[i];
choice.value=arr[i];
choice.insert();
}
}

This way, you will avoid adding duplicate choices every time the form is loaded.

 

Please mark helpful to remove from unanswered question.

 

HI mzhar thank you for your response i tried this but its not working again duplicates are creating when ever i load the form kindly help on this 

 

var response = r.execute();
var responseBody = response.getBody();
gs.log('sitebuilding check1:' + responseBody);
var httpStatus = response.getStatusCode();
gs.log('status' + httpStatus, 'sh31');
var arr = responseBody.split(',');
var existingChoices = [];
for (i = 0; i <= arr.length; i++) {
if(existingChoices.indexOf(arr[i]) == -1) {
existingChoices.push(arr[i]);
var choices = new GlideRecord('question_choice');
choices.initialize();
choices.question = '848e17541b6211503a95ed30604bcb0d';
choices.text = arr[i];
choices.value = arr[i];
choices.insert()
}
}
} catch (ex) {
var message = ex.message;
}

},
type: 'namezones'
});

 

Create an import set row table and a transform map with proper coalesce.

Instead of inserting into the choices table directly, insert into the import set row.

If the coalesce in the associated transform map is correctly configured (basically a combination of question and value) no duplicates will be created.