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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Hidden sections (by javascript) appear after saving the record

(0) ShareShare
ReportReport
Posted on by 10

Hi all,

I'm having a hard time with the following problem:

On a custom entity i'm using javascript to hide some sections based on a lookup (one remains visible the other sections are hidden). The javascript works perfectly (on change & on load) but when I save the record all the sections are visible again. After reloading the page it works fine again. I also tried to run the javascript after save but this was not the solution. The fields were visible after the first save and only when I clicked a second time on the save button the javascript worked again.

This is the javascript I use:

function hideProductSections(executionContext){
    var formContext = executionContext.getFormContext();
    var productId = formContext.getAttribute("bc_parentproduct").getValue();
    var product;

    if (productId != null) {
        product = String(productId[0].id);

        switch(product) {
            case "{D614A830-F420-EC11-B6E6-6045BD8A193A}"://nen4400-1
                formContext.ui.tabs.get("general").sections.get("nen4400-1").setVisible(true);
                formContext.ui.tabs.get("general").sections.get("nen4400-2").setVisible(false);
                break;
            case "{63B28BC1-F420-EC11-B6E6-6045BD8A193A}"://nen4400-2
                formContext.ui.tabs.get("general").sections.get("nen4400-1").setVisible(false);
                formContext.ui.tabs.get("general").sections.get("nen4400-2").setVisible(true);
                break;
            default:
                formContext.ui.tabs.get("general").sections.get("nen4400-1").setVisible(false);
                formContext.ui.tabs.get("general").sections.get("nen4400-2").setVisible(false);
                break;
        }
    }
    else {
        formContext.ui.tabs.get("general").sections.get("nen4400-1").setVisible(false);
        formContext.ui.tabs.get("general").sections.get("nen4400-2").setVisible(false);
    }
}

This is a short version, the switch contains more cases.

Hopefully someone can help me with this issue.

Thanks in advance!

I have the same question (0)
  • Verified answer
    Community Member Profile Picture
    on at

    Hi Gerard91,

    I couldn't reproduce your issue(When clicking save button, the all sections would be visible) with the code you provided.

    But you could try the following on save JS, it would hide/show the sections after clicking save button(Click it one time is ok).

    var saveData = true;
    function hideProductSectionsOnSave(executionContext){
        var formContext = executionContext.getFormContext();
        if(!saveData){
    		saveData = true;
    		return;
        }
        executionContext.getEventArgs().preventDefault();
    
        setTimeout( () => {
            saveData = false;
            formContext.data.save().then(
    	    function (){
    			var productId = formContext.getAttribute("bc_parentproduct").getValue();
    			var product;
    
    			if (productId != null) {
    				product = String(productId[0].id);
    
    				switch(product) {
    					case "{D614A830-F420-EC11-B6E6-6045BD8A193A}"://nen4400-1
    						formContext.ui.tabs.get("general").sections.get("nen4400-1").setVisible(true);
    						formContext.ui.tabs.get("general").sections.get("nen4400-2").setVisible(false);
    						break;
    					case "{63B28BC1-F420-EC11-B6E6-6045BD8A193A}"://nen4400-2
    						formContext.ui.tabs.get("general").sections.get("nen4400-1").setVisible(false);
    						formContext.ui.tabs.get("general").sections.get("nen4400-2").setVisible(true);
    						break;
    					default:
    						formContext.ui.tabs.get("general").sections.get("nen4400-1").setVisible(false);
    						formContext.ui.tabs.get("general").sections.get("nen4400-2").setVisible(false);
    						break;
    				}
    			}
    			else {
    				formContext.ui.tabs.get("general").sections.get("nen4400-1").setVisible(false);
    				formContext.ui.tabs.get("general").sections.get("nen4400-2").setVisible(false);
    			}
    	    }, 
                function (error){console.log(error.message)}, 
            );	
        }, 500);
    }

  • Gerard91 Profile Picture
    10 on at

    Hi Steve,

    Thanks for you quick response!

    After your message that you couldn't reproduce my issue I investigated the issue even further. After a while i noticed that the issue was caused by a business rule. It turned out that on the sections some fields were shown or hidden and when the fields were shown or hidden the sections became visible again.

    I removed the fields from those sections and it worked fine!

    Thanks again for your help!

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Martin Dráb Profile Picture

Martin Dráb 49 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 38 Super User 2025 Season 2

#3
#ManoVerse Profile Picture

#ManoVerse 31

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans