web
You’re offline. This is a read only version of the page.
close
Skip to main content
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,545 Super User 2025 Season 2 on at
    Change the User Id for REST APIs that use OAUTH 2.0
    Hello,
     
    This is a repeated post.
    You can view my response from the first post.
     
     
  • Netjacker2097 Profile Picture
    286 on at
    Change the User Id for REST APIs that use OAUTH 2.0
    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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 3,377

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 2,696 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,512 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans
Liquid error: parsing "/forums/thread/?+Cloud+Innovators=&dfname=Code+&discussionforumid=fa08e8fa-192f-f011-8c4e-7c1e5248e2ba" - Nested quantifier +.