How to check if the current user is in a User Group
Scenario: I have assigned a Purchase Order to a specific User Group in Dynamics 365. I want to implement customized business logic that enables the Submit button only if the current user belongs to that group, otherwise disabling the button for unauthorized users.
High level resolution steps
Create an extension of PurchTable formdatasource and implement the logic in active method.
Detailed resolution steps
Step 1: Create a custom class and make an extension of Purch table data source.

Code Snippet:
///
/// Extension of purchtable formdatasource.
///
[ExtensionOf(FormDataSourcestr(PurchTable, PurchTable))]
internal final class SCC_PurchTable_Extension
{
///
/// Enables or disables the "Submit" button on the form based on whether the current user is part of the specified user group in the Purchase Table.
///
///
int active()
{
int ret = next active();
Formdatasource fds = this;
PurchTable purchTable = fds.cursor();
fds.formRun().design().controlName(formControlStr(purchTable, Submit)).enabled(UserInfoHelp::userInUserGroup(curUserId(),purchTable.UserGroupId));
return ret;
}
}
This code is used to check if the current user is a member of a specific user group assigned to a Purchase Table record.
UserInfoHelp::userInUserGroup(curUserId(),purchTable.UserGroupId)); |
1. UserInfoHelp is a static class that provides utility methods related to user information, such as user roles, user groups.
2. userInUserGroup is a static method within this class that checks whether a user is part of a specific user group. It returns a Boolean value (true or false) based on whether the user belongs to the specified group.
it accepts 2 parameters first parameter is the user and second parameter is user group.
3. curUserId() retrieves the current user’s ID.
4. purchTable.UserGroupId gets the ID of the user group assigned to the purchase order record.
Output:
When user in the user group access the form, he will be able to submit the Purchase Order

when the user isn’t available in the group the submit gets disabled.

User Group references for the user.

