RE: How can I hide a field in a Quick Create Form based on the number of records in a subgrid?
Hello Leah I was trying to achieve something else, that is instead of hiding the year when the Quick Create Form Loads, if it is not the first record being created, it sets the default selected value of the Year field to the next year.
This means if for a backlog I have 3 cashflow records: 1 for 2020, 1 for 2021, and 1 for 2022, when I click 'Create New Cashflow' from the subgrid, it sets the Year field in the Quick Create Form to 2023 by default.
function autoSelectNextYear(executionContext) {
//Initiated Form Context.
var formContext = executionContext.getFormContext();
//get backlog Lookup value & pipeline Lookup value
var backlog = formContext.getAttribute("cra1c_backlog_cashflow").getValue();
var pipeline = formContext.getAttribute("cra1c_pipeline_cashflow").getValue();
//Get Year Choice Options
var optionSetValues = formContext.getAttribute("cra1c_yearchoice").getOptions();
if (backlog != null && pipeline == null) {
var backlogId = backlog[0].id;
//Get all cashflows related to this backlog, and sort them by Year in descending order
Xrm.WebApi.retrieveMultipleRecords("cra1c_mn_cashflow", "?$filter=_cra1c_backlog_cashflow_value eq " backlogId "&$orderby=cra1c_yearchoice desc").then(
function success(results) {
var count = results.entities.length;
//If it's not the first record being entered
if (count != 0) {
//print count
console.log(count);
//Get the Next Year (1 after the latest Year entered) as a label
var nextYearAsInt = parseInt(results.entities[0]['cra1c_yearchoice@OData.Community.Display.V1.FormattedValue']) 1;
var nextYearAsString = nextYearAsInt.toString();
//Get the value of the fetched Next Year's Choice, and set it on the Year field
for (i = 0; i < optionSetValues.length; i ) {
if (optionSetValues[i].text == nextYearAsString) {
formContext.getAttribute("cra1c_yearchoice").setValue(optionSetValues[i].value);
}
}
}
else {
formContext.getAttribute("cra1c_yearchoice").setValue(null);
}
},
function (error) {
console.log(error.message);
}
)
}
}
I am using the code above but after saving and quitting the quick create form, the years are getting automatically incremented again and set to wrong values. It's obviously a logical error but any ideas on why it could be happening?