Search code examples
javascriptdynamics-crmdynamic-programming

Simplify JavaScript in dynamics365


I have a function in js and inside that I have two condition checks, how can I simplify this. I am using this in d365

Looks like i have written complex code, can you help to simplify

function selectedNeedSupport(executionContext) {
  var formContext = executionContext.getFormContext();
  let selectedItem = formContext.getAttribute("neededsupport").getValue();
  if (selectedItem != null && selectedItem.includes(9)) {
    formContext.getControl("comments").setVisible(true);
    if (selectedItem != null && selectedItem.includes(8)) {
      formContext.ui.tabs.get("tab_Application").setVisible(true);
    } else {
      formContext.ui.tabs.get("tab_Application").setVisible(false);
    }
  } else {
    formContext.getControl("comments").setVisible(false);
    if (selectedItem != null && selectedItem.includes(8)) {
      formContext.ui.tabs.get("tab_Application").setVisible(true);
    } else {
      formContext.ui.tabs.get("tab_Application").setVisible(false);
    }
  }
}

Solution

  • You have tests and booleans. Use the tests instead of true and false:

    const nine = selectedItem?.includes(9)    
    const eight = selectedItem?.includes(8)
    
    formContext.getControl("comments").setVisible(nine && !eight);
    

    for example