DYNAMICS 365 HOW TO RETRIEVE BUSINESS UNIT DEFAULT TEAM IN C# AND JAVASCRIPT

In Dynamics 365, each Business Unit can have one or more Teams under it, but has one Default Team that will be automatically created by the system each time a new Business Unit is created, and will have the same name as the Business Unit name.

The membership of this team will be systematically managed, where users are automatically added to the team whenever users are added to the business unit and removed from the team whenever users are removed from the business unit.

In addition, the default team can be used to simplify the management of the security roles when users have common permissions, where you can associate a security role to the Default Team with a set of permissions that will be given to all users in the business unit.

On the other hand, Default Teams cannot be deleted and will have the field Team Type set to Owner; therefore, they can own records if needed by setting it as the owner field of CRM records.

Sometimes, and based on the requirements, you have a scenario where you want to automatically set the owner to the default team of a business unit. Therefore, you have to write some code in order to dynamically fetch the appropriate Team and set it as owner.

In this post, we will see how to retrieve Business Unit Default Team using C# or JavaScript.

  1. Retrieve Default Team of Business Unit in C#

    public Guid RetrieveBusinessUnitDefaultTeam(string businessUnitName)
    {
    Guid guidTeam = Guid.Empty;
    QueryExpression query = new QueryExpression(Team.EntityLogicalName)
    {
    ColumnSet = new ColumnSet(Team.Fields.Id, Team.Fields.Name),
    Criteria = new FilterExpression
    {
    Conditions =
    {
    new ConditionExpression(Team.Fields.IsDefault, ConditionOperator.Equal, true)
    }
    },
    LinkEntities =
    {
    new LinkEntity(Team.EntityLogicalName, BusinessUnit.EntityLogicalName, Team.Fields.BusinessUnitId, BusinessUnit.Fields.BusinessUnitId, JoinOperator.Inner)
    {
    Columns = new ColumnSet(BusinessUnit.Fields.Id, BusinessUnit.Fields.Name),
    EntityAlias = "bu",
    LinkCriteria = new FilterExpression
    {
    Conditions =
    {
    new ConditionExpression(BusinessUnit.Fields.Name, ConditionOperator.Equal, businessUnitName)
    }
    }
    }
    }
    };
    var bu = AdminService.RetrieveMultiple<Team>(query).FirstOrDefault();
    if (bu != null)
    guidTeam = bu.GetAttributeValue<Guid>(Team.Fields.Id);
    return guidTeam;
    }
  2. Retrieve Default Team of Business Unit in JavaScript

    function retrieveBusinessUnitDefaultTeam(context, buName) {
    var formContext = context.getFormContext();
    var defaultTeamGuid = "";
    var fetchXml = '<fetch>';
    fetchXml += '<entity name="team">';
    fetchXml += '<attribute name="teamid" />';
    fetchXml += '<attribute name="name" />';
    fetchXml += '<filter>';
    fetchXml += '<condition attribute="isdefault" operator="eq" value="1" />';
    fetchXml += '</filter>';
    fetchXml += '<link-entity name="businessunit" from="businessunitid" to="businessunitid" link-type="inner" alias="bu">';
    fetchXml += '<filter>';
    fetchXml += '<condition attribute="name" operator="eq" value="' + buName + '" />';
    fetchXml += '</filter>';
    fetchXml += '</link-entity>';
    fetchXml += '</entity>';
    fetchXml += '</fetch>';

    var encodedFetchXML = encodeURIComponent(fetchXml);
    var fetchXmlRequest = "?fetchXml=" + encodedFetchXML;

    Xrm.WebApi.retrieveMultipleRecords("team", fetchXmlRequest).then(
    function success(result) {
    defaultTeamGuid = result.entities[0].teamid;
    },
    function (error) {
    var alertMessage = { text: error.message };
    Xrm.Navigation.openAlertDialog(alertMessage, null);
    }
    );
    }


Bonus Tip:
  • When you want to set the Default Team as owner, don't forget to assign a security role with appropriate permissions, otherwise, you will get a permission exception


Hope This Helps!

Comments

Post a Comment

Popular posts from this blog

DYNAMICS 365 FILTER LOOKUP FIELD BASED ON N:N RELATIONSHIP

DYNAMICS 365 LEVEL UP BROWSER EXTENSION - PART 1 - FORMS

DYNAMICS 365 REFERENCE PANEL