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

shabbir9
Tera Contributor

Actually we don't have option to create a table because of the client requirement, we need to follow this script and API procedure. Everything is good only duplicates issue we are facing .we need to do changes in script it self kindly share any idea on the above script any changes I need to do so that I can remove duplicates ..thanx for your response

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);
}

 

Thank You so much Mzhar,,this script helped me a lot thank you for help...👍