The CreatorCon Call for Content is officially open! Get started here.

TypeError: Cannot read properties of undefined (reading 'employee_group')

SM_Toronto
Tera Guru

Hi SN Community,

 

I have an issue with my script (Client Script onChange - see screenshot#1). Here's how it works:

1) 'select_options' allows a number and verifies if it is within the Option Number (this works well)

2) After selecting, it loads the 3 identified variables respectively (this also works well).

 

The issue is when I select the third option, it throws a error (see screenshot #2; also see the video below) 

 

Any tips or leads is much appreciated.

Thanks,

SM

(screenshot#1)

SM_Toronto_0-1722876752507.png

 

(screenshot#2)

SM_Toronto_1-1722877256092.png

VIDEO: https://vimeo.com/995102500/a150a4abb0?share=copy

 

1 ACCEPTED SOLUTION

SM_Toronto
Tera Guru

Hi Brad,

For whatever reason, I assigned variables the 3 values to be loaded (first 3 lines here) and for whatever reason, the error message is gone...no need for a N+1 dummy data.

                var empGroup = arrJsonSO[myIndex].employee_group;
                var myMinSalRange = arrJsonSO[myIndex].min_sr;
                var myMaxSalRange = arrJsonSO[myIndex].max_sr;
                g_form.setValue("u_ee_grp", empGroup);
                g_form.setValue("u_min_salary_range", myMinSalRange);
                g_form.setValue("u_max_salary_range", myMaxSalRange);

I guess it would be nice to know why but in any case, the error message is gone. (all good now.)

Thanks, SM 

View solution in original post

2 REPLIES 2

Brad Bowman
Kilo Patron
Kilo Patron

I recreated the MRVS and Catalog Item variables in my PDI, re-typed some of the Client Script, and entered some dummy-data in three rows on the MRVS and it works fine for me in the native UI and Service Portal.  The screenshots and video are great to explain and show the issue, but next time post a script using the insert code (</>) icon to allow for easier troubleshooting.  Here's the script I used:

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

	var myIndex = Number(g_form.getValue("select_options")) - 1;
	var myMRVS = g_form.getValue("salary_range_options");
	var arrJsonSO = JSON.parse(myMRVS);
	var lenJsonSO = arrJsonSO.length -1;
	if (myIndex < 0 || myIndex > lenJsonSO) {
		g_form.showFieldMsg("select_options", "out of range", "error");
	} else {
		g_form.setValue("u_ee_grp", arrJsonSO[myIndex].employee_group);
		g_form.setValue("u_min_salary_range", arrJsonSO[myIndex].min_sr);
		g_form.setValue("u_max_salary_range", arrJsonSO[myIndex].max_sr);
	}
}

It looks like you are auto-loading the MRVS onLoad of the Catalog Item form?  Try adding data for a fourth row to see if you still get the error on row 3 and/or 4.  Try disabling this, adding the three rows manually, then see if you get the same error.  If not, that points to something amiss with the load - extra characters or a syntax or method that seems to get the job done, but does not result in valid data in the MRVS.  If I got the same error I was going to try adding some alerts on myMRVS, myIndex,... to see if anything looked wrong when selecting 3.

SM_Toronto
Tera Guru

Hi Brad,

For whatever reason, I assigned variables the 3 values to be loaded (first 3 lines here) and for whatever reason, the error message is gone...no need for a N+1 dummy data.

                var empGroup = arrJsonSO[myIndex].employee_group;
                var myMinSalRange = arrJsonSO[myIndex].min_sr;
                var myMaxSalRange = arrJsonSO[myIndex].max_sr;
                g_form.setValue("u_ee_grp", empGroup);
                g_form.setValue("u_min_salary_range", myMinSalRange);
                g_form.setValue("u_max_salary_range", myMaxSalRange);

I guess it would be nice to know why but in any case, the error message is gone. (all good now.)

Thanks, SM