[RESOLVED] Auto-populate fields

Hiya,

I fixed the problem with regex.

As for this: fd.field('FieldName').value.LookupValue

The code runs fine but errors are sill present in the console.

Here is the code that is powering that section:

fd.spRendered(function() {
    
    fd.field('RERostering').disabled = true
	fd.field('Question_1').$on('change', function() {
 	fd.field('RERostering').value = fd.field('Question_1').value; 
	})
    
    fd.field('RLeavingTrust').disabled = true
	fd.field('Question_2').$on('change', function() {
 	fd.field('RLeavingTrust').value = fd.field('Question_2').value; 
	})
    
    fd.field('RTermAssignment').disabled = true
	fd.field('Question_3').$on('change', function() {
 	fd.field('RTermAssignment').value = fd.field('Question_3').value; 
	})
        
    fd.field('RSupervisorEmail').disabled = true
	fd.field('Supervisor_Email_Address').$on('change', function() {
 	fd.field('RSupervisorEmail').value = fd.field('Supervisor_Email_Address').value; 
	})
    
    fd.field('RSupervisorFirstName').disabled = true
	fd.field('Supervisor_First_Name').$on('change', function() {
 	fd.field('RSupervisorFirstName').value = fd.field('Supervisor_First_Name').value; 
	})
    
    fd.field('RSupervisorLastName').disabled = true
	fd.field('Supervisor_Last_Name').$on('change', function() {
 	fd.field('RSupervisorLastName').value = fd.field('Supervisor_Last_Name').value; 
	})
    
	fd.field('RTitle').disabled = true
	fd.field('Titles').$on('change', function() {
 	fd.field('RTitle').value = fd.field('Titles').value; 
	})
    
    fd.field('RFirstName').disabled = true
	fd.field('First_Name').$on('change', function() {
 	fd.field('RFirstName').value = fd.field('First_Name').value; 
	})
    
    fd.field('RLastName').disabled = true
	fd.field('Last_Name').$on('change', function() {
 	fd.field('RLastName').value = fd.field('Last_Name').value; 
	})
    
    fd.field('RAssignNum').disabled = true
	fd.field('Assignment_Number').$on('change', function() {
 	fd.field('RAssignNum').value = fd.field('Assignment_Number').value; 
	})
    
    fd.field('REmailAddress').disabled = true
	fd.field('Email_Address').$on('change', function() {
 	fd.field('REmailAddress').value = fd.field('Email_Address').value; 
	})
    
    fd.field('RPayBand').disabled = true
	fd.field('Pay_Band').$on('change', function() {
 	fd.field('RPayBand').value = fd.field('Pay_Band').value; 
	})
    
    fd.field('RCostCentreCode').disabled = true
	fd.field('Cost_Centre').$on('change', function() {
 	fd.field('RCostCentreCode').value = fd.field('Cost_Centre').value; 
	})
    
    fd.field('RDepartment').disabled = true
	fd.field('Department').$on('change', function() {
 	fd.field('RDepartment').value = fd.field('Department').value.LookupValue;
	})
    
    //This code will retrieve the auto-populated values from Wizard 2
    fd.field('RSpeciality').disabled = true
    fd.field('Speciality').$on('change', function(value){
   //check that field has no additional data
    if(typeof value == 'number'){
        fd.field('Speciality').reloadValue().then(function(){        
    	    fd.field('RSpeciality').value = fd.field('Speciality').value.LookupValue;        
        })
        
    }
    else if(value != null) {
        fd.field('RSpeciality').value = value.LookupValue
    }
    })
    
    //Directorate
    fd.field('RDirectorate').disabled = true
    fd.field('Directorate').$on('change', function(value){
   //check that field has no additional data
    if(typeof value == 'number'){
        fd.field('Directorate').reloadValue().then(function(){        
    	    fd.field('RDirectorate').value = fd.field('Directorate').value.LookupValue;        
        })
        
    }
    else if(value != null) {
        fd.field('RDirectorate').value = value.LookupValue
    }
    })
    
    //Division
    fd.field('RDivision').disabled = true
    fd.field('Division').$on('change', function(value){
   //check that field has no additional data
    if(typeof value == 'number'){
        fd.field('Division').reloadValue().then(function(){        
    	    fd.field('RDivision').value = fd.field('Division').value.LookupValue;        
        })
        
    }
    else if(value != null) {
        fd.field('RDivision').value = value.LookupValue
    }
    })
    
    //Hospital
    fd.field('RHospital').disabled = true
    fd.field('Hospital').$on('change', function(value){
   //check that field has no additional data
    if(typeof value == 'number'){
        fd.field('Hospital').reloadValue().then(function(){        
    	    fd.field('RHospital').value = fd.field('Hospital').value.LookupValue;        
        })
        
    }
    else if(value != null) {
        fd.field('RHospital').value = value.LookupValue
    }
    })
    
    fd.field('RStaffGroup').disabled = true
	fd.field('Main_Staff_Group').$on('change', function() {
 	fd.field('RStaffGroup').value = fd.field('Main_Staff_Group').value; 
	})

    fd.field('RTerminationDate').disabled = true
	fd.field('Employee_Termination_Date').$on('change', function() {
 	fd.field('RTerminationDate').value = fd.field('Employee_Termination_Date').value.toDateString(); 
	})
    
    fd.field('RLastWorkingDay').disabled = true
	fd.field('Employee_Last_Working_Day').$on('change', function() {
 	fd.field('RLastWorkingDay').value = fd.field('Employee_Last_Working_Day').value.toDateString(); 
	})
    
    fd.field('RReasonForDifference').disabled = true
	fd.field('Reason_For_Difference').$on('change', function() {
 	fd.field('RReasonForDifference').value = fd.field('Reason_For_Difference').value; 
	})
    
    fd.field('RLeavingReason').disabled = true
	fd.field('Leaving_Reason').$on('change', function() {
 	fd.field('RLeavingReason').value = fd.field('Leaving_Reason').value.LookupValue; 
	})
    
    //ResignationType
    fd.field('RResignationType').disabled = true
    fd.field('Resignation_Type').$on('change', function(value){
   //check that field has no additional data
    if(typeof value == 'number'){
        fd.field('Resignation_Type').reloadValue().then(function(){        
    	    fd.field('RResignationType').value = fd.field('Resignation_Type').value.LookupValue;        
        })
        
    }
    else if(value != null) {
        fd.field('RResignationType').value = value.LookupValue
    }
    })
    
    fd.field('RDestinationOnLeaving').disabled = true
	fd.field('Destination_On_Leaving').$on('change', function() {
 	fd.field('RDestinationOnLeaving').value = fd.field('Destination_On_Leaving').value; 
	})
    
    fd.field('RNHSOrganisationLeavingTo').disabled = true
	fd.field('NHS_Organisation_Leaving_To').$on('change', function() {
 	fd.field('RNHSOrganisationLeavingTo').value = fd.field('NHS_Organisation_Leaving_To').value.LookupValue; 
	})
    
    fd.field('RNHSOrganisationLeavingTo').disabled = true
	fd.field('NHS_Organisation_Leaving_To').$on('change', function() {
 	fd.field('RNHSOrganisationLeavingTo').value = fd.field('NHS_Organisation_Leaving_To').value.LookupValue; 
	})
    
    //ResignationType
    fd.field('RVPD').disabled = true
    fd.field('VPD').$on('change', function(value){
   //check that field has no additional data
    if(typeof value == 'number'){
        fd.field('VPD').reloadValue().then(function(){        
    	    fd.field('RVPD').value = fd.field('VPD').value.LookupValue;        
        })
        
    }
    else if(value != null) {
        fd.field('RVPD').value = value.LookupValue
    }
    })
    
    fd.field('RHolidayHoursDue').disabled = true
	fd.field('Holidays_Hours_Due').$on('change', function() {
 	fd.field('RHolidayHoursDue').value = fd.field('Holidays_Hours_Due').value; 
	})
    
    fd.field('RHolidayHoursOverpaid').disabled = true
	fd.field('Holiday_Hours_Overpaid').$on('change', function() {
 	fd.field('RHolidayHoursOverpaid').value = fd.field('Holiday_Hours_Overpaid').value; 
	})
      
    fd.field('RLieuHoursOutstanding').disabled = true
	fd.field('Lieu_Hours_Outstanding').$on('change', function() {
 	fd.field('RLieuHoursOutstanding').value = fd.field('Lieu_Hours_Outstanding').value; 
	})
    
    fd.field('RPaymentsLieuNotice').disabled = true
	fd.field('Payments_Made_In_Lieu_Notice').$on('change', function() {
 	fd.field('RPaymentsLieuNotice').value = fd.field('Payments_Made_In_Lieu_Notice').value; 
	})
    
    fd.field('RDateLeavingPost').disabled = true
	fd.field('Date_Employee_Leaving_Post').$on('change', function() {
 	fd.field('RDateLeavingPost').value = fd.field('Date_Employee_Leaving_Post').value.toDateString(); 
	})
});

Hello @DryChips,

The code has no syntax errors, that is all I can tell.
Debug the part of the code where you are getting LookupValue with debugger or test the code form the console.

1 Like

Thanks Margarita, I will test and let you know.

1 Like

Hi Margarita,

Sorry to keep annoying you.

So I have modified my code using regex and the only issue I have now is that when hit 'backspace', the field is still green and not erroring out when an invalid Cost-Centre number is entered.

Valid Cost Centre: A000003
Invalid Cost Centre: A00003

Here is my code:

 function validateCC(value){
//validate the entered value
if ((/^[A-Za-z]\d{6}$/).test(value)) {
  var filter = "Cost_x0020_Centre eq '" + value + "'";
  pnp.sp.web.lists.getByTitle("OrgHierarchy").items.filter(filter).getAll().then(function(items){
    if(items.length > 0){
    $($(fd.field('Cost_Centre').$el).find('input')[0]).attr('style', 'border-color: #6CEE5A; box-shadow: 0 0 5px #6CEE5A;');
    ccexists = true;
    
    }
    else{
    $($(fd.field('Cost_Centre').$el).find('input')[0]).attr('style', 'border-color:#ff0000; box-shadow: 0 0 5px #ff0000; Color:#ff0000;');
    ccexists = false;
    }
  });
}
};

Hello @DryChips,

If the entered value doesn't match the pattern, that means that the PnP request doesn't return anything. You need to add else condition for the regex pattern check:

 function validateCC(value){
//validate the entered value
if ((/^[A-Za-z]\d{6}$/).test(value)) {
  var filter = "Cost_x0020_Centre eq '" + value + "'";
  pnp.sp.web.lists.getByTitle("OrgHierarchy").items.filter(filter).getAll().then(function(items){
    if(items.length > 0){
    $($(fd.field('Cost_Centre').$el).find('input')[0]).attr('style', 'border-color: #6CEE5A; box-shadow: 0 0 5px #6CEE5A;');
    ccexists = true;
    }

  });
}    

else{
    $($(fd.field('Cost_Centre').$el).find('input')[0]).attr('style', 'border-color:#ff0000; box-shadow: 0 0 5px #ff0000; Color:#ff0000;');
    ccexists = false;
}
};
1 Like

Awesome! This has fixed the issue. The final little bug is as soon as I enter a value, it errors out. :joy:

This is such a pain in the backside :rofl:

@DryChips,

As an option, you can add a field validator. In this case, the field will be validated against RegEx pattern when a user leaves a field.

And to check if Cost Center is present, use the function you've shared, but instead show/hide the error message within the field.

2 Likes