Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Unanswered

Code to hide Account's Primary Contact Field

Posted on by 166
Hello, 
 
I am trying to hide the Primary Contact Field in the Account Form if the Business Unit of the User viewing this Account form is different from this Account Record's Owning Business Unit.
If the User's Business Unit is the same as the Account record's Owning Business Unit, or if the User's Business Unit is a parent BU of the Account Record's owning BU, the Primary Contact Field should be visible.
 
Ex: If Account Record named 'X' belongs to a business unit called 'BU 1', any user in Business Unit 'BU 1' should be able to see this account's primary contact. And any user in a parent Business Unit of 'BU 1' should also be able to see the primary contact.
 
Can someone help regarding accomplishing this using Javascript please? 
  • Joseph Nasr Profile Picture
    Joseph Nasr 166 on at
    Code to hide Account's Primary Contact Field
    Hello Rudy, 
     
    Thank you for your reply and assistance. I modified the code replacing a few things like the field logical names and I got the user's business unit by first getting the UserID:
    function hidePrimaryContactBasedOnBusinessUnit() {
        var accountOwningBusinessUnit = Xrm.Page.getAttribute("owningbusinessunit").getValue(); // Assuming the field storing the Account's Owning Business Unit is "owningbusinessunit"
    
         // get the userId
         var userId = Xrm.Utility.getGlobalContext().userSettings.userId;
         // remove { and } from the userId
         userId = userId.replace("{", "").replace("}", "");
    
        Xrm.WebApi.retrieveRecord("systemuser", userId, "?$select=_businessunitid_value").then(
            function success(result) {
                // User's Business Unit
                var userbusinessunitid = result["_businessunitid_value"]; // Lookup
                console.log(userbusinessunitid);
                if (userbusinessunitid && accountOwningBusinessUnit) {
                    // Check if User's Business Unit is different from Account's Owning Business Unit
                    var isUserInDifferentBusinessUnit = (userbusinessunitid[0].id !== accountOwningBusinessUnit[0].id);
            
                    // Check if User's Business Unit is not a parent of Account's Owning Business Unit
                    var isUserNotParentOfAccountBU = true;
                    for (var i = 0; i < accountOwningBusinessUnit.length; i++) {
                        if (userbusinessunitid[0].id === accountOwningBusinessUnit[i].id) {
                            isUserNotParentOfAccountBU = false;
                            break;
                        }
                    }
            
                    // Get the Primary Contact field
                    var primaryContactField = Xrm.Page.getControl("primarycontactid");
            
                    // Show/hide the Primary Contact field based on the conditions
                    if (isUserInDifferentBusinessUnit && isUserNotParentOfAccountBU) {
                        primaryContactField.setVisible(false);
                    } else {
                        primaryContactField.setVisible(true);
                    }
                }
            },
            function (error) {
                console.log(error.message);
            }
        )
    }
    Nevertheless it is still not working, any ideas why could this be happening?
  • RudyZhang Profile Picture
    RudyZhang Microsoft Employee on at
    Code to hide Account's Primary Contact Field
    Hi,
     
     
    Based on your needs, our team members have come up with a possible solution below that includes the following steps
    Step1, get the user's BUs and accountOwningBUs
    Step2, first determine whether the BU exists and whether the two BUs are identical. If they are not the same, iterate through the accountOwningBU to check if it contains the userBU.
    Step3, get the main contact field of the account and control the display of the field based on the previous result.
     
    Below is the code written by our team according to the above steps, but please note that the code is for testing only, some of the details may be different from your business.
    function hidePrimaryContactBasedOnBusinessUnit() {
        var userBusinessUnit = Xrm.Page.getAttribute("new_userbusinessunit").getValue(); // Assuming the field storing the User's Business Unit is "new_userbusinessunit"
        var accountOwningBusinessUnit = Xrm.Page.getAttribute("owningbusinessunit").getValue(); // Assuming the field storing the Account's Owning Business Unit is "owningbusinessunit"
        
        if (userBusinessUnit && accountOwningBusinessUnit) {
            // Check if User's Business Unit is different from Account's Owning Business Unit
            var isUserInDifferentBusinessUnit = (userBusinessUnit[0].id !== accountOwningBusinessUnit[0].id);
    
            // Check if User's Business Unit is not a parent of Account's Owning Business Unit
            var isUserNotParentOfAccountBU = true;
            for (var i = 0; i < accountOwningBusinessUnit.length; i++) {
                if (userBusinessUnit[0].id === accountOwningBusinessUnit[i].id) {
                    isUserNotParentOfAccountBU = false;
                    break;
                }
            }
    
            // Get the Primary Contact field
            var primaryContactField = Xrm.Page.getControl("primarycontactid");
    
            // Show/hide the Primary Contact field based on the conditions
            if (isUserInDifferentBusinessUnit && isUserNotParentOfAccountBU) {
                primaryContactField.setVisible(false);
            } else {
                primaryContactField.setVisible(true);
            }
        }
    }
     
     
    I hope my answer is helpful to you! If you have any other questions, please feel free to contact me.
     
    Best Regards,
    Rudy Zhang
     

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

Product updates

Dynamics 365 release plans