web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Javascript error in Model Driven App

(0) ShareShare
ReportReport
Posted on by 5

Hello!

Please could someone take a look at the below and potentially reveal an answer? Any help would be much appreciated!

I am trying to implement JS to only show the tabs relevant to the business process flow stage.

However, the formContext.ui.tabs.get is returning null:

pastedimage1638706794256v1.png

I have double checked the name of the tabs and business process flow stage name, so I am unsure why it is returning as null.

Code is below.

function onLoadMatt(executionContext) {
  var formContext = executionContext.getFormContext();
  formContext.data.process.addOnStageChange(onStageChangeMatt);
}

function onStageChangeMatt(executionContext) {
  var formContext = executionContext.getFormContext();
  var stage = formContext.data.process.getSelectedStage();
  var stageName = stage.getName();
 
  formContext.ui.tabs.get("tab_Create").setVisible(false);
  formContext.ui.tabs.get("tab_PI").setVisible(false);
  formContext.ui.tabs.get("tab_QR").setVisible(false);
  formContext.ui.tabs.get("tab_PO").setVisible(false);

  switch(stageName) {
    case "Create": {
      formContext.ui.tabs.get("tab_Create").setVisible(true);
      formContext.ui.tabs.get("tab_Create").setFocus();
      if(formContext.getAttribute("createdon").getValue() != null) {
        var tabObj = formContext.ui.tabs.get("tab_Create");
        var sectionObj = tabObj.sections.get("tab_Create_section_3");
        sectionObj.setVisible(true);
      }
      break;
    }
    case "PI": {
      formContext.ui.tabs.get("tab_PI").setVisible(true);
      formContext.ui.tabs.get("tab_PI").setFocus();
      break;
    }
    case "QS": {
      formContext.ui.tabs.get("tab_QR").setVisible(true);
      formContext.ui.tabs.get("tab_QR").setFocus();
      break;
  }
    case "PO": {
      formContext.ui.tabs.get("tab_PO").setVisible(true);
      formContext.ui.tabs.get("tab_PO").setFocus();
      break;
}
    default: {
      formContext.ui.tabs.get("tab_Create").setVisible(true);
      formContext.ui.tabs.get("tab_Create").setFocus();
      break;
    }
  }
}

I am calling the functions on the form onLoad event stage and I have checked that the execution context is being passed as the first parameter, and that the web resources are correct.

pastedimage1638707002590v2.png

I am unsure what to change in the code to make this right?

Thanks

I have the same question (0)
  • Suggested answer
    Venkatesh Nadar Profile Picture
    Microsoft Employee on at

    Hi,

    We can use the below article to investigate on form issues in Model Driven Apps-

    Troubleshoot form issues in model-driven apps (model-driven apps) - Power Apps | Microsoft Docs

    Hope this helps!

    Regards,

    Venkatesh N

  • ankit_singhal Profile Picture
    560 on at

    Can you try to call one function at a time on Onload and check the error. I am using formContext.ui.tabs.get this also and working fine for me.

  • Suggested answer
    NODAL Profile Picture
    860 on at

    Hi,

    Try using anonymous function and pass onload executionContext.

    Replace onload function with following.

    function onLoadMatt(executionContext) {

     var formContext = executionContext.getFormContext();

     formContext.data.process.addOnStageChange(function(e){onStageChangeMatt(executionContext)});

    }

  • Suggested answer
    Moh Helper Profile Picture
    250 on at

    Hi,

    I suppose that the problem is linked to the first line of the onStageChangeMatt method ("var formContext = executionContext.getFormContext();"). You are overwritting the formContext and so removing the correct formcontext set during the onloadMatt method.

    I will suggest you to create a global variable formContext (var formContext;)  and set it only in the onLoadMatt method. It will be available in all your other methods.

    Normally your problem will be resolved.

    Kr,

    M.Helper

  • Inogic Profile Picture
    764 on at

    Hi,

    One of the reasons behind the error is that the logical name of the tab ("tab_QR") could be incorrect. The reason is that you have mentioned it as “QS” in the switch case. I would suggest you to recheck the code with the tab’s logical name and use the debugger which will help you to find breakpoints. 

    Thanks!

  • Suggested answer
    Bipin D365 Profile Picture
    28,985 Moderator on at

    Hi,

    I have used same  code on my instance and it is working as expected.

    See below result.

    CR9.PNG

    Make sure you are providing the correct tab name. You have mentioned Name field from Tab properties instead of Label.

    CR10.PNG

    If you multiple Events added on form then move this method to top to see if it resolve your issue.

    Good Luck!

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 69 Most Valuable Professional

#2
ManoVerse Profile Picture

ManoVerse 66 Super User 2026 Season 1

#3
11manish Profile Picture

11manish 45

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans