How to get sys_id of question choice in record producer script

tuitendung
Tera Contributor

 

Morning!
I hope you can help me. As you can see, I want to get sys_id of 2 variable's choice in record producer. Any solutions ? Thanks for your helps

 
6 REPLIES 6

Hi @tuitendung 
This is how OOTB works, but value will always remain same for all the language.
.getDispalyValue() will the label and .getValue() will give the actual value of the option selected.

 

 


Thanks and Regards,

Saurabh Gupta

Aniket Chavan
Tera Sage
Tera Sage

Hello @tuitendung 

I am still confuse that how you want that sys_id and what is the exact use of that but still i just gave shot to get the sys_id of my choice variable in portal when I select any choice with the help of alert I am able to get the sys_id of the choice from that variable and the sys_id i got from the "question_choice" table .

So accomplish that I have used on Change client script and one script include:
onChange Client Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    //Type appropriate comment here, and begin script below
    var choiceValue = g_form.getValue('test_var'); //test_var is my variable name
    g_form.addInfoMessage("TEST - " + choiceValue);


    var ga = new GlideAjax('ChoiceSysIdScriptInclude');
    ga.addParam('sysparm_name', 'getSysIdByChoice');
    ga.addParam('sysparm_choiceValue', choiceValue);

    ga.getXML(function(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        alert('Sys_ID of selected choice: ' + answer);
    });



}

Script Include:

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

    getSysIdByChoice: function() {
        var choiceValue = this.getParameter('sysparm_choiceValue');

        // Add an info log
        gs.info('Script Include - getSysIdByChoice - choiceValue: ' + choiceValue);

        var choiceGR = new GlideRecord('question_choice');
        //choiceGR.addQuery('question', 'e7d72b771bc331108390c910604bcbc1');
        choiceGR.addQuery('value', choiceValue);
        choiceGR.query();

        if (choiceGR.next()) {
            var sysId = choiceGR.getUniqueValue();

            // Add another info log
            gs.info('Script Include - getSysIdByChoice - sysId found: ' + sysId);

            return sysId;
        }

        // Log that the choice was not found
        gs.info('Script Include - getSysIdByChoice - Choice not found for value: ' + choiceValue);

        return null;
    },

    type: 'ChoiceSysIdScriptInclude'
});

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Regards,
Aniket