Skip to main content

Notifications

Announcements

No record found.

Small and medium business | Business Central, N...
Suggested answer

How to return Json using unbound action api in business central

(2) ShareShare
ReportReport
Posted on by 29
I need to create API which inserts and updates data, both using POST Request where I need to check if the record already exists to modify it otherwise will insert new record.
I tried this using page API and it works well but the response is empty for unspecified fields 

for example :
This is the response of GET Request 
{
 
            /Primary_Key/: /218000_0104003/,
            /Group_Account/: /218000/,
            /Group_Account_Name/: /Other tangible assets/,
            /Analytical_Account_No_/: /0104003/,
            /Analytical_Account_Name/: /T&M ASSISTANCE/,
            /Group_Blocked/: true,
            /Blocked/: false,
            /Analytical_Blocked/: false
        }
 
 
when I try to modify the Group_blocked , I need to send Primary key and group_Blocked in the request body and expect the whole record as response 
 
{
    /Primary_Key/: /218000_0104003/,
    /Group_Blocked/: false
 
}
 
I get this response
 
{
 
    /Primary_Key/: /218000_0104003/,
    /Group_Account/: //,
    /Group_Account_Name/: //,
    /Analytical_Account_No_/: //,
    /Analytical_Account_Name/: //,
    /Group_Blocked/: false,
    /Blocked/: false,
    /Analytical_Blocked/: false
}

This is the code 
 
page 71172 /XEE_Analytical_Account/
{
    PageType = API;
    Caption = 'Analytical Accounts';
    APIPublisher = 'XEETEK';
    APIGroup = 'Payroll';
    APIVersion = 'v2.0';
    EntityName = 'AnalyticalAccount';
    EntitySetName = 'AnalyticalAccounts';
    SourceTable = /XEE Group-Analytical Account/;
    DelayedInsert = true;
    ODataKeyFields = /Primary Key/;
    // DataAccessIntent = ReadWrite;
    // Editable = false;
    ModifyAllowed = true;
    InsertAllowed = true;
    layout
    {
        area(Content)
        {
            repeater(GroupName)
            {
                field(/Primary_Key/; rec./Primary Key/)
                {
                    Caption = 'ID';
                    ApplicationArea = all;
                    trigger OnValidate()
                    var
                    begin
                        RegisterFieldSet(rec.FieldNo(/Primary Key/));
                    end;
                }
                field(/Group_Account/; rec./Group Account/)
                {
                    ApplicationArea = all;
                    trigger OnValidate()
                    var
                    begin
                        RegisterFieldSet(rec.FieldNo(/Group Account/));
                    end;
                }
                field(/Group_Account_Name/; rec./Group Account Name/)
                {
                    ApplicationArea = all;
                    trigger OnValidate()
                    var
                    begin
                        RegisterFieldSet(rec.FieldNo(/Group Account Name/));
                    end;
                }
                field(/Analytical_Account_No_/; rec./Analytical Account No./)
                {
                    ApplicationArea = all;
                    trigger OnValidate()
                    var
                    begin
                        RegisterFieldSet(rec.FieldNo(/Analytical Account No./));
                    end;
                }
                field(/Analytical_Account_Name/; rec./Analytical Account Name/)
                {
                    ApplicationArea = all;
                    trigger OnValidate()
                    var
                    begin
                        RegisterFieldSet(rec.FieldNo(/Analytical Account Name/));
                    end;
                }
                field(/Group_Blocked/; rec./Group Blocked/)
                {
                    ApplicationArea = all;
                    trigger OnValidate()
                    var
                    begin
                        RegisterFieldSet(rec.FieldNo(/Group Blocked/));
                    end;
                }
                field(Blocked; rec.Blocked)
                {
                    ApplicationArea = all;
                    trigger OnValidate()
                    var
                    begin
                        RegisterFieldSet(rec.FieldNo(Blocked));
                    end;
                }
                field(/Analytical_Blocked/; rec./Analytical Blocked/)
                {
                    ApplicationArea = all;
                    trigger OnValidate()
                    var
                    begin
                        RegisterFieldSet(rec.FieldNo(/Analytical Blocked/));
                    end;
                }
            }
        }
    }
    trigger OnInsertRecord(Xrec: Boolean): Boolean
    var
        GropAna: Record /XEE Group-Analytical Account/;
    begin
        GropAna.SetRange(/Primary Key/, rec./Primary Key/);
        if GropAna.FindFirst() then begin
            if TempFieldSet.Get(Database::/XEE Group-Analytical Account/, Rec.FieldNo(Blocked)) then
                GropAna.Blocked := rec.Blocked;
            if TempFieldSet.Get(Database::/XEE Group-Analytical Account/, Rec.FieldNo(/Group Blocked/)) then
                GropAna./Group Blocked/ := rec./Group Blocked/;
            if TempFieldSet.Get(Database::/XEE Group-Analytical Account/, Rec.FieldNo(/Analytical Blocked/)) then
                GropAna./Analytical Blocked/ := rec./Analytical Blocked/;
            GropAna./Analytical Account No./ := GropAna./Analytical Account No./;
            GropAna./Analytical Account Name/ := GropAna./Analytical Account Name/;
            GropAna./Group Account/ := GropAna./Group Account/;
            GropAna./Group Account Name/ := GropAna./Group Account Name/;
            GropAna.Modify();
        end
        else begin
            if (rec./Analytical Account No./ = '') and (rec./Group Account/ = '') then
                Error('There should be at least one account');
            rec.Insert(true);
        end;
        exit(false);
    end;
    var
        TempFieldSet: Record 2000000041 temporary;

    local procedure RegisterFieldSet(FieldNo: Integer)
    begin
        if TempFieldSet.Get(Database::/XEE Group-Analytical Account/, FieldNo) then
            exit;
        TempFieldSet.Init();
        TempFieldSet.TableNo := Database::/XEE Group-Analytical Account/;
        TempFieldSet.Validate(/No./, FieldNo);
        TempFieldSet.Insert(true);
    end;
}
 
Is There any error in this code or any other way to send the record in response
Note: I dont want to send patch request, I want update and insert using POST request 
  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 75,723 Super User 2024 Season 2 on at
    How to return Json using unbound action api in business central

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

Congratulations 2024 Spotlight Honorees!

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December!

Congratulations to our December super stars! 🥳

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,684 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,414 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans