Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

How to populate a field on change of another field's value?

Linda6
Mega Guru

Hello, 

I'm new to coding and I would very much like to ask you for help.

I need to develop a client script running on change request table, which checks for the value of the field 'cmdb_ci' and when it changes, the CI's GMP Approval Group's value will be populated in the 'u_gmp_approval_group' field.

Some CI's do have this GMP Approval Group filled in, some of them don't.

How do I do this correctly, please?

There's is an existing display BR for this, but the client wants to have it covered with client script instead.

Here's the form:

find_real_file.png

And here's my sad onChange client script code:

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

    if (!g_form.isNewRecord())
        return;

    var businessService = g_form.getValue('business_service');
    var cmdbCi = g_form.getValue('cmdb_ci');
    var serviceOffering = g_form.getValue('service_offering');
    var gmpApprovalGroup = g_form.getValue('u_gmp_approval_group');

//the code needs to compare the fields Business Service, Service Offering and Configuration item
//they have different priority, the highest is Configuration item and if that one has got a value, then that will be used
//if there is no value in Configuration item's GMP Approval Group field, then the value of the Service Offering's GMP Approval Group will be used
//if there is no value in the Service offering's GMP Approval Group field, then the value of the Business Service's GMP Approval will be used

    if (cmdbCi.u_gmp_approval_group !== '') {
        g_form.setValue('u_gmp_approval_group', cmdbCi.u_gmp_approval_group);
    } else if (cmdbCi.u_gmp_approval_group == '' && serviceOffering.u_gmp_approval_group !== '') {
        g_form.setValue('u_gmp_approval_group', serviceOffering.u_gmp_approval_group);
    } else if (cmdbCi.u_gmp_approval_group == '' && businessService.u_gmp_approval_group !== '') {
        g_form.setValue('u_gmp_approval_group', businessService.u_gmp_approval_group);
    }

}

Thank you very much for any help, very appreciaed

1 ACCEPTED SOLUTION

Jaspal Singh
Mega Patron
Mega Patron

Try below

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

    if (!g_form.isNewRecord())
        return;

    var businessService = g_form.getValue('business_service');

//changed
    var cmdbCi = g_form.getReference('cmdb_ci',callbackme);


    var serviceOffering = g_form.getValue('service_offering');
    var gmpApprovalGroup = g_form.getValue('u_gmp_approval_group');


function callbackme(cmdbCi)
{
 var grpis=cmdbCi.u_gmp_approval_group; //ensure here the u_gmp_approval_group field is the name of field on the cmdb_ci table

 g_form.setValue('u_gmp_approval_group',grpis); //ensure here the u_gmp_approval_group field is the name of field on change table
}


}

View solution in original post

2 REPLIES 2

Jaspal Singh
Mega Patron
Mega Patron

Try below

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

    if (!g_form.isNewRecord())
        return;

    var businessService = g_form.getValue('business_service');

//changed
    var cmdbCi = g_form.getReference('cmdb_ci',callbackme);


    var serviceOffering = g_form.getValue('service_offering');
    var gmpApprovalGroup = g_form.getValue('u_gmp_approval_group');


function callbackme(cmdbCi)
{
 var grpis=cmdbCi.u_gmp_approval_group; //ensure here the u_gmp_approval_group field is the name of field on the cmdb_ci table

 g_form.setValue('u_gmp_approval_group',grpis); //ensure here the u_gmp_approval_group field is the name of field on change table
}


}

Thank you very much, Jaspal!