We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Code issue

keepitsimpl
Tera Contributor

hello team,
the below code is not working for me . The code is to change the sub-category based on the category selection.
Can u help me find the errors

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
if(g_form.getValue("id")=='category')
{
    if(newValue=='hardware')
    g_form.setValue("subcategory","laptop");
    else if(newValue=='software')
    g_form.setValue("subcategory","OS");
    else if(newValue=='')
    {
    g_form.showFieldMsg("subcategory","please fill vaue of category","info",true);
    var y = new Date();
    g_form.setValue('opened_at',y);
    g_form.setValue('caller_id', g_user.userName());
    return false;
    }
}
}
1 REPLY 1

Amitoj Wadhera
Kilo Sage

Hi @keepitsimpl ,

 

Here is the corrected version of your script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    function formatDateTime(date) {
        var year = date.getFullYear();
        var month = ('0' + (date.getMonth() + 1)).slice(-2); 
        var day = ('0' + date.getDate()).slice(-2);
        var hours = ('0' + date.getHours()).slice(-2); 
        var minutes = ('0' + date.getMinutes()).slice(-2); 
        var seconds = ('0' + date.getSeconds()).slice(-2); 
        var formattedDate = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
        return formattedDate;
    }

    if (g_form.getValue("id") == 'category') {
        if (newValue == 'hardware')
            g_form.setValue("subcategory", "laptop");
        else if (newValue == 'software')
            g_form.setValue("subcategory", "OS");
        else if (newValue == '') {
            g_form.showFieldMsg("subcategory", "please fill vaue of category", "info", true);
            var y = new Date();
            g_form.setValue('opened_at', formatDateTime(new Date()));
            g_form.setValue('caller_id', g_user.userName());
        }
    }
}

 

If you find my response helpful, please consider marking it as the 'Accepted Solution' and giving it a 'Helpful' rating. Your feedback not only supports the community but also encourages me to continue providing valuable assistance.

 

Thanks,

Amitoj Wadhera