Skip to main content
Solved

How can I get the first (eldest) record of an entity?

  • June 18, 2025
  • 7 replies
  • 101 views

Litox
Freshman I

Using the REST API I need to know which is the first record (chronologically) of JournalTransaction. That is, the one with the eldest CreatedDateTime.

  1. How can I get that?
  2. What's the default ordering of that collection?
  3. Can I do something like sort + top=1?

Best answer by CherryStreet

This is what I am getting:

This is a known nuance with Acumatica’s Contract-Based REST API (CBAPI).

🔍 The Core Issue:

You are using valid OData-style parameters ($orderby, $top), but those do not work reliably in CBAPI, especially for ordering by system-generated fields like CreatedDateTime or LastModifiedDateTime. Here’s why:

 

📌 CBAPI does not fully support OData query parameters like $orderby or $top, even if it appears to accept them silently. These parameters are not guaranteed to be parsed or respected unless the endpoint is explicitly built to do so.

 

This is documented behavior, and Acumatica support confirms that only OData endpoints support $orderby, $top, $filter reliably.

 

✅ What You Can Do Instead (CBAPI-compatible)

 

Since you’re restricted to CBAPI, here’s the correct approach:

 

✅ Option 1: 

Filter on CreatedDateTime Manually

 

Because CBAPI returns unordered data, you’ll need to:

  1. Retrieve a small page of records

  2. Manually find the oldest by CreatedDateTime

Example request:

POST /entity/Default/23.200.001/JournalTransaction
{
"pageSize": 100,
"fields": {
"ReferenceNbr": {},
"CreatedDateTime": {}
}
}

Then in your client code:

oldest = min(results, key=lambda x: x['CreatedDateTime'])

⚠️ If there are many pages, you will need to loop through them.

 

✅ Option 2: 

Create a Custom Endpoint That Supports Sorting

 

If you have admin access, this is the most maintainable method:

 

  1. Go to: Web Services Endpoints (SM.20.20.60)

  2. Clone the default Default/23.200.001

  3. Add a new custom endpoint method:

     

    • Based on JournalTransaction

    • With a view or GI that sorts by CreatedDateTime ASC

  4. Publish the endpoint

  5. Use this new endpoint in your REST call — no $orderby needed

 

 

✅ Option 3: 

Create a Generic Inquiry (GI) and Expose via OData

 

If you’re allowed to use OData GIs, you can:

 

  1. Build a GI on JournalTransaction

  2. Sort by CreatedDateTime ASC

  3. Publish to OData

  4. Use:

/OData/YourGI?$top=1

 

 

⚠️ You mentioned OData is not allowed, so this may be blocked in your environment.

 

 

🛠️ TL;DR Fix Summary:

Task

Status in CBAPI

Workaround

$orderby (CreatedDateTime)

❌ Not supported reliably

Use paging + client-side sort

$top=1

❌ Ignored unless combined with sort

Use paging manually

Custom endpoint with sort

✅ Works

Use custom endpoint + sorting

OData GI

✅ Works

Only if OData is permitted

 

 

✅ Recommendation

 

If you cannot modify endpoints, your only safe solution is:

 

  • Pull a page of data

  • Manually sort by CreatedDateTime

  • If necessary, page through all results and keep the min

 

7 replies

Litox
Freshman I
  • Author
  • Freshman I
  • June 18, 2025

Just to expand, I don't have access to the OData interface, only the basic REST API.


CherryStreet
Jr Varsity I
Forum|alt.badge.img
  • Jr Varsity I
  • June 19, 2025

Ran this through our AI Assistant, let me know if this helps:

Since you're using Acumatica’s Contract-Based REST API (not OData), here's exactly how you can get the first JournalTransaction record chronologically by CreatedDateTime using filtering and ordering.

 

Goal:

Get the oldest (earliest) JournalTransaction record by CreatedDateTime using the Contract-Based REST API.

 

Answer Summary:

Yes — you can use:

  • orderBy to sort by CreatedDateTime
  • top=1 to limit results
 

🔧 Example Request:

Assuming your endpoint is similar to:

/entity/Default/20.200.001/JournalTransaction

Use this GET request:

GET /entity/Default/20.200.001/JournalTransaction?$top=1&$orderby=CreatedDateTime asc

🔍 Explanation:

  • ?$top=1: Limits the result to 1 record
  • &$orderby=CreatedDateTime asc: Sorts from oldest to newest
  • This gives you the first (eldest) journal transaction by creation time
 

What's the default ordering?

The Contract-Based REST API has no guaranteed default order.
So without explicitly using $orderby, the results are unordered or based on internal index, and not reliable for chronological operations.

 

Bonus: Using Filters (Optional)

If you want the oldest transaction of a specific type, you can combine $filter:

GET /entity/Default/20.200.001/JournalTransaction?$top=1&$filter=Ledger eq 'ACTUAL'&$orderby=CreatedDateTime asc

 

🧪 Testing Tip

Enable API Logging or use Postman to:

  • View the full response structure
  • Ensure CreatedDateTime is included in your returned fields (or explicitly request it via a custom screen endpoint)
 

🚫 Caveat

  • Some Acumatica versions do not expose CreatedDateTime by default on all entities.
  • If you don’t see it in the returned JSON, you may need to extend the endpoint and expose the CreatedDateTime field manually via the Endpoint screen (SM.20.20.60).
 

Final Answer:

Use this Contract-Based REST API call:

GET /entity/Default/20.200.001/JournalTransaction?$top=1&$orderby=CreatedDateTime asc

It will return the oldest journal transaction by creation time.

 


Litox
Freshman I
  • Author
  • Freshman I
  • June 19, 2025

Awesome! 
I couldn't find any reference to the $orderby parameter in the documentation, that's why I didn't know it existed! It makes all the difference in my case.
Thank you very much.


CherryStreet
Jr Varsity I
Forum|alt.badge.img
  • Jr Varsity I
  • June 19, 2025

My pleasure!


Litox
Freshman I
  • Author
  • Freshman I
  • June 19, 2025

Well, I tried it and it doesn't work. The API call doesn't fail, but it doesn't matter what I put in the parameter, I always get the same result  😢

entity/Default/23.200.001/JournalTransaction?$orderby=LastModifiedDateTime desc&$top=1

entity/Default/23.200.001/JournalTransaction?$orderby=LastModifiedDateTime Desc&$top=1

entity/Default/23.200.001/JournalTransaction?$orderby=LastModifiedDateTime asc&$top=1

entity/Default/23.200.001/JournalTransaction?$orderby=LastModifiedDateTime Asc&$top=1


CherryStreet
Jr Varsity I
Forum|alt.badge.img
  • Jr Varsity I
  • June 19, 2025

Ok, let me do some troubleshooting for you.


CherryStreet
Jr Varsity I
Forum|alt.badge.img
  • Jr Varsity I
  • Answer
  • June 19, 2025

This is what I am getting:

This is a known nuance with Acumatica’s Contract-Based REST API (CBAPI).

🔍 The Core Issue:

You are using valid OData-style parameters ($orderby, $top), but those do not work reliably in CBAPI, especially for ordering by system-generated fields like CreatedDateTime or LastModifiedDateTime. Here’s why:

 

📌 CBAPI does not fully support OData query parameters like $orderby or $top, even if it appears to accept them silently. These parameters are not guaranteed to be parsed or respected unless the endpoint is explicitly built to do so.

 

This is documented behavior, and Acumatica support confirms that only OData endpoints support $orderby, $top, $filter reliably.

 

✅ What You Can Do Instead (CBAPI-compatible)

 

Since you’re restricted to CBAPI, here’s the correct approach:

 

✅ Option 1: 

Filter on CreatedDateTime Manually

 

Because CBAPI returns unordered data, you’ll need to:

  1. Retrieve a small page of records

  2. Manually find the oldest by CreatedDateTime

Example request:

POST /entity/Default/23.200.001/JournalTransaction
{
"pageSize": 100,
"fields": {
"ReferenceNbr": {},
"CreatedDateTime": {}
}
}

Then in your client code:

oldest = min(results, key=lambda x: x['CreatedDateTime'])

⚠️ If there are many pages, you will need to loop through them.

 

✅ Option 2: 

Create a Custom Endpoint That Supports Sorting

 

If you have admin access, this is the most maintainable method:

 

  1. Go to: Web Services Endpoints (SM.20.20.60)

  2. Clone the default Default/23.200.001

  3. Add a new custom endpoint method:

     

    • Based on JournalTransaction

    • With a view or GI that sorts by CreatedDateTime ASC

  4. Publish the endpoint

  5. Use this new endpoint in your REST call — no $orderby needed

 

 

✅ Option 3: 

Create a Generic Inquiry (GI) and Expose via OData

 

If you’re allowed to use OData GIs, you can:

 

  1. Build a GI on JournalTransaction

  2. Sort by CreatedDateTime ASC

  3. Publish to OData

  4. Use:

/OData/YourGI?$top=1

 

 

⚠️ You mentioned OData is not allowed, so this may be blocked in your environment.

 

 

🛠️ TL;DR Fix Summary:

Task

Status in CBAPI

Workaround

$orderby (CreatedDateTime)

❌ Not supported reliably

Use paging + client-side sort

$top=1

❌ Ignored unless combined with sort

Use paging manually

Custom endpoint with sort

✅ Works

Use custom endpoint + sorting

OData GI

✅ Works

Only if OData is permitted

 

 

✅ Recommendation

 

If you cannot modify endpoints, your only safe solution is:

 

  • Pull a page of data

  • Manually sort by CreatedDateTime

  • If necessary, page through all results and keep the min