Dan
Dan

Reputation: 17

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);
    }
  }
}

Upvotes: 1

Views: 255

Answers (2)

kol
kol

Reputation: 28688

function selectedNeedSupport(executionContext) {
  const selectedItem = formContext.getAttribute("neededsupport").getValue();
  const formContext = executionContext.getFormContext();
  formContext.getControl("comments").setVisible(selectedItem?.includes(9));
  formContext.ui.tabs.get("tab_Application").setVisible(selectedItem?.includes(8));  
}

Upvotes: 1

mplungjan
mplungjan

Reputation: 177830

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

Upvotes: 0

Related Questions