Copy List collector values to a multi line text field

Pravallika1
Giga Guru

Hi !!

I am facing issue on how to copy values from left slushbucket in a list collector field to a multi line text field in a record producer using catalog client script.

I have a list collector field which contains some state values , as soon as the user selects some values ,I should exclude those selected values and copy remaing values (left slushbucket) values in a multi line text field.

My list collector field is referenced to select box variable that contains choice values on question choices table.

I am unable to populate the values on change of the list collector variable using catalog client script.

Please do help me with the code.

Pravallika1_0-1708457758461.png

From the above screenshot , i should be able to populate display users field with all other values from left slushbucket excluding Hawaii & Michigan as soon as the user selects them in list collector variable.

 

Caltalog Client script:

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

    var initialChoices = ['dc8b769893a0021035e6b2ddfaba10cc,1ecb365893a0021035e6b2ddfaba1009,4bebf65893a0021035e6b2ddfaba104e,fc2cfe9893a0021035e6b2ddfaba10e4,7d3cb6909320021035e6b2ddfaba10aa,2e5c3a9893a0021035e6b2ddfaba1066,b66c769893a0021035e6b2ddfaba10d6,ea8cf6d893a0021035e6b2ddfaba101f,ee9cf69893a0021035e6b2ddfaba10c8'];
    var traineeEmailCollection = [];
    var states = [];
    var traineeNameList = g_form.getValue('test_users');
    // give here list collector variable name in above syntax
    var trainees = traineeNameList.split(',');
    for (var j = 0; j < trainees.length; j++) {
        traineeEmailCollection.push(trainees);
    }
    g_form.addInfoMessage('Array=' + traineeEmailCollection);
    for (var i = 0; i < initialChoices.length; i++) {
        if (trainees.indexOf(initialChoices[i]) == -1) {
            states.push(initialChoices[i]);
        }
    }
    //g_form.setValue('display_users', states);

    var gaStates = new GlideAjax('u_AjaxUtils');
    gaStates.addParam('sysparm_name', 'getStatesDiff');
    gaStates.addParam('sysparm_states', states);
    gaStates.getXML(SetfieldValues);

    function SetfieldValues(response) {

        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.addInfoMessage('answer= ' + answer);
        g_form.setValue('display_users', answer);
    }

}
 
Script Include:
 getStatesDiff: function() {
        var states = this.getParameter('sysparm_states');
        var statesList = states.split(',');
        var traineeEmailCollection = [];
        for (var i = 0; i < statesList.length; i++) {

            var gr = new GlideRecord('question_choice');
            gr.addQuery('sys_id', statesList[i]);
            gr.query();
            while (gr.next()) {
                traineeEmailCollection.push(gr.text);
            }
            gs.addInfoMessage('traineeEmailCollection = ' + traineeEmailCollection.toString());
        }
        return traineeEmailCollection.toString();

    },
 
Please do help me with the code.
Thanks in Advance!!
1 ACCEPTED SOLUTION

Sohithanjan G
Kilo Sage
Kilo Sage

Hi @Pravallika1 
There are a few adjustments needed. Below is the revised version of your script:

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

    var initialChoices = ['dc8b769893a0021035e6b2ddfaba10cc','1ecb365893a0021035e6b2ddfaba1009','4bebf65893a0021035e6b2ddfaba104e','fc2cfe9893a0021035e6b2ddfaba10e4','7d3cb6909320021035e6b2ddfaba10aa','2e5c3a9893a0021035e6b2ddfaba1066','b66c769893a0021035e6b2ddfaba10d6','ea8cf6d893a0021035e6b2ddfaba101f','ee9cf69893a0021035e6b2ddfaba10c8'];
    var selectedChoices = g_form.getValue('test_users');
    var selectedChoicesArray = selectedChoices.split(',');
    var states = [];

    for (var i = 0; i < initialChoices.length; i++) {
        if (selectedChoicesArray.indexOf(initialChoices[i]) == -1) {
            states.push(initialChoices[i]);
        }
    }

    var gaStates = new GlideAjax('u_AjaxUtils');
    gaStates.addParam('sysparm_name', 'getStatesDiff');
    gaStates.addParam('sysparm_states', states);
    gaStates.getXML(SetfieldValues);

    function SetfieldValues(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('display_users', answer);
    }
}

 

Script Include:

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

    getStatesDiff: function() {
        var states = this.getParameter('sysparm_states').split(',');
        var traineeEmailCollection = [];

        for (var i = 0; i < states.length; i++) {
            var gr = new GlideRecord('question_choice');
            if (gr.get(states[i])) {
                traineeEmailCollection.push(gr.text);
            }
        }

        return traineeEmailCollection.join('\n');
    }
});

 

HIT Helpful & Accept Solution !! 

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)

View solution in original post

11 REPLIES 11

Pravallika1
Giga Guru

Thank you so much all of you!!

These codes really worked for me @Akshay Gupta2 @Sohithanjan G 

But i am facing an issue when selected slushbucket is empty i.e,

If we add two values to selected slushbucket (e.g,Alaska , Alabama) and if i again move Alabama to available slushbucket the Display users field is updated correctly , then if i send that last value i.e, Alaska and check the Display users field, the Alaska value is not updated.

Please do provide a solution on this.

Please follow the screenshots:

1.Pravallika1_0-1708531442321.png

2.In this screenshot you can see 'Alabama' is updated in display users field

Pravallika1_1-1708531566379.png

3.In this screenshot you can see , even after i move back Alaska display users field is not updated-

Pravallika1_2-1708531668976.png

Please help me on this.

Thanks in advance!!

 

 

Hi @Pravallika1 ,

It seems like there might be an issue with the logic when selectedChoices is empty. Let's adjust the code to handle this scenario more effectively:

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === undefined || newValue === null) {
        // Clear display_users field when no selection is made
        g_form.setValue('display_users', '');
        return;
    }

    var initialChoices = ['dc8b769893a0021035e6b2ddfaba10cc','1ecb365893a0021035e6b2ddfaba1009','4bebf65893a0021035e6b2ddfaba104e','fc2cfe9893a0021035e6b2ddfaba10e4','7d3cb6909320021035e6b2ddfaba10aa','2e5c3a9893a0021035e6b2ddfaba1066','b66c769893a0021035e6b2ddfaba10d6','ea8cf6d893a0021035e6b2ddfaba101f','ee9cf69893a0021035e6b2ddfaba10c8'];
    var selectedChoices = g_form.getValue('test_users');
    var selectedChoicesArray = selectedChoices ? selectedChoices.split(',') : [];
    var states = [];

    for (var i = 0; i < initialChoices.length; i++) {
        if (selectedChoicesArray.indexOf(initialChoices[i]) == -1) {
            states.push(initialChoices[i]);
        }
    }

    var gaStates = new GlideAjax('u_AjaxUtils');
    gaStates.addParam('sysparm_name', 'getStatesDiff');
    gaStates.addParam('sysparm_states', states);
    gaStates.getXML(SetfieldValues);

    function SetfieldValues(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('display_users', answer);
    }
}

 

 


Please HIT helpful & Mark as Accepted Solution !!!

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)

@Sohithanjan G I shouldn't clear the display_users field when no selection is made instead I should populate all the states present in left slushbucket.

Please do help with code

Thanks in advance!!

@Sohithanjan G I also declared initialChoices array with sys ids in client script. Now i am unable to use gs.getProperty function in client script. Can you please help me how to code this without using sys ids in client script

Pravallika1
Giga Guru

Hi Guys!!

Thank you all so much for your response, it is really helpful to achieve my initial solution. 

Please do help me with the continuation requirement which i posted in the question - To map values from list collector variable to a ch... - ServiceNow Community

Please do help me with code and solution.

Thanks in Advance!!