on change client script is not working for particular catalog item

keval3
Tera Contributor

Hi All,

I have written below client script but it is not working for my catalog item it is apply for all catalog item.

kindly let me know what is issue.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue == '') {

        return;

    }

var catitem = g_form.getValue("u_catalog_item");

alert(catitem);

 

   

   

    //g_form.addInfoMessage("line 9 testing");

 

    if(catitem == '9c9df05597661e500cb57bae2153afe2')

    alert("askhr");

    {

 

    if(g_form.getValue('state') =="7")

    {

g_form.setValue("close_notes", "123455");

g_form.setValue("u_knowledge_article", "no");

g_form.setValue("u_support_type", "desk");

g_form.setValue("u_resolution_code", "Solved (Permanently)");

 

g_form.setValue("u_via_kb_article", "no");

g_form.setValue("u_via_kb_article", "no");

    }

 

    //Type appropriate comment here, and begin script below

 

}

}

 

Thanks & Regards

KP

11 REPLIES 11

@keval3 

you want this script to run only for 1 catalog item right?

then have you given the correct catalog item sysId in the comparison?

also are you sure the section is visible on the form for that catalog item?

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

Hi Ankur,

I have given correct sys_id.

Thanks & Regards

KP

@keval3 

is it getting inside the IF?

any other script is setting the fields with other value?

out of 5 fields which fields are not getting set?

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

@keval3 

Hope you are doing good.

Did my reply answer your question?

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

Runjay Patel
Giga Sage

Hi @keval3 ,

 

Your code is not proper, use below code.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    // Exit the script if it is loading or if the new value is empty
    if (isLoading || newValue === '') {
        return;
    }

    // Check if the catalog item matches the specific item
    if (g_form.getValue("sys_id") === '9c9df05597661e500cb57bae2153afe2') {
        alert("This is the correct catalog item!");

        // Perform further checks for specific field values
        if (g_form.getValue('state') === "7") {
            g_form.setValue("close_notes", "123455");
            g_form.setValue("u_knowledge_article", "no");
            g_form.setValue("u_support_type", "desk");
            g_form.setValue("u_resolution_code", "Solved (Permanently)");
            g_form.setValue("u_via_kb_article", "no");
        }
    }
}

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------