Basic JavaScript Help: Auto-Populate From Another List


Userlevel 4
Badge +10

Hi Folks,

I have some JavaScript that works in part but I want to add a little complexity and am not very god with JS code.

NWF$(function() {
//  When an employee is selected using llu_Western_Employee, this runs upon change
NWF$('#'+ jsvar_EmployeeLookup_LLU).on('change', function()
{
  setTimeout(function() {
   NWF$('#'+ jsvar_EmpID_TXT).val(NWF$('#'+ jsvar_EmpID_CAL).val());
   NWF$('#'+ jsvar_LastName_TXT).val(NWF$('#'+ jsvar_LastName_CAL).val());
   NWF$('#'+ jsvar_FirstName_TXT).val(NWF$('#'+ jsvar_FirstName_CAL).val());
   NWF$('#'+ jsvar_MI_TXT).val(NWF$('#'+ jsvar_MI_CAL).val());
   NWF$('#'+ jsvar_Section_TXT).val(NWF$('#'+ jsvar_Section_CAL).val());
   NWF$('#'+ jsvar_DateOfHire_DTE).val(NWF$('#'+ jsvar_DateOfHire_CAL).val());
   NWF$('#'+ jsvar_Phone_TXT).val(NWF$('#'+ jsvar_Phone_CAL).val());
   NWF$('#'+ jsvar_Gender_CHO).val(NWF$('#'+ jsvar_Gender_CAL).val());
   NWF$('#'+ jsvar_PassportNumber_TXT).val(NWF$('#'+ jsvar_PassportNumber_CAL).val());
   NWF$('#'+ jsvar_PassportExpiration_DTE).val(NWF$('#'+ jsvar_PassportExpiration_CAL).val());
   NWF$('#'+ jsvar_SupervisorEmail_TXT).val(NWF$('#'+ jsvar_SupervisorEmail_CAL).val());
  },2000);
});
//  When a contract period is selected using llu_ContractPeriod, this runs upon change
NWF$('#'+ jsvar_CPLookup_LLU).on('change', function()
{
  setTimeout(function() {
   NWF$('#'+ jsvar_RRAvailable_TXT).val(NWF$('#'+ jsvar_RRAvailable_CAL).val());
   NWF$('#'+ jsvar_TravelDayAvailable_TXT).val(NWF$('#'+ jsvar_TravelDayAvailable_CAL).val());
   NWF$('#'+ jsvar_PaidAirfareAvailable_TXT).val(NWF$('#'+ jsvar_PaidAirfareAvailable_CAL).val());

   NWF$('#'+ jsvar_SickAvailable_100_TXT).val(NWF$('#'+ jsvar_SickAvailable_100_CAL).val());
   NWF$('#'+ jsvar_SickAvailable_75_TXT).val(NWF$('#'+ jsvar_SickAvailable_75_CAL).val());
   NWF$('#'+ jsvar_SickAvailable_50_TXT).val(NWF$('#'+ jsvar_SickAvailable_50_CAL).val());
   NWF$('#'+ jsvar_SickAvailable_25_TXT).val(NWF$('#'+ jsvar_SickAvailable_25_CAL).val());
   NWF$('#'+ jsvar_SickAvailable_LWOP_TXT).val(NWF$('#'+ jsvar_SickAvailable_LWOP_CAL).val())
  
   NWF$('#'+ jsvar_SickIssued_100_TXT).val(NWF$('#'+ jsvar_SickIssued_100_CAL).val());
   NWF$('#'+ jsvar_SickIssued_75_TXT).val(NWF$('#'+ jsvar_SickIssued_75_CAL).val());
   NWF$('#'+ jsvar_SickIssued_50_TXT).val(NWF$('#'+ jsvar_SickIssued_50_CAL).val());
   NWF$('#'+ jsvar_SickIssued_25_TXT).val(NWF$('#'+ jsvar_SickIssued_25_CAL).val());
   NWF$('#'+ jsvar_SickIssued_LWOP_TXT).val(NWF$('#'+ jsvar_SickIssued_LWOP_CAL).val())
  },2000);
});
});

There are basically two parts to this code block. The first half populates employee data when the control with the Client ID JavaScript variable "jsvar_EmployeeLookup_LLU" is changed. The lines with code like "jsvar_EmpID_CAL" are pointing at calculated value controls.

The second half of the code should populate available leave and sick time data when the control with the Client ID JavaScript variable "jsvar_CPLookup_LLU" is changed.

The problem I am running into is that when the jsvar_EmployeeLookup_LLU is changed, ALL of the calculated value controls populate. the Leave and Sick Time values do not wait for second list lookup control to change.

I suspect that this is just a rookie mistake in my code but I hope someone can see a problem.

Thanks and Regards,

Patrick


7 replies

Badge +7

HI,

Just to understand better:

U said:

The problem I am running into is that when the jsvar_EmployeeLookup_LLU is changed, ALL of the calculated value controls populate. the Leave and Sick Time values do not wait for second list lookup control to change.

Y are they populating? Seems they are linked to  jsvar_EmployeeLookup_LLU, right? 

If so, why do you want to wait for jsvar_CPLookup_LLU to be changed in order to apply the second part of your code?

BR,

Philip

Userlevel 5
Badge +14
The problem I am running into is that when the jsvar_EmployeeLookup_LLU is changed, ALL of the calculated value controls populate. the Leave and Sick Time values do not wait for second list lookup control to change.

 

do you mean that (eg.) jsvar_RRAvailable_CAL control recalculates after jsvar_EmployeeLookup_LLU is changed?

but you want it to recalculate on jsvar_CPLookup_LLU change?

 

calculated value control recalculates once a control which its formula depends on changes.

so likely jsvar_RRAvailable_CAL's formula depends on jsvar_EmployeeLookup_LLU value, but not on jsvar_CPLookup_LLU.

if you want it to recalculate on jsvar_CPLookup_LLU change you have to make it dependent on that control.

 

 

 

could you show us one of formulas you think that do not recalculates correctly?

Userlevel 4
Badge +10

Hi Philip

I have a two list lookup controls on the form and the first goes to a list that has the employee information. It collects that then populates the appropriate fields. I want the second LLU control discriminate between records in the TimeBank list based on the value in the first LLU and the value selected in the second, ansd then populate the leave and sick time fields.

An employee has one record in the tbl_Westerners list. pretty simple; just have llu_Western_Employee control select a record based on the employee's FullName field.

An employee has several TimeBank records. Each record will have an allotment of leave and sick time for each contract period. Within a TimeBank record, there is a CP field to display the contract period (BASE, CP01, CP01, CP03, etc.) and the employee's FullName.

FIGURE 1: TimeBank recordset sample.

I need the second LLU control (llu_ContractPeriod) to select the correct record in the TimeBank based on FullName from the first LLU control and the Contract Period from the second.

FIGURE 2: Display of the employee info panel with the Contract Period List Lookup Control active.

Then, when the absence type (Sick or Leave) is selected, the appropriate input panels are revealed...

FIGURE 3: Request Info panel for Leave time exposed after absence selection is chosen.

I am actually struggling with two things right now, the first is: getting the Contract Period list lookup control to select a TimeBank record based on the value in the first list lookup (employee) and the second: getting the code block to not populate the Leave/Sick time values until the Contract Period is chosen.

Does this help? Sorry for the lack of clarity.

EDIT: You may notice in figure 1 a calculated field (LookupKey is the field name) that concatenates the Contract Period and the Employee ID. I put this there as I was tinkering to see if I could figure out issue 1: "getting the Contract Period list lookup control to select a TimeBank record". The reason I am not using EmpID to get the employee Info is that it was not a meaningfull enough value to display for employees in the dropdown. Is there a better way to do this as well?

Userlevel 4
Badge +10

Hi Marian Hatala‌,

Here is a screenshot of the formula for the cv_RRAvailable calculated value control. I think I see what you are getting at. Am I confusing things here because I have not solved the problem of discriminating between TimeBank records yet (issue referenced in my response to Philip's post)?

Userlevel 5
Badge +14

so if I got you correctly you would need formula like follows

lookup("/ASP/PMO/AdminWebDB|TimeBank",
               "FirstColumnFromTBSample",
               parseLookup(llu_ContractPeriod,true) + "_" + lookup("/ASP/PMO/AdminWebDB|tbl Westerners","ID",llu_Western_Employee,"empID"),
               "R&R Available") ‍‍‍‍

it ensures it recalculates after either of llu_ContractPeriod or llu_Western_Employee is changed, as well as it searches for an item(s) in TimeBank list that match both of the selection.

while one of lookups is empty, the formula doesn't match any TimeBank item and so leave details will not be populated (despite the formula was recalculated)

Userlevel 4
Badge +10

I think you are on the right track but this formula renders #Value! in the calculated value and the text control. Here is the actual formula after I added the actual references...

lookup("/ASP/PMO/AdminWebDB|TimeBank",
                    "LookupKey",
                    parseLookup(llu_ContractPeriod,true) + "_" + lookup("/ASP/PMO/AdminWebDB|tbl Westerners","ID",llu_Western_Employee,"EmpID"),
                    "R&R Available"

Thanks!

Userlevel 4
Badge +10

Marian Hatala‌!!!

Please disregard my last post. YOU DID IT!

I copied and pasted without regarding the spelling carefully enough. Your code had "tbl Westerners" and the line called for "tbl_Westerners" with an underscore.

It is working!

YOU ROCK!

Reply