I have 2 Forms (of 2 entities: Backlog & Pipeline). Each of them has a subgrid for a third Entity called Cashflow where the relationships between Backlog and Cashflow and Pipeline and Cashflow are both (1:N)
Cashflow Records have a Choice field called Year (a list of Years: [2020, 2021, 2022, 2023, 2024, 2025).
Once the user clicks on the Cashflow Subgrid's 'Create New Cashflow' button, in the Backlog Main Form for example, if the subgrid contains 1 or more records the Year Field in the Quick Create Form that opens should be set to the next year choice automatically. Image:
The JS code I am trying is below, launches OnLoad of the Cashflow Quick Create Form, and is correctly setting the next year in the Quick Create Form, but after saving the record and refreshing the page or after adding a new Cashflow record related to this Backlog the Year of the record in the Subgrid is incrementing alone and changing to a wrong value
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(); var optionSetValues = formContext.getAttribute("cra1c_yearchoice").getOptions(); //Backlog Record (not Pipeline) 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 countOfRelatedRecords = results.entities.length; if (countOfRelatedRecords !=0){ var followingYearId = results.entities[0]['cra1c_yearchoice'] 1; formContext.getAttribute("cra1c_yearchoice").setValue(followingYearId); } else{ formContext.getAttribute("cra1c_yearchoice").setValue(null); } }, function (error) { console.log(error.message); } ) } }
I've been stuck on this for a while so if anyone could please help I would greatly appreciate it.