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);
}
)
}
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);
}
}
}
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,280 Super User 2024 Season 2
Martin Dráb 230,214 Most Valuable Professional
nmaenpaa 101,156