How to fix the error "There is a JavaScript error in your browser console".

Afsar Hussain
Kilo Guru

Hi, 

I have created one catalog item in the Resolution field is populating by concatenating first six fields.

It is working fine in the catalog view but when I am testing this catalog item in the portal side I am getting this error- "There is a JavaScript error in your browser console". 

For this catalog item I have written 6 same onChange client scripts which I have mentioned below in this question.

I have also attached the catalog form as well as browser console error screenshot.

How can we fix this error?

The client script I have written is below-

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }
    else
        {
    var lang=g_form.getReference('language');
            
    var language=lang.id;
    var ctry=g_form.getReference('country');
            
    var country=ctry.iso3166_2;
    var region=g_form.getValue('region');
    var category=g_form.getValue('category');
    var subcategory=g_form.getValue('subcategory');
    var site=g_form.getValue('site');
            
    
    var fieldArray=[];
    fieldArray[0]=region;
    fieldArray[1]=category;
    fieldArray[2]= subcategory;
            if(language==undefined)
                {
                    g_form.addInfoMessage("inside if");
                    fieldArray[3]="not count";
                }
            else
                {
                    g_form.addInfoMessage("inside else");
                    fieldArray[3]=language;
                }
            if(country==undefined)
                {
                    fieldArray[4]="not count";
                }
            else
                {
                    fieldArray[4]=country;
                }
    fieldArray[5]=site;
    var i, count=0;
    for(i=0;i<=5;i++)
        {
            if(fieldArray[i]!="not count" && fieldArray[i]!='')
                {
                            count++;
                }    
        }
    g_form.addInfoMessage("value of count:" + count);
    var resolutionArray=[count];
    var k=0,j=0;
    for(j=0;j<6;j++)
        {
            
            if(fieldArray[j]!="not count")
                {
                        resolutionArray[k]=fieldArray[j];
                        k++;                    
                }       
        }


    g_form.addInfoMessage("value of k: " + k);
    var result='';
    var l=0;
    for(l=0;l<count;l++)
        {
             if(l!=count-1)
                 {
                   
                     result= result + resolutionArray[l] + "." ;
                     result=result.toLowerCase();
                 }
            else
                {
                   
                    result= result + resolutionArray[l];
                    result=result.toLowerCase();
                }
                    
          
            if(l==count-1)
                {
                    g_form.setValue('resolution', result);
                }
                    
        }

        }
}

1 ACCEPTED SOLUTION

Please try this below scripts 

Client Script: Write this script on Language field (on change)

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

var ga = new GlideAjax('getStandardFields');//this is the script include
ga.addParam("sysparm_name", "getFields"); //this is the function within the script include
ga.addParam("sysparm_language", newValue);
ga.addParam("sysparm_country",g_form.getValue('country'));
ga.getXML(getResponse);
}
function getResponse(response) {
var values = response.responseXML.documentElement.getAttribute('answer').toString().split('|');
var region=g_form.getValue('region');
 var category=g_form.getValue('category');
var subcategory=g_form.getValue('subcategory');
var site=g_form.getValue('site');

g_form.setValue('resolution field back end name',region+'.'+category+'.'+subcategory+'.'+values[0]+'.'+values[1]+'.'+site);

}
}

Script include:

var getStandardFields = Class.create();
getStandardFields.prototype = {
 getFields : function() {
var standardFields = new GlideRecord('your language table name');
standardFields.addQuery('sys_id', this.getParameter('sysparm_language'));
standardFields.query();
if(standardFields.next()) {
var country = new GlideRecord('your country table name');
country.addQuery('sys_id',this.getParameter('sysparm_country'))
country.query();
if(country.next())
{
return standardFields.id.toString()+'|'+country.iso3166_2.toString();
}
}
 return '';
 },
 type: 'getStandardFields'


};

Please mark my answer correct if it helped you

View solution in original post

13 REPLIES 13

Talvin Singh
Tera Expert

Hello Afsar,

Kindly check the below link:

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0696644

Thanks

I have gone through this link but I am not getting to what I should update my client script?

Mohith Devatte
Tera Sage
Tera Sage

Hello,

After seeing the error looks like there is a problem with this line 

var language=lang.id;

are you sure the backend name of ID field that you are trying to access is correct?

No, Mohith.

The backend name is 'langauge'.

I am storing the reference of this field in lang variable. See the below code-

var lang=g_form.getReference('language');

The reference is stored in lang variable then I am assigned language id field value to some other variable i.e language . See below line-

Var language= lang.id;

Infact, this is working fine when I am using Try it button in catalog item form. But in the portal it is not working.