Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Hide/show section based on process stage - help on JavaScript

Posted on by 85

Hey there,

I wanted to implement a JavaScript that hides/shows form sections based on particular process stage.

This is what I figured out so far:

var stagetabs = new Array();
stagetabs["Projektkonzeption"] = ["Projektkonzeption"];
stagetabs["Partnersuche"] = ["Partnersuche"];
stagetabs["Skizze"] = ["Skizze"];
stagetabs["Antrag"] = ["Antrag"];
stagetabs["Antragsergänzungen"] = ["Antragsergänzungen"];
stagetabs["Zuwendungsbescheid"] = ["Zuwendungsbescheid"];


var stages = null;


function Form_OnLoad()
{

Xrm.Page.data.process.addOnStageSelected(OnStageSelected);
Xrm.Page.data.process.addOnStageChange(OnStageChanged);


if (Xrm.Page.data.process != null)
{
stages = Xrm.Page.data.process.getActiveProcess().getStages();

OnStageChanged();
}
}


function StageTabsLoop(stage, index)
{
for (var j = 0; j < stagetabs[stage.getName()].length; j++) { Xrm.Page.ui.tabs.get(stagetabs[stage.getName()][j]).setVisible(false); } }


function OnStageSelected(state) 
{
	var eventAgrs = state.getEventArgs(); 
	var selectedStage = eventAgrs.getStage() stages.forEach(StageTabsLoop); 
	var selectedStage = selectedStage.getName(); 
	for (var i = 0; i < stagetabs[selectedStage].length; i++) { Xrm.Page.ui.tabs.get(stagetabs[selectedStage][i]).setVisible(true); } 
}


function OnStageChanged() 
{ 
	stages.forEach(StageTabsLoop); 
	var activeStage = Xrm.Page.data.process.getActiveStage().getName(); 
	for (var i = 0; i < stagetabs[activeStage].length; i++) { Xrm.Page.ui.tabs.get(stagetabs[activeStage][i]).setVisible(true); } 
}


 Form_5F00_OnLoad.JPG

But I get this error:

ReferenceError: Form_OnLoad is not defined at eval...

Am I missing something? Code looks fine to me :-D...

TIA

DD

*This post is locked for comments

  • Verified answer
    Dick_Darlington Profile Picture
    Dick_Darlington 85 on at
    RE: Hide/show section based on process stage - help on JavaScript

    Hey there,

    figured it ou myself...

    As Goutam's suggestion didn't work I optimized my script as follows:

    var stages = null;
    
    function Form_OnLoad()
    {
    Xrm.Page.data.process.addOnStageSelected(OnStageSelected);
    Xrm.Page.data.process.addOnStageChange(OnStageChanged);
    
    if (Xrm.Page.data.process != null)
    {
    stages = Xrm.Page.data.process.getActiveProcess().getStages();
    
    OnStageChanged();
    }
    }
    
    
    function StageTabsLoop(stage, index)
    {
    for (var j = 0; j < stagetabs[stage.getName()].length; j++) { Xrm.Page.ui.tabs.get(stagetabs[stage.getName()][j]).setVisible(false); }
     }
    
    
    function OnStageSelected(state) 
    {
    	var eventAgrs = state.getEventArgs(); 
    	var selectedStage = eventAgrs.getStage();  stages.forEach(StageTabsLoop); 
    	var selectedStage = selectedStage.getName(); 
    	for (var i = 0; i < stagetabs[selectedStage].length; i++) { Xrm.Page.ui.tabs.get(stagetabs[selectedStage][i]).setVisible(true); } 
    }
    
    
    function OnStageChanged() 
    { 
    	stages.forEach(StageTabsLoop); 
    	var activeStage = Xrm.Page.data.process.getActiveStage().getName(); 
    	for (var i = 0; i < stagetabs[activeStage].length; i++) { Xrm.Page.ui.tabs.get(stagetabs[activeStage][i]).setVisible(true); } 
    }
    
    var stagetabs = new Array();
    stagetabs["Anlegen"] = ["Anlegen"];
    stagetabs["Entwickeln"] = ["Entwickeln"];
    stagetabs["Angebotsphase"] = ["Angebotsphase"];
    stagetabs["Vertragsabschluss"] = ["Vertragsabschluss"];


    Works like a charm now :-)

    Thanks anyway!

  • Dick_Darlington Profile Picture
    Dick_Darlington 85 on at
    RE: Hide/show section based on process stage - help on JavaScript

    Hey Goutam,

    thank you for your code :).

    Just implemented it and got this error:

    ReferenceError: stagetabs is not defined at OnStageSelected

    As this does not seem to be a lie, how do I have to define 'stagetabs'?

    :)

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Hide/show section based on process stage - help on JavaScript

    Hello , 

    Seems the code you written its complex one and I found some of the step not required , so make it simple like below .

    function Form_OnLoad() {
        Xrm.Page.data.process.addOnStageSelected(OnStageSelected);
        if (Xrm.Page.data.process != null) {
            OnStageSelected();
        }
    }
    
    
    
    function OnStageSelected() {
        var stageName = Xrm.Page.data.process.getSelectedStage().getName().toString().toLowerCase();
        var stageID = Xrm.Page.data.process.getSelectedStage().getId().toString().toLowerCase();
    
        Xrm.Page.ui.tabs.get(stagetabs[selectedStage][i]).setVisible(true);
    
        switch (stageName) {
            case 'stge1': // Change the stage name
                if (Xrm.Page.ui.tabs.get("Projektkonzeption") != null)
                    Xrm.Page.ui.tabs.get("Projektkonzeption").setVisible(true);
                // You can add more section hide   
                break;
    
            case 'stge2': // Change the stage name 
                if (Xrm.Page.ui.tabs.get("Antragsergänzungen") != null)
                    Xrm.Page.ui.tabs.get("Antragsergänzungen").setVisible(true);
    
                // You can add more section hide   
    
                break;
    
    
                // You can add more stages condition 
        }
    }
    


    Hope this helps.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans