Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a JavaScript code that I can't seem to get working. I am working with Model Driven App and trying to prepopulate some fields using JavaScript depending on the ID of that Table. One of field Data type is Whole number and the other is Single Line of text with a Choice. Here is my code:

JavaScript
function getEmployeeCounts(executionContext) {
	var formContext = executionContext.getFormContext();

	var formType = GetFormType(formContext);

	if (formType == 2) {// execute only if formType == Update
		var company = formContext.getAttribute("digins_companyid").getValue();

		//Get RFP's Company
		if (company[0] != null) {
			var guid = company[0].id;
			var entType = company[0].entityType;

			try {
				//Get Number of Employees and Employees (Benefit Eligible) from Company
				Xrm.WebApi.retrieveRecord(entType, guid, "?$select=name,numberofemployees,digins_employeesbenefiteligible")
					.then(function (data) {
						setEmployeesForRFPAndSegmentSize(formContext, data);
					},
						function (error) {
							Xrm.Utility.alertDialog(error.message);
						});
			} catch (e) {
				Xrm.Utility.alertDialog(e.message);
			}
		}
	}


What I have tried:

JavaScript
<pre>function getEmployeeCounts(executionContext) {
	var formContext = executionContext.getFormContext();

	var formType = GetFormType(formContext);

	if (formType == 2) {// execute only if formType == Update
		var company = formContext.getAttribute("digins_companyid").getValue();

		//Get RFP's Company
		if (company[0] != null) {
			var guid = company[0].id;
			var entType = company[0].entityType;

			try {
				//Get Number of Employees and Employees (Benefit Eligible) from Company
				Xrm.WebApi.retrieveRecord(entType, guid, "?$select=name,numberofemployees,digins_employeesbenefiteligible")
					.then(function (data) {
						setEmployeesForRFPAndSegmentSize(formContext, data);
					},
						function (error) {
							Xrm.Utility.alertDialog(error.message);
						});
			} catch (e) {
				Xrm.Utility.alertDialog(e.message);
			}
		}
	}
}

function setEmployeesForRFPAndSegmentSize(formContext, data) {
	//digins_segmentsize options
	// 2-99 (867460000)
	// 100-249 (867460001)
	// 250-499 (867460002)
	// 500-999 (867460003)
	// 1000+ (100000000)

	//Get Get Employees (Benefit Eligible) value (which is optional)
	var employeesbenefiteligible = data.digins_employeesbenefiteligible;

	//Get Number of Employees (which is required)
	var numberofemployees = data.numberofemployees;

	// Use employeesbenefiteligible if populated otherwise use numberofemployees
	var employeeCountToEvaluate = employeesbenefiteligible ?? numberofemployees;

	//Get Employees for RFP
	var employeesForRFP = formContext.getAttribute("digins_employeesforrfp").getValue();
	
	//Determine if Employees for RFP is null
	if (employeesForRFP == null) {
		//Set employeesForRFP to employee count from company
		formContext.getAttribute("digins_employeesforrfp").setValue(employeeCountToEvaluate);
	}
	else {
		//Use Existing Employees For RFP
		employeeCountToEvaluate = employeesForRFP;
	}

	//Get SegmentSize control, value. and text
	var segmentSizeControl = formContext.getAttribute("digins_segmentsize");
	var currentSegmentSizeValue = segmentSizeControl.getValue();
	var currentSegmentSizeText = segmentSizeControl.getText();

	var newSegmentSizeText = "";
	var newSegmentSizeValue;

	//Determine Segment size for RFP based on Client's Employees (Benefit Eligible) value OR numberofemployees
	switch (true) {
		case (employeeCountToEvaluate < 100):
			newSegmentSizeText = '2-99';
			newSegmentSizeValue = 867460000;
			segmentSizeControl.setValue(867460000);
			break;
		case (employeeCountToEvaluate < 250):
			newSegmentSizeText = '100-249';
			newSegmentSizeValue = 867460001;
			segmentSizeControl.setValue(867460001);
			break;
		case (employeeCountToEvaluate < 500):
			newSegmentSizeText = '250-499';
			newSegmentSizeValue = 867460002;
			segmentSizeControl.setValue(867460002);
			break;
		case (employeeCountToEvaluate < 1000):
			newSegmentSizeText = '500-999';
			newSegmentSizeValue = 867460003;
			segmentSizeControl.setValue(867460003);
			break;
		case (employeeCountToEvaluate >= 1000):
			newSegmentSizeText = '1000+';
			newSegmentSizeValue = 100000000;
			segmentSizeControl.setValue(100000000);
			break;
		default:
			alert('employeeCountToEvaluate is out of range');
			break;
	}
}
	//Determine if the RFP's current Segment Size Value is other than suggested based on the number of employees TOOK OUT DUE TO AUTOMATION 9/19/2023
	/*if (currentSegmentSizeValue != newSegmentSizeValue) {
		setSegmentSizeConfirmation(segmentSizeControl, currentSegmentSizeText, newSegmentSizeText, newSegmentSizeValue);
	}
}
Posted
Comments
[no name] 25-Feb-24 16:39pm    
"Pre-populating" usually means using a "default" record for the "refresh" ("initial" display); instead of dealing with individual fields. It shouldn't feel like something extraordinary.
Computer Wiz99 25-Feb-24 17:33pm    
Okay, Thanks but I am trying to pre-populate from a table. Any why to do that in JavaScript?
Pete O'Hanlon 27-Feb-24 3:14am    
What does "can't seem to get working" mean? Are you not getting data back? Is one field populating, but not the other? We can't see your screen so we need additional information if we are to help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900