The CreatorCon Call for Content is officially open! Get started here.

Copy the state choice value from one table to another table

Divya K1
Tera Guru

Hi All,

asmt_assessment_instance.state : State field is choice field with different languages.

requirement is copy the Display value of asmt_assessment_instance.state to asmt_assessment_instance_question. u_assessment_state.  The script probably needs to be run to update the values on the records in the asmt_assessment_instance_question table that have the State in non-English. State value should update with only english language choice value.

1 ACCEPTED SOLUTION

Hi Ankur,

 

Thanks for helping, the below script worked

when to run filter Conditions -- state changes

Advanced --condition - current.state != previous.state

 

var questionGR = new GlideRecord('asmt_assessment_instance_question');
        questionGR.addQuery('instance', current.sys_id);
        questionGR.query();

        while (questionGR.next()) {
            questionGR.u_assessment_state = current.getValue('state');
            questionGR.update();
        }

View solution in original post

22 REPLIES 22

Hi Ankur,

 

Ankur, I tried with the above script but still u_assessment_state field value updating with different language values. I find out that u_assessment_state updating based on Work order task assigned to user profile language. ( eg. If WOT assigned to user language is Spanish, u_assessment_state is displaying in spanish language)

 

please find the attached screenshot

@Divya K1 

please ensure "u_assessment_state" is string type and then you can query sys_choice table with the state value and language as en and then set the display value i.e. label

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

    var queGr = new GlideRecord('asmt_assessment_instance_question');
    queGr.addQuery('instance', current.sys_id);
    queGr.query();
    while (queGr.next()) {
        var gr = new GlideRecord("sys_choice");
        gr.addEncodedQuery("nameSTARTSWITHasmt_assessment_instance^element=state^languageSTARTSWITHen^valueSTARTSWITH" + current.state);
        gr.query();
        if (gr.next()) {
            queGr.u_assessment_state = gr.label;
            queGr.update();
        }
    }

})(current, previous)

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Hi Ankur,

 

I will work on this and update you

Hi Ankur,

 

I tried with your suggestion by keeping logs as well.. Still u_assessment_state field is updating based on WOT assigned to user language. i checked the logs, i can see the warnings, please find the screenshots .

 

Can we use query user session language in the below script, Suppose something like this 

var userLanguage = gs.getSession().getLanguage();
        if (userLanguage != "en")
help me with the below script

 

var queGr = new GlideRecord('asmt_assessment_instance_question');
    queGr.addQuery('instance', current.sys_id);
    queGr.query();
    while (queGr.next()) {
        var gr = new GlideRecord("sys_choice");
        gr.addEncodedQuery("nameSTARTSWITHasmt_assessment_instance^element=state^languageSTARTSWITHen^valueSTARTSWITH" + current.state);
        gr.query();
        if (gr.next()) {
            queGr.u_assessment_state = gr.label;
            queGr.update();

        }
    }