Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

on change client script using a synch call

AnjaliY27945328
Tera Contributor
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if(isLoading||newValue==''){
        return;
    }
 
var ci=g_form.getReference('u_configuration_item',getCIdetails);
function getCIdetails(ci) // ci is object

{
g_form.getValue('u_asset_tag',ci.Asset_Tag);
g_form.getValue('u_serial_number',ci.Serial_Number);
}
}
 
the script is not working on changing ci value 
no auto populate value of asset tag and serial number
pls check and tell me where I am wrong
5 REPLIES 5

Astik Thombare
Tera Sage

Hi @AnjaliY27945328 ,

 

The issue with your script lies in the way you are trying to set the values for u_asset_tag and u_serial_number. The g_form.getValue method is used to retrieve values, not to set them. Instead, you should use g_form.setValue to set the values of the fields.

 

Here is the corrected script:

 

 

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

    var ci = g_form.getReference('u_configuration_item', getCIdetails);

    function getCIdetails(ci) {
        // Ensure ci object is defined and has the properties you want to set
        if (ci) {
            g_form.setValue('u_asset_tag', ci.asset_tag );
            g_form.setValue('u_serial_number', ci.serial_number);
        }
    }
}

 

 

In this script, I replaced g_form.getValue with g_form.setValue to set the values of u_asset_tag and u_serial_number fields with the corresponding values from the ci object. Additionally, I added checks to ensure that the ci object is defined before trying to access its properties.

 

If my reply helped with your issue please mark helpful 👍 and correct ✔️ if your issue is resolved.

 

By doing so you help other community members find resolved questions which may relate to an issue they're having

 

 

Thanks,

Astik Thombare

 

SN_Learn
Kilo Patron

Hi @AnjaliY27945328 ,

 

The getValue() is use to retrieves the string value of a specified field.

It takes only one parameter :

getValue(String fieldName)

 

In the above case you have to use setValue(), as shown below:

 

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

    var ci = g_form.getReference('u_configuration_item', getCIdetails);

    function getCIdetails(ci) 
    {
        g_form.setValue('u_asset_tag', ci.asset_tag);
        g_form.setValue('u_serial_number', ci.serial_number);
    }

}

 

Output:

SN_Learn_0-1720950887826.png

 

 

Mark this as Helpful / Accept the Solution if this helps

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

Deepak Shaerma
Mega Sage

Hi @AnjaliY27945328 

your script is mostly correct just need some modifications"

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

    g_form.getReference('u_configuration_item', CIdata);

    function CIdata(config) {
        if (config) {
            // Use g_form.setValue to set the values of the fields
            g_form.setValue('u_asset_tag', ci.u_asset_tag);
            g_form.setValue('u_serial_number', ci.u_serial_number);
        }
    }
}

Not applicable

Hi @AnjaliY27945328

I checked your script there is only one issues please find below code 

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

    var ci = g_form.getReference('u_configuration_item', getCIdetails);

    function getCIdetails(ci) {
        // Ensure ci object is defined and has the properties you want to set
        if (ci) {
            g_form.setValue('u_asset_tag', ci.asset_tag ); // Backend name of asset_tag or u_asset_tag
            g_form.setValue('u_serial_number', ci.serial_number); // Backend name of serial_number or u_serial_number
        }
    }
}

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards 

Sarthak