How to insert values and labels in sys_choice table from a custom table.

Raviteja Kunal1
Tera Expert

I have three fields in a custom table as below.

find_real_file.png

Whenever a new record is inserted with all the values I need to write a script to insert the same with labels and values in the sys_choice table with elements as Category and SubCategory. The issue I am facing here is I need to do insert 2 times with different element values for one record inserted in the custom table. Any idea how can I achieve this?

I really appreciate any help you can provide.

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Raviteja,

Create a business rule on the custom table that executes on "insert".

Check on "Advanced" and add the following script. I've named fields "fuctionalarea" and added fields to enter table name, language, value, and label to set on the choice.

(function executeRule(current, previous /*null when async*/ ) {

    if (current.category == '' || current.functionalarea == '' || current.subcategory == '') {
        return;
    }
    var fieldList = ['category', 'subcategory', 'functionalarea'];
    var name = current.name;  // 'incident';
    var lang = current.language; // 'en';
    var value = current.value;  // 'test';
    var label = current.label;  // 'test';

    for (var i = 0; i < fieldList.length; i++) {
        insertRecord(name, lang, value, label, fieldList[i]);
    }

    function insertRecord(name, lang, value, label, field) {
        var grChoice = new GlideRecord("sys_choice");
        grChoice.addQuery('name', name);
        grChoice.addQuery('element', field);
        grChoice.addQuery('language', lang);
        grChoice.addQuery('value', value);
        grChoice.query();
        if (!checkGR.hasNext()) {
            var grChoice2 = new GlideRecord("sys_choice");
            grChoice2.initialize();
            grChoice2.setValue('name', name);
            grChoice2.setValue('element', field);
            grChoice2.setValue('language', lang);
            grChoice2.setValue('value', value);
            grChoice2.setValue('label', label);

            grChoice2.insert();
        }
    }


})(current, previous);

 

View solution in original post

12 REPLIES 12

@Raviteja Kunala 

why not use flow designer for this?

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur

I am not aware of flow designer.

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Raviteja,

Create a business rule on the custom table that executes on "insert".

Check on "Advanced" and add the following script. I've named fields "fuctionalarea" and added fields to enter table name, language, value, and label to set on the choice.

(function executeRule(current, previous /*null when async*/ ) {

    if (current.category == '' || current.functionalarea == '' || current.subcategory == '') {
        return;
    }
    var fieldList = ['category', 'subcategory', 'functionalarea'];
    var name = current.name;  // 'incident';
    var lang = current.language; // 'en';
    var value = current.value;  // 'test';
    var label = current.label;  // 'test';

    for (var i = 0; i < fieldList.length; i++) {
        insertRecord(name, lang, value, label, fieldList[i]);
    }

    function insertRecord(name, lang, value, label, field) {
        var grChoice = new GlideRecord("sys_choice");
        grChoice.addQuery('name', name);
        grChoice.addQuery('element', field);
        grChoice.addQuery('language', lang);
        grChoice.addQuery('value', value);
        grChoice.query();
        if (!checkGR.hasNext()) {
            var grChoice2 = new GlideRecord("sys_choice");
            grChoice2.initialize();
            grChoice2.setValue('name', name);
            grChoice2.setValue('element', field);
            grChoice2.setValue('language', lang);
            grChoice2.setValue('value', value);
            grChoice2.setValue('label', label);

            grChoice2.insert();
        }
    }


})(current, previous);