Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Hide subgrid '+' Add new and Add existing button by security roles using javascript

Posted on by 75

Hello experts,

I am doing hide '+ Add new' and 'Add existing' button from subgrid by security role of a user. I am getting the current user of security roles and added a condition 'if the security role is not equal to the current user security role then the subgrid button will hide. Below is my code for hiding button,

function hideAddButtonSubgrid(subgridId) {
	 try {
		 
        addEventToGridRefresh(subgridId, setAddButtonDisplayNone);
    }
    catch (e) {
 
    }
}
 
function setAddButtonDisplayNone(subgridId) {
	debugger;
     var imageId = subgridId + "_" + "addImageButton";
     if (imageId) {
        window.parent.document.getElementById(imageId).style.display = 'none';
    }
    //var imageId2 = subgridId + "_" + "addImageButtonImage";
    //document.getElementById(imageId2).style.display = 'none';
}
 
function addEventToGridRefresh(subgridId, functionToCall) {
	debugger;
	
    // retrieve the subgrid
      var grid =  window.parent.document.getElementById(subgridId);
    // // if the subgrid still not available we try again after 1 second
   
    if(grid == null){
        setTimeout(function () {addEventToGridRefresh(subgridId, functionToCall); }, 1000);
    }
 
    // add the function to the onRefresh event
     grid.control.add_onRefresh(functionToCall);
 
     var imageId = subgridId + "_" + "addImageButton";
     if (imageId) {
        if (document.getElementById(imageId).style.display.toLowerCase() == "block") {
            setAddButtonDisplayNone(subgridId);
       }
    }
    
}


When I debug this code I am getting null value at 'grid'. How can resolve this problem? Is that any issue in new UCI of dynamics 365? 

*This post is locked for comments

  • Suggested answer
    Charles Abi Khirs Profile Picture
    Charles Abi Khirs 3,569 on at
    RE: Hide subgrid '+' Add new and Add existing button by security roles using javascript

    Hello,

    As Aric suggested, I recommend to go with the Ribbon Workbench tool and apply a display rule in order to show/hide the sub-grid button based on the user permission.

  • Kiran_girase Profile Picture
    Kiran_girase 75 on at
    RE: Hide subgrid '+' Add new and Add existing button by security roles using javascript

    Thanks for the suggestion sreevalli,

    I tried your code but I am getting error $ is not defined.

    For solving that issue I am also added below code,

    if (typeof($) === 'undefined') {

            var script = document.createElement('script');
    		script.src = 'ajax.googleapis.com/.../jquery.min.js';
    		script.async = false; 
    		document.head.appendChild(script);
    		}


    still I am getting same error
  • Suggested answer
    Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Hide subgrid '+' Add new and Add existing button by security roles using javascript

    As Ravi said, that is unsupported customization. Try using Ribbon Workbench to add a function to the AddNew and AddExisting buttons display rules that will call a JS function. The JS function will return true or false to show the buttons.

    Hope this helps.

  • Suggested answer
    Sreevalli Profile Picture
    Sreevalli 3,256 on at
    RE: Hide subgrid '+' Add new and Add existing button by security roles using javascript

    Hi,

    This worked for me, give a try.

    var addButton = window.parent.document.getElementById("quotedetailsGrid_addImageButtonImage");

       if (flag)

           $($(addButton).parent().parent()[0]).hide();

       else

           $($(addButton).parent().parent()[0]).show();

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Hide subgrid '+' Add new and Add existing button by security roles using javascript

    Hi,

    This will be because you are using unsupported method to get the subgrid. The only available methods for subgrid are mentioned in below doc. Please refer and change your code accordingly-

    docs.microsoft.com/.../grids

    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,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans