How to create role and permission programmatically using the B2B module Adobe Commerce?

Here is a piece of code to create a role by company ID.

public function __construct(
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
\Magento\Company\Api\RoleRepositoryInterface $roleRepository,
\Magento\Company\Api\Data\RoleInterfaceFactory $roleFactory,
\Magento\Company\Model\CompanyUser $companyUser,
\Magento\Company\Model\PermissionManagementInterface $permissionManagement,
Logger $logger
)
{
$this->searchCriteriaBuilder          = $searchCriteriaBuilder;
$this->roleRepository                 = $roleRepository;
$this->roleFactory                    = $roleFactory;
$this->permissionManagement           = $permissionManagement;
$this->logger                         = $logger;
}
    /**
     * @param $companyId
     * @return void
     */
    private function createDefaultRolesByCompanyId($companyId): void
    {
        $allRoles = array(
            'Accountant' => array(
                "Magento_Company::index",
                "Magento_Sales::all",
                "Magento_Sales::place_order",
                "Magento_Sales::payment_account",
                "Magento_Sales::view_orders",
                "Magento_Sales::view_orders_sub",
                "Magento_Sales::view_orders_sub_anchor",
                "Magento_NegotiableQuote::all",
                "Magento_NegotiableQuote::manage",
                "Magento_NegotiableQuote::checkout",
                "Magento_NegotiableQuote::view_quotes",
                "Magento_NegotiableQuote::view_quotes_sub",
                "Magento_Company::view",
                "Magento_Company::view_account",
                "Magento_Company::view_address",
                "Magento_Company::contacts",
                "Magento_Company::payment_information",
                "Magento_Company::shipping_information",
                "Magento_Company::user_management",
                "Magento_Company::credit",
            ),
            'Buyer' => array(
                "Magento_Company::index",
                "Magento_Sales::all",
                "Magento_Sales::place_order",
                "Magento_Sales::payment_account",
                "Magento_Sales::view_orders",
                "Magento_Sales::view_orders_sub",
                "Magento_Sales::view_orders_sub_anchor"
            )
        );
        foreach ($allRoles as $roleName => $rolePermissions) {
            try {
                $role = $this->roleFactory->create();
                $role->setRoleName($roleName);
                $role->setCompanyId($companyId);
                $role->setPermissions($this->permissionManagement->populatePermissions($rolePermissions));
                $this->roleRepository->save($role);
            } catch (\Magento\Framework\Exception\LocalizedException $e) {
                $this->logger->critical($e->getMessage());
            }
        }
    }

You can change the permissions array according to your needs.

The following table lists all the resources that are available to the customers defined with a company. To visualize the resource hierarchy, log in to a store as the Company Admin and select Roles and Permissions, then click the Edit action next to the Default User role.

DISPLAY NAMERESOURCE NAME
  AllMagento_Company::index
    SalesMagento_Sales::all
      Allow CheckoutMagento_Sales::place_order
        Use Pay On Account methodMagento_Sales::payment_account
      View ordersMagento_Sales::view_orders
      View orders of subordinate usersMagento_Sales::view_orders_sub
    QuotesMagento_NegotiableQuote::all
      ViewMagento_NegotiableQuote::view_quotes
        Request, Edit, DeleteMagento_NegotiableQuote::manage
        Checkout with QuoteMagento_NegotiableQuote::checkout
      View quotes of subordinate usersMagento_NegotiableQuote::view_quotes_sub
  Order ApprovalsMagento_PurchaseOrder::all
    View My Purchase OrdersMagento_PurchaseOrder:view_purchase_orders
      View for subordinatesMagento_PurchaseOrder:view_purchase_orders_for_subordinates
      View for all companyMagento_PurchaseOrder:view_purchase_orders_for_company
    Auto-approve POs created within this roleMagento_PurchaseOrder:autoapprove_purchase_order
    Approve Purchase Orders without other approvalsMagento_PurchaseOrder:super_approve_purchase_order
    View Approval RulesMagento_PurchaseOrder:view_approval_rules
      Create, Edit and DeleteMagento_PurchaseOrder:manage_approval_rules
    Company ProfileMagento_Company::view
      Account Information (View)Magento_Company::view_account
        EditMagento_Company::edit_account
      Legal Address (View)Magento_Company::view_address
        EditMagento_Company::edit_address
      Contacts (View)Magento_Company::contacts
      Payment Information (View)Magento_Company::payment_information
      Shipping Information (View)Magento_Company::shipping_information
    Company User ManagementMagento_Company::user_management
      View roles and permissionsMagento_Company::roles_view
        Manage roles and permissionsMagento_Company::roles_edit
      View users and teamsMagento_Company::users_view
        Manage users and teamsMagento_Company::users_edit
    Company creditMagento_Company::credit
      ViewMagento_Company::credit_history
I have used the Resource Name from the list above.

Reference from Adobe Developer