Set field value based on another field value

Valerie24
Tera Contributor

Hi,

 

I'm trying to create a Client Script that will set the value for a custom field based off of a value from another custom field.

 

Here is how it should work:

If u_essay_1_score is 5, then u_essay_1_points should display 33

If u_essay_1_score is 4, then u_essay_1_points should display 27

If u_essay_1_score is 3, then u_essay_1_points should display 21

If u_essay_1_score is 2, then u_essay_1_points should display 13

If u_essay_1_score is 1, then u_essay_1_points should display 5 

 

Any help with this script would be greatly appreciated!

 

Thank you, Valerie

3 ACCEPTED SOLUTIONS

Elijah Aromola
Mega Sage

The following client script should work to map the value.

var dataMap = {
    "5": "33", 
    "4": "27",
    "3": "21",
    "2": "13",
    "1": "5"
}

var value = g_form.getValue("u_essay_1_score");
g_form.setValue("u_essay_1_points", dataMap[value])

View solution in original post

SAI VENKATESH
Tera Sage
Tera Sage

Hi @Valerie24 

 

You can try the below Script:

On change client script (field : u_essay_1_score) and change the field names based on your requirement

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var score = g_form.getValue('essay_1_score');
    alert(score);
    if (score == '5') {
        g_form.setValue('essay_1_points', '33');
    } else if (score == '4') {
        g_form.setValue('essay_1_points', '27');
    } else if (score == '3') {
        g_form.setValue('essay_1_points', '21');
    } else if (score == '2') {
        g_form.setValue('essay_1_points', '13');
    } else if (score == '1') {
        g_form.setValue('essay_1_points', '1');
    }


    //Type appropriate comment here, and begin script below

}

 

 

 

Thanks and Regards

Sai Venkatesh

View solution in original post

Hari Prasath
Tera Guru

Hi @Valerie24 ,

 

You can achieve it by using onChange client script. You can choose the client script type as "onChange" and Field name as "Essay score". Copy paste the below script.

 

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

    //Type appropriate comment here, and begin script below
    switch (newValue) {
        case '1':
            g_form.setValue('u_essay_points', '53');
            break;
        case '2':
            g_form.setValue('u_essay_points', '54');
            break;
        default:
            g_form.setValue('u_essay_points', '55');
    }

}

When ever you change the Essay score, the Essay points will auto populate. Please let me know if you've any questions.

 

Please mark the answer helpful, if it helped you accordingly.

 

Regards,

Hari

View solution in original post

3 REPLIES 3

Elijah Aromola
Mega Sage

The following client script should work to map the value.

var dataMap = {
    "5": "33", 
    "4": "27",
    "3": "21",
    "2": "13",
    "1": "5"
}

var value = g_form.getValue("u_essay_1_score");
g_form.setValue("u_essay_1_points", dataMap[value])

SAI VENKATESH
Tera Sage
Tera Sage

Hi @Valerie24 

 

You can try the below Script:

On change client script (field : u_essay_1_score) and change the field names based on your requirement

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var score = g_form.getValue('essay_1_score');
    alert(score);
    if (score == '5') {
        g_form.setValue('essay_1_points', '33');
    } else if (score == '4') {
        g_form.setValue('essay_1_points', '27');
    } else if (score == '3') {
        g_form.setValue('essay_1_points', '21');
    } else if (score == '2') {
        g_form.setValue('essay_1_points', '13');
    } else if (score == '1') {
        g_form.setValue('essay_1_points', '1');
    }


    //Type appropriate comment here, and begin script below

}

 

 

 

Thanks and Regards

Sai Venkatesh

Hari Prasath
Tera Guru

Hi @Valerie24 ,

 

You can achieve it by using onChange client script. You can choose the client script type as "onChange" and Field name as "Essay score". Copy paste the below script.

 

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

    //Type appropriate comment here, and begin script below
    switch (newValue) {
        case '1':
            g_form.setValue('u_essay_points', '53');
            break;
        case '2':
            g_form.setValue('u_essay_points', '54');
            break;
        default:
            g_form.setValue('u_essay_points', '55');
    }

}

When ever you change the Essay score, the Essay points will auto populate. Please let me know if you've any questions.

 

Please mark the answer helpful, if it helped you accordingly.

 

Regards,

Hari