How to add values in Choice Tables

Learner10
Mega Guru

Hello Community Experts,

 

We have record producer on the Problem table  and one of the Variable called Select Service it's single line text and We are looking for the way to check entered value in select service variable available or not in the sys_choice table and if it's not available we want to create in the choice table is there any way to achive this on submission.

Highly appreciated if anyone share the script to achieve this.

 

Thanks,

Learner

5 REPLIES 5

Hello @Learner10 ,

Try using below code in your script include:

checkChoice :function() {
		var service = this.getParameter('sysparm_service');
		var choiceGr = new GlideRecord('sys_choice');
		choiceGr.addQuery('name','incident'); //table name
		choiceGr.addQuery('element','u_service'); //field name
		choiceGr.addQuery('value',service); //value to search
		choiceGr.query();
		if(!choiceGr.next()){
			choiceGr.initialize();
                        var choiceGr2 = new GlideRecord("sys_choice");
                        choiceGr2.addQuery('name', 'incident'); //table name
                        choiceGr2.addQuery('element', 'u_service'); //field name
                        choiceGr2.orderByDesc('sequence');
                        choiceGr2.setLimit(1);
                        choiceGr2.query();
                        if(choiceGr2.next())
                               choiceGr.sequence = choiceGr2.sequence + 10;     //increment choice sequence by 10 each time
                        else
                               choiceGr.sequence = 10;  //if this is first choice to be added then set sequence to 10
			choiceGr.element = "u_service";
			choiceGr.name = "incident";
			choiceGr.value = service;
			choiceGr.label = service;
			choiceGr.insert();	
			return "insert";
		}
		else
			return "exist";
	}

 

If my answer has helped with your question, please mark it as correct and give it a thumbs up!

 

Regards,

Shubham