web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

Change the User Id for REST APIs that use OAUTH 2.0

(0) ShareShare
ReportReport
Posted on by 25
I am working on an extension for BC, my task was to enable REST API services for that extension. I had created the Azure Application and connected it to BC via the Microsoft Entra Applications Page.

The issue that I am running into is that, some validation is required before data is entered into a table on BC. One of the validation checks is against the currently logged in user.

The validation check gets the user name of the user account that is logged on to the current session. Here's a snippet of the code:

Screen shot:
 

Code snippet:
trigger OnInsert()
    begin
        /* Reset Filters */
        HREmp.Reset;
        /* Set Filter for "Employee UserID" where it is equal to the user name of the user account that is logged on to the current session. */
        HREmp.SetRange(HREmp."Employee UserID", UserId);
        if HREmp.Find('-')then begin
           /* Other Code */
        end
        else /* If the seatch fails, throw the error below */
        begin
            Error('UserID' + ' ' + '[' + UserId + ']' + ' has not been assigned to any employee. Please consult the HR officer for assistance')end;
    end;

When I try to insert a record into the above table via the API, the UserId will be set to a value that isn't in the Users table in BC, which will fail the if check, which will fail the insertion of data. Here's the error snippet from the API:
{
    "error": {
        "code": "Authentication_InvalidCredentials",
        "message": "The server has rejected the client credentials.  CorrelationId:  67626907-e975-47e5-a1fe-fb3d892e4bd5."
    }
}

So my question is, can I change the UserId that is passed in from the API to another one?

Any assistance on this is appreciated.
I have the same question (0)
  • Suggested answer
    Yi Yong Profile Picture
    2,736 Super User 2026 Season 1 on at
    Hello,
     
    This is a repeated post.
    You can view my response from the first post.
     
     
  • Netjacker2097 Profile Picture
    286 on at
    Hi,
     
    If you have control over the API client (e.g., the application making the API calls to BC), you could modify the API to include a custom header or parameter that specifies the UserId to use. Then, you can retrieve this custom UserId in your AL code.
     
    procedure OnBeforeInsertRecord()
    var
        APIUserId: Text;
        CustomHeaders: Text;
    begin
        // Retrieve the custom userId passed in the API request
        CustomHeaders := Request.Headers.Get('UserId');
        if CustomHeaders <> '' then
            APIUserId := CustomHeaders
        else
            APIUserId := 'DEFAULT_API_USER_ID'; // Fallback if not provided
        HREmp.Reset;
        HREmp.SetRange(HREmp."Employee UserID", APIUserId);
        if HREmp.Find('-') then begin
           /* Other Code */
        end else begin
            Error('UserID' + ' ' + '[' + APIUserId + ']' + ' has not been assigned to any employee. Please consult the HR officer for assistance');
        end;
    end;

     
    Thanks,
    Michael.

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,497 Super User 2026 Season 1

#2
YUN ZHU Profile Picture

YUN ZHU 1,778 Super User 2026 Season 1

#3
AndrewThomas81 Profile Picture

AndrewThomas81 1,507

Last 30 days Overall leaderboard

Featured topics

Microsoft Training Manuals

Product updates

Dynamics 365 release plans