Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Hiding fields in Quick View form with JS not working

(0) ShareShare
ReportReport
Posted on by 70

Hi,

I need to hide few fields in a Quick View Form based on an Option Set (Choice) selection in that Quick View form. However it is not working, so am sharing the code I used for the same here. In my code, I am trying to hide certain fields if the option selected is not equal to a particular value...

function hideFields(executionContext) {

    var formContext = executionContext.getFormContext();

    var quickViewControl = formContext.ui.quickForms.get("General");

    if (quickViewControl != undefined) {

        if (quickViewControl.isLoaded()) {

               var orgtypevalue = quickViewControl.getControl("new_organizationtype ").getValue();

               if (orgtypevalue != 248870006 ) {

               quickViewControl.getControl("new_recipienttype").setVisible(false);

               quickViewControl.getControl("new_businesstype").setVisible(false);

               quickViewControl.getControl("new_businesstypecode").setVisible(false);                                                               quickViewControl.getControl("new_businesstypecharacteristicstypecode").setVisible(false);        

               return;

                }

               else {

               // Wait for some time and check again

               setTimeout(getAttributeValue, 10, executionContext);

               }

    }

    else {

        console.log("No data to display in the quick view control.");

        return;

    }

    else {

            quickViewControl.getControl("new_recipienttype").setVisible(true);

            quickViewControl.getControl("new_businesstype").setVisible(true);

            quickViewControl.getControl("new_businesstypecode").setVisible(true); 

            quickViewControl.getControl("new_businesstypecharacteristicstypecode").setVisible(true);            

            return;  

 }

}

}

}

I need to know where am I wrong. Any help will be appreciated.

Thanks!

  • AqiqaJamshi Profile Picture
    AqiqaJamshi 5 on at
    RE: Hiding fields in Quick View form with JS not working

    Did you try to debug, seen any errors in browser developer toolbar console in url?

      

  • D365 User 06 Profile Picture
    D365 User 06 70 on at
    RE: Hiding fields in Quick View form with JS not working

    Yes, now I got it! Thank you !!

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Hiding fields in Quick View form with JS not working

    Hi D365 User 06,

    Control is HTML elements present on the form.  It is usually used to set fields visible/disable or add some event to this control. Reference:

    Controls in model-driven apps for Dynamics 365 - Power Apps | Microsoft Docs

    To get the field's value, we usually use the Attribute. 

    Columns in model-driven apps - Power Apps | Microsoft Docs

  • D365 User 06 Profile Picture
    D365 User 06 70 on at
    RE: Hiding fields in Quick View form with JS not working

    Hi Milansinh Raj ,

    Thank you for your help. I tried the code that Steve gave me and it worked for me.

    My code was wrong at couple of places - like I dint add .getAttribute() to get the value, and the else code for setting the Visibility as 'true' was added towards the end when it was supposed to be added right after the .setVisibility(false) lines. Also, in the timeout line, I got the name of the function wrong (instead of hideFields, I wrote the name of the function that I had previously named this function).

    So I rectified all these errors and now its working fine. However, you had also pointed out to me about the empty space in my attribute name, so I'm also marking your comment as Verified.

    Thanks!

  • D365 User 06 Profile Picture
    D365 User 06 70 on at
    RE: Hiding fields in Quick View form with JS not working

    Hi   ,

    Wow, that code worked! Thank you so much for your help and taking the extra effort to actually try out the code!!

    My code was wrong at couple of places - like I dint add .getAttribute() to get the value, and the else code for setting the Visibility as 'true' was added towards the end. Also, in the timeout line, I got the name of the function wrong (instead of hideFields, I wrote the name of the function that I had previously named this function). Thanks a lot for pointing them out to me! I'm marking your answer as Verified.

    Can I ask you one question - I had already added .getControl to get the value, then why did we have to added .getAttribute() too after it? As in the setVisibility lines, we are only adding .getControl and still it is working, so why do we have to add .getAttribute() to get the value?

    Thanks!

  • Milansinh Raj Profile Picture
    Milansinh Raj 215 on at
    RE: Hiding fields in Quick View form with JS not working

    Can you mark my answer as Verified answer :D

    Also post your new code here so can check why not working

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Hiding fields in Quick View form with JS not working

    Hi D365 User 06,

    Please test this code:

    function hideFields(executionContext) {
    	//If not work,please debug.
    	//debugger;
        var formContext = executionContext.getFormContext();
    
        var quickViewControl = formContext.ui.quickForms.get("General");
    
        if (quickViewControl != undefined) {
    
            if (quickViewControl.isLoaded()) {
    
    			var orgtypevalue = quickViewControl.getControl("new_organizationtype").getAttribute().getValue();
    
    			if (orgtypevalue != 248870006 ) {
    
    				quickViewControl.getControl("new_recipienttype").setVisible(false);
    
    				quickViewControl.getControl("new_businesstype").setVisible(false);
    
    				quickViewControl.getControl("new_businesstypecode").setVisible(false);
    				
    				quickViewControl.getControl("new_businesstypecharacteristicstypecode").setVisible(false);    
    				
    				return;
    			}else {
    
    				quickViewControl.getControl("new_recipienttype").setVisible(true);
    
    				quickViewControl.getControl("new_businesstype").setVisible(true);
    
    				quickViewControl.getControl("new_businesstypecode").setVisible(true); 
    
    				quickViewControl.getControl("new_businesstypecharacteristicstypecode").setVisible(true);            
    
    				return;  
    			}
    			
    		}
    		else {
    
    				// Wait for some time and check again
    
    				setTimeout(hideFields, 500, executionContext);
    
    			}
    		
    	}else {
    
    			console.log("No data to display in the quick view control.");
    
    			return;
    
    		}
    
    }
    

    It works for me(I only create "Recipient Type" field to test):

    Organization Type != 248870006

    pastedimage1662361516271v1.png

    Organization Type == 248870006

    pastedimage1662361620729v2.png

    If it could not work for you, please debug this code and check which line doesn't work.

  • D365 User 06 Profile Picture
    D365 User 06 70 on at
    RE: Hiding fields in Quick View form with JS not working

    Thank you for your response, Milansinh Raj! You are right, there is a space in the attribute name which I dint notice earlier, so I corrected it. Thanks for pointing it out to me! Also, there was an extra bracket towards the end of the code, which I removed. But still my code is not working. I hope all my brackets are correct.

    I want to hide 4 fields inside the Quick View Form if the value of the Option Set (which is also a field inside the Quick View form) is not equal to a particular value.

    Thank you!

  • Verified answer
    Milansinh Raj Profile Picture
    Milansinh Raj 215 on at
    RE: Hiding fields in Quick View form with JS not working

    Your code seems correct except one line as below where you have extra space added which can be not found as attribute name:

     var orgtypevalue = quickViewControl.getControl("new_organizationtype ").getValue();

    use this instead:

     var orgtypevalue = quickViewControl.getControl("new_organizationtype").getValue();

    Please mark as answer if this helps

    Thanks 

  • Milansinh Raj Profile Picture
    Milansinh Raj 215 on at
    RE: Hiding fields in Quick View form with JS not working

    Hey D365 User 06 , you want to hide Quick View Form or the field inside it ?

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey Pt 2

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,540 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans