SELECT BOX

NannuSubhani Sh
Tera Contributor

HI 

in Rest message GET response we are  getting the names list i need to display those names list in variable &the variable type is select box (names_list)  ..those names are dynamic if new value coming in rest message response i need to create new question choice in select box variable (names_list) automatically by script can anyone help me out in the script for at least with one name which are showing in below rest message response..so that i can move forward

 

NannuSubhaniSh_0-1669808454227.png

 

 

5 REPLIES 5

Mohith Devatte
Tera Sage
Tera Sage

Hello @NannuSubhani Sh ,

You can write a insertion script in question choice table for this some thing like below 

var choices = ["street-111" , "street-211"];
for(var i=0; i<choices.length ; i++)
{
var gr= new GlideRecord('question_choice');
gr.initialize();
gr.text=choices[i];
gr.values=choices[i];
gr.question="your_question_sys_id";
gr.insert();
}

Some thing like this but not sure if this works but you can give it a try 

Hope this helps 

Mark my answer correct if this helps you 

Thanks

S Goutham
Tera Guru

Hey @NannuSubhani Sh 

 

Here is a sample script that you can enhance as per your requirement

var response = ["Street","City","Country"]; //your Rest Response
response.forEach(addChoice)

function addChoice(choice){
    var arrayUtil = new global.ArrayUtil();
    var currentchoice = [];
    var choice_list = new GlideRecord('sys_choice');
    choice_list.addQuery('name','incident'); // add your table
    choice_list.addQuery('element','type'); // add your varibale name
    choice_list.query();
    while(choice_list.next()){
        currentchoice.push(choice_list.value.toString());
    }
    if(!(arrayUtil.contains(currentchoice,choice))){ // Skip choice addition if the choice with same value already present 
    var add_choice = new GlideRecord('sys_choice');
    add_choice.initialize();
    addChoice.name = 'incident'; // add your table
    add_choice.element = 'type' // add your varibale name 
    add_choice.label = choice.toString();
    add_choice.value = choice.toString();
    add_choice.sequence = currentchoice.length == 0 ? 0 : currentchoice.length-1; //sequence the choice new created
    add_choice.insert();
    }
}

Please mark helpful or if it solves the your problem mark it as solution

I hope this solves your issue
Mark this as Helpful / Accept the Solution if this clears your issue

var timezone = Class.create();
timezone.prototype = Object.extendsObject(AbstractAjaxProcessor, {

 

getCategories: function() {

try {
var r = new sn_ws.RESTMessageV2('restmessage Access', 'get zonenames');

//override authentication profile
//authentication type ='basic'/ 'oauth2'
//r.setAuthenticationProfile(authentication type, profile name);

//set a MID server name if one wants to run the message on MID
//r.setMIDServer('MY_MID_SERVER');

//if the message is configured to communicate through ECC queue, either
//by setting a MID server or calling executeAsync, one needs to set skip_sensor
//to true. Otherwise, one may get an intermittent error that the response body is null
//r.setEccParameter('skip_sensor', true);

var response = r.execute();

var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.log('status' + httpStatus, 'sh31');

var arr = responseBody.split(',');
// gs.log('response body ' + arr[i], 'sh30');
//var order =100;
for(i=0;i<=arr.length;i++){
var choices = new GlideRecord('question_choice');
choices.initialize();
choices.question='848e17541b6211503a95ed30604bcb0d';
choices.text=arr[i];
choices.value=arr[i];
gs.log('response body ' + arr[i], 'sh30');
choices.insert();
}
} catch (ex) {
var message = ex.message;
gs.log('exeption ' + message, 'sh30');
}

},
type: 'timezone'
});

 

 

 

Hi goutham we are calling this script include via on load client script we are creating question choice based on rest message response but now the problem is when ever we are loading catalog item question choice are automatically creating for example in rest message 20 names are there every time we load the catalog item 20 new values are creating and the count is increasing. Now we need to to create value in question choice when ever new value added in response not duplicates can you help us in this script to remove duplicate and create a question choice only when new value added in rest message response ..reply me for any queries ..

@NannuSubhani Sh Try the Below Script

  • Check the uniqueness of choices already exsist by the value

 

var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var arr = responseBody.split(','); // converted to array
arr.forEach(addChoice)

function addChoice(choice){
    var arrayUtil = new global.ArrayUtil();
    var currentchoice = [];
    var choice_list = new GlideRecord('question_choice');
    choice_list.addQuery('question','848e17541b6211503a95ed30604bcb0d'); // choice field
    choice_list.query();
    while(choice_list.next()){
        currentchoice.push(choice_list.value.toString()); //push exsisting choice values of the field
        gs.log('@Test Current Choice List :'+currentchoice);
    }
    if(!(arrayUtil.contains(currentchoice,choice))){ // Skip choice addition if the choice with same value already present 
    var add_choice = new GlideRecord('question_choice');
    add_choice.initialize();
    addChoice.question = '848e17541b6211503a95ed30604bcb0d'; // choice field
    add_choice.text = choice.toString();
    add_choice.value = choice.toString();
    add_choice.order = currentchoice.length == 0 ? 0 : currentchoice.length-1; //sequence the choice new created
    add_choice.insert();
    }
}

 

 

I hope this solves your issue
Mark this as Helpful / Accept the Solution if this clears your issue