Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Supply chain | Supply Chain Management, Commerce
Answered

API JSON response is not returning all the lines but just the last inserted row

(0) ShareShare
ReportReport
Posted on by 8
Hi All,
 
I have a requirement for which an API is developed
A third party will pull some data via the API.
 
Now the issue is that the API response is just return one line , I mean the last inserted row.
 
I have debugged my code and could see that the list is having all the elements in it,
Any suggestion for the below issue
 
//
 
Please see my code below
 
JSON Response code:
 
[DataContractAttribute]
class OEI1INStagingStockTransferHirarchyJSONResponse
{
    OEI1INStagingStockTransferHeaderJSONResponse    header;
    OEI1INStagingStockTransferItemJSONResponse      item;
    OEI1INStagingStockTransferCustomerJSONResponse  customer;
    List                                            headerList,itemsList,customerList;
    // TO Header
    [DataMemberAttribute('StockTransferHeader'),
    DataCollectionAttribute(Types::Class,classStr(OEI1INStagingStockTransferHeaderJSONResponse))]
    public List parmTOHeader(List _tranferOrderHeaders = headerList)
    {
        headerList = _tranferOrderHeaders;
        return headerList;
    }
    // TO Lines
    [DataMemberAttribute('StockTransferItem'),
    DataCollectionAttribute(Types::Class,classStr(OEI1INStagingStockTransferItemJSONResponse))]
    public List parmTOLines(List _tranferOrderLines = itemsList)
    {
        itemsList = _tranferOrderLines;
        return itemsList;
    }
    // TO Customers
    [DataMemberAttribute('StockTransferCustomer'),
    DataCollectionAttribute(Types::Class,classStr(OEI1INStagingStockTransferCustomerJSONResponse))]
    public List parmTOCustomers(List _customerList = customerList)
    {
        customerList = _customerList;
        return customerList;
    }
}
 
 
main contract class:
 
[DataContractAttribute]
class OEI1INStagingStockTransferHirarchyRootJSONResponse
{
    OEI1INStagingStockTransferHirarchyJSONResponse rootJSONResponse;
    List                                           rootResponselist;
    // TO Header
    [DataMemberAttribute('_requestStockTransfer'),
    AifCollectionTypeAttribute('_requestStockTransfer',Types::Class,classStr(OEI1INStagingStockTransferHirarchyJSONResponse)),
    AifcollectionTypeAttribute('return',Types::Class,classStr(OEI1INStagingStockTransferHirarchyJSONResponse))]
    public List parmRootResponse(List _rootResponseList = rootResponselist)
    {
        rootResponselist = _rootResponseList;
        return rootResponselist;
    }
}
 
 
Service CLass:
 
class OEI1INStagingStockTransferService
{
    public OEI1INStagingStockTransferRootSerializer processStockTransfer(OEI1INStagingStockTransferJSONRequest _stockTransferRequest)
    {
        Query query = new Query(queryStr(OEI1INStagingStockTransferQuery));
        QueryRun                qrun;
        QueryBuildDataSource    qbdsCustCode;
        QueryBuildRange         qbr;
        int                     iCnt = 0;
        Notes JSONstring = //;
        OEI1INStagingStockTransferHirarchyJSONResponse mainJsonResponse = new OEI1INStagingStockTransferHirarchyJSONResponse();
        OEI1INStagingStockTransferHirarchyJSONResponse mainJsonResponseList = new OEI1INStagingStockTransferHirarchyJSONResponse();
        OEI1INStagingStockTransferRootSerializer    rootJSonResponse = new OEI1INStagingStockTransferRootSerializer();
        OEI1INStgStockTransferJSRes jsonResSTR,jsonResSTRRec;
        OEI1INStagingStockTransferHeader stagingStockTFHeader,stockTransferHeaderUpdate;
        OEI1INStagingStockTransferCustomer stagingStockTFCustomer,stockTransferCustomerUpdate;
        OEI1INStagingStockTransferItem stagingStockTFItem,stockTransferItemUpdate;
        OEI1INStagingStockTransferContract requestStockTransfer;
        ListIterator listIterator;
        List list = new List(Types::Class);
        List headerlist = new List(Types::Class);
        List custlist = new List(Types::Class);
        List itemlist = new List(Types::Class);
        ListEnumerator  lEnum,lEnumCust,lEnumItem;
        OEI1INStagingStockTransferJSONResponse jSonResponse = new OEI1INStagingStockTransferJSONResponse();
        // Initialize the interface iterator
        listIterator = new ListIterator(_stockTransferRequest.OEI1INStagingStockTransferContract());
        
            while (listIterator.more())
            {
                requestStockTransfer = listIterator.value();
                try
                {
                    if (requestStockTransfer.parmcustomerCode() != //)
                    {
                        qbdsCustCode = query.dataSourceTable(tableNum(OEI1INStagingStockTransferCustomer));
                        qbr = qbdsCustCode.addRange(fieldNum(OEI1INStagingStockTransferCustomer,CustomerCode));
                        qbr.value(SysQuery::value(requestStockTransfer.parmcustomerCode()));
                        qrun = new QueryRun(query);
                        while (qrun.next())
                        {
                            //stagingStockTFHeader = qrun.get(tableNum(OEI1INStagingStockTransferHeader));
                            stagingStockTFCustomer = qrun.get(tableNum(OEI1INStagingStockTransferCustomer));
                            //stagingStockTFItem = qrun.get(tableNum(OEI1INStagingStockTransferItem));
                            
                            headerlist = mainJsonResponse.parmTOHeader(OEI1INStagingStockTransferJSONSerializerHeader::serializeJSON(stagingStockTFHeader,  
                                                                                                                    stagingStockTFCustomer,
                                                                                                                    stagingStockTFItem));
                            custlist = mainJsonResponse.parmTOCustomers(OEI1INStagingStockTransferJSONSerializer::serializeJSON(stagingStockTFHeader, 
                                                                                                                    stagingStockTFCustomer,
                                                                                                                    stagingStockTFItem));
                            itemlist =  mainJsonResponse.parmTOLines(OEI1INStagingStockTransferJSONSerializerItems::serializeJSON(stagingStockTFHeader, 
                                                                                                                    stagingStockTFCustomer,
                                                                                                                    stagingStockTFItem));
                            
                            if (mainJsonResponse)
                            {
                                OEI1INStagingStockTransferHeader::updateOBProcessed(stagingStockTFHeader.RecId);
                                OEI1INStagingStockTransferCustomer::updateOBProcessed(stagingStockTFCustomer.RecId);
                                OEI1INStagingStockTransferItem::updateOBProcessed(stagingStockTFItem.RecId);
                            }
                        }
                    }
                    else
                    {
                        qrun = new QueryRun(query);
                        while (qrun.next())
                        {
                            stagingStockTFHeader = qRun.get(tableNum(OEI1INStagingStockTransferHeader));
                            stagingStockTFCustomer = qRun.get(tableNum(OEI1INStagingStockTransferCustomer));
                            stagingStockTFItem = qRun.get(tableNum(OEI1INStagingStockTransferItem));
                            mainJsonResponse.parmTOHeader(OEI1INStagingStockTransferJSONSerializerHeader::serializeJSON(stagingStockTFHeader,
                                                                                                                    stagingStockTFCustomer,
                                                                                                                    stagingStockTFItem));
                            mainJsonResponse.parmTOCustomers(OEI1INStagingStockTransferJSONSerializer::serializeJSON(stagingStockTFHeader,
                                                                                                                    stagingStockTFCustomer,
                                                                                                                    stagingStockTFItem));
                            mainJsonResponse.parmTOLines(OEI1INStagingStockTransferJSONSerializerItems::serializeJSON(stagingStockTFHeader,
                                                                                                                    stagingStockTFCustomer,
                                                                                                                    stagingStockTFItem));
                            if (mainJsonResponse)   
                            {
                                OEI1INStagingStockTransferHeader::updateOBProcessed(stagingStockTFHeader.RecId);
                                OEI1INStagingStockTransferCustomer::updateOBProcessed(stagingStockTFCustomer.RecId);
                                OEI1INStagingStockTransferItem::updateOBProcessed(stagingStockTFItem.RecId);
                            }
                        }
                    }
                }
                catch (Exception::Error)
                {
                   // mainJsonResponse.parmSuccessMessage(false);
                    //mainJsonResponse.parmOrderMessage(OEI1INLogInterfacesExceptions::getErrorMessage());
                }
            
                listIterator.next();
            }
        
        
        //list.addEnd(mainJsonResponse);
        mainJsonResponseList.parmTOHeader(headerlist);
        mainJsonResponseList.parmTOCustomers(custlist);
        mainJsonResponseList.parmTOLines(itemlist);
        list.addEnd(mainJsonResponseList);
        rootJSonResponse.parmMainJson(list);
        return rootJSonResponse;
    }
}
 
 
Please give some suggestion here
  • Suggested answer
    Community member Profile Picture
    8 on at
    API JSON response is not returning all the lines but just the last inserted row
    Hi Martin,
     
    Thanks a ton for helping me.
     
    Your advice and solution was awesome and it helped me a lot achieving the requirement.
     
    The issue was with the list as you said , actually it was getting overwritten and also the selection criteria . I did some modification on my query and returned a single list and then it worked .
     
    also as you suggested I have used just one header and inside that used the lines as its property.
     
    You are just the super star of D365 F&O ....!!!
     
    I HAVE ALWAYS BEEN A BIG FAN OF YOURS.
     
    Could you advice where can I get all the best practices that needs to be followed while development?
  • Verified answer
    Martin Dráb Profile Picture
    232,866 Most Valuable Professional on at
    API JSON response is not returning all the lines but just the last inserted row
    Note that saying "I could see all the data inside the list" isn't very useful, because your solution uses many lists and it's not clear which one is "the list".
     
    In addition to bugs that I mentioned and/or already fixed (such as the fact that you keep overwriting the lists), I see these in those two classes:
     
    1. OEI1INStagingStockTransferRootSerializer isn't decorated with DataContract attribute. And as the discussed, the name is completely wrong, because it's no serializer.
    2. Name of serializeJSON() is wrong, as suspected. It does no serialization to JSON.
    3. You have a loop inside serializeJSON(), but you have just a single header object. If TransferOrderNo is a unique key of OEI1INStagingStockTransferHeader table, then the query is useless - you can simply use _stockTransferHeader. If it's not, then it's a bug that you don't create a new object for each record. If the query returns ten records, you'll end up with a list of ten references to the same object, with values form the last OEI1INStagingStockTransferHeader record.
    4. It seems that neither _stockTransferCustomer nor _stockTransferItem parameter of serializeJSON() are needed. _stockTransferItem is useless for sure - you never use it. The only purpose of _stockTransferCustomer is getting TransferOrderNo, but don't you have the same value in _stockTransferHeader.TransferOrderNo?
    5. I don't know your code of OEI1INStagingStockTransferJSONSerializer::serializeJSON() and OEI1INStagingStockTransferJSONSerializerItems::serializeJSON(), but I'm assuming that you duplicated the same bugs there.
     
    Your code may provide a correct result only in special cases (when there no or a single record to create). Otherwise the bugs guarantee a wrong result.
     
    I recommend that you look at all the advice and sample code I gave you and rewrite your code accordingly. You'll make things much more simpler if you do a better job in the design of your contract classes. For example, instead of having two independent lists for headers and lines, make a list of lines a property of the header contract.
     
    Then create a runnable class for testing. Run this class repeatedly and test all parts of your code. Only when all the pieces work correctly and you're getting a correct object tree for given parameters, it's time to use your code in a web service.
     
    Maybe you'll still run into a problem that you'll need to ask about, but when you debug smaller parts of your code, you'll be able to ask more specific questions. For example, you'd show us code of OEI1INStagingStockTransferJSONSerializerHeader::serializeJSON() and ask why you're getting the same values in headerList variable. This is much better than throwing a bunch of code us, a part of which is useless (e.g. all the unused variables) and a lot of code missing and asking us what's wrong. This shows that you didn't debug your code correctly. Also note and the design and code quality issues makes debugging harder for you, therefore dealing with these issues makes your own life easier.
  • Community member Profile Picture
    8 on at
    API JSON response is not returning all the lines but just the last inserted row
    Hi MArtin,
     
    I have modified the code as per you suggested and still getting the same issue.
     
    Please see the modified code below:
    class OEI1INStagingStockTransferService
    {
        [AifCollectionTypeAttribute('return', Types::Class, classStr(OEI1INStagingStockTransferHirarchyJSONResponse))]
        public List processStockTransfer(OEI1INStagingStockTransferJSONRequest _stockTransferRequest)
        {
            Query query = new Query(queryStr(OEI1INStagingStockTransferQuery));
            //QueryRun                qrun;
            //QueryBuildDataSource    qbdsCustCode;
            //QueryBuildRange         qbr;
            //int                     iCnt = 0;
            //Notes JSONstring = "";
            //OEI1INStagingStockTransferHirarchyJSONResponse mainJsonResponse = new OEI1INStagingStockTransferHirarchyJSONResponse();
            //OEI1INStagingStockTransferHirarchyJSONResponse mainJsonResponseList = new OEI1INStagingStockTransferHirarchyJSONResponse();
            //OEI1INStagingStockTransferRootSerializer    rootJSonResponse = new OEI1INStagingStockTransferRootSerializer();
            //OEI1INStgStockTransferJSRes jsonResSTR,jsonResSTRRec;
            //OEI1INStagingStockTransferHeader stagingStockTFHeader,stockTransferHeaderUpdate;
            //OEI1INStagingStockTransferCustomer stagingStockTFCustomer,stockTransferCustomerUpdate;
            //OEI1INStagingStockTransferItem stagingStockTFItem,stockTransferItemUpdate;
            //OEI1INStagingStockTransferContract requestStockTransfer;
            //ListIterator listIterator;
            //List list = new List(Types::Class);
            //List headerlist = new List(Types::Class);
            //List custlist = new List(Types::Class);
            ////List itemlist = new List(Types::Class);
            //ListEnumerator  lEnum,lEnumCust,lEnumItem;

            List itemList = new List(Types::Class);
            ListEnumerator requestEnumerator = _stockTransferRequest.OEI1INStagingStockTransferContract().getEnumerator();
            while (requestEnumerator.moveNext())
            {
                OEI1INStagingStockTransferContract requestStockTransfer = requestEnumerator.current();
            
                try
                {
                    QueryBuildDataSource transferCustDs = query.dataSourceTable(tableNum(OEI1INStagingStockTransferCustomer));
                    if (requestStockTransfer.parmCustomerCode())
                    {
                        QueryBuildRange custCodeRange = SysQuery::findOrCreateRange(transferCustDs, fieldNum(OEI1INStagingStockTransferCustomer, CustomerCode));
                        custCodeRange.value(queryValue(requestStockTransfer.parmCustomerCode()));
                    }
                
                    QueryRun queryRun = new QueryRun(query);
                    while (queryRun.next())
                    {
                        //OEI1INStagingStockTransferHeader stagingStockTFHeader = queryRun.get(tableNum(OEI1INStagingStockTransferHeader));
                        OEI1INStagingStockTransferCustomer stagingStockTFCustomer = queryRun.get(tableNum(OEI1INStagingStockTransferCustomer));
                        //OEI1INStagingStockTransferItem stagingStockTFItem = queryRun.get(tableNum(OEI1INStagingStockTransferItem));
                    
                        OEI1INStagingStockTransferHirarchyJSONResponse responseItem = this.createResponse(stagingStockTFCustomer.TransferOrderNo,
                                                                                                          stagingStockTFCustomer.CustomerCode);
                    
                        // Here we add the item to a list
                        itemList.addEnd(responseItem);
                        // TODO: don't forget to think about database transactions. The current implementation could lead to data inconsistency.
                        //OEI1INStagingStockTransferHeader::updateOBProcessed(stagingStockTFHeader.RecId);
                        //OEI1INStagingStockTransferCustomer::updateOBProcessed(stagingStockTFCustomer.RecId);
                        //OEI1INStagingStockTransferItem::updateOBProcessed(stagingStockTFItem.RecId);
                    }
                }
                catch (Exception::Error)
                {
                    // TODO: implement
                }
            }
            
            //OEI1INStagingStockTransferJSONResponse jSonResponse = new OEI1INStagingStockTransferJSONResponse();
            //// Initialize the interface iterator
            //listIterator = new ListIterator(_stockTransferRequest.OEI1INStagingStockTransferContract());

            //    while (listIterator.more())
            //    {
            //        requestStockTransfer = listIterator.value();
            //        try
            //        {
            //            if (requestStockTransfer.parmcustomerCode() != "")
            //            {
            //                qbdsCustCode = query.dataSourceTable(tableNum(OEI1INStagingStockTransferCustomer));
            //                qbr = qbdsCustCode.addRange(fieldNum(OEI1INStagingStockTransferCustomer,CustomerCode));
            //                qbr.value(SysQuery::value(requestStockTransfer.parmcustomerCode()));
            //                qrun = new QueryRun(query);
            //                while (qrun.next())
            //                {
            //                    //stagingStockTFHeader = qrun.get(tableNum(OEI1INStagingStockTransferHeader));
            //                    stagingStockTFCustomer = qrun.get(tableNum(OEI1INStagingStockTransferCustomer));
            //                    //stagingStockTFItem = qrun.get(tableNum(OEI1INStagingStockTransferItem));

            //                    headerlist = mainJsonResponse.parmTOHeader(OEI1INStagingStockTransferJSONSerializerHeader::serializeJSON(stagingStockTFHeader,  
            //                                                                                                            stagingStockTFCustomer,
            //                                                                                                            stagingStockTFItem));
            //                    custlist = mainJsonResponse.parmTOCustomers(OEI1INStagingStockTransferJSONSerializer::serializeJSON(stagingStockTFHeader, 
            //                                                                                                            stagingStockTFCustomer,
            //                                                                                                            stagingStockTFItem));
            //                    itemlist =  mainJsonResponse.parmTOLines(OEI1INStagingStockTransferJSONSerializerItems::serializeJSON(stagingStockTFHeader, 
            //                                                                                                            stagingStockTFCustomer,
            //                                                                                                            stagingStockTFItem));
            //                    if (mainJsonResponse)
            //                    {
            //                        OEI1INStagingStockTransferHeader::updateOBProcessed(stagingStockTFHeader.RecId);
            //                        OEI1INStagingStockTransferCustomer::updateOBProcessed(stagingStockTFCustomer.RecId);
            //                        OEI1INStagingStockTransferItem::updateOBProcessed(stagingStockTFItem.RecId);
            //                    }
            //                }
            //            }
            //            else
            //            {
            //                qrun = new QueryRun(query);
            //                while (qrun.next())
            //                {
            //                    stagingStockTFHeader = qRun.get(tableNum(OEI1INStagingStockTransferHeader));
            //                    stagingStockTFCustomer = qRun.get(tableNum(OEI1INStagingStockTransferCustomer));
            //                    stagingStockTFItem = qRun.get(tableNum(OEI1INStagingStockTransferItem));
            //                    mainJsonResponse.parmTOHeader(OEI1INStagingStockTransferJSONSerializerHeader::serializeJSON(stagingStockTFHeader,
            //                                                                                                            stagingStockTFCustomer,
            //                                                                                                            stagingStockTFItem));
            //                    mainJsonResponse.parmTOCustomers(OEI1INStagingStockTransferJSONSerializer::serializeJSON(stagingStockTFHeader,
            //                                                                                                            stagingStockTFCustomer,
            //                                                                                                            stagingStockTFItem));
            //                    mainJsonResponse.parmTOLines(OEI1INStagingStockTransferJSONSerializerItems::serializeJSON(stagingStockTFHeader,
            //                                                                                                            stagingStockTFCustomer,
            //                                                                                                            stagingStockTFItem));
            //                    if (mainJsonResponse)   
            //                    {
            //                        OEI1INStagingStockTransferHeader::updateOBProcessed(stagingStockTFHeader.RecId);
            //                        OEI1INStagingStockTransferCustomer::updateOBProcessed(stagingStockTFCustomer.RecId);
            //                        OEI1INStagingStockTransferItem::updateOBProcessed(stagingStockTFItem.RecId);
            //                    }
            //                }
            //            }
            //        }
            //        catch (Exception::Error)
            //        {
            //           // mainJsonResponse.parmSuccessMessage(false);
            //            //mainJsonResponse.parmOrderMessage(OEI1INLogInterfacesExceptions::getErrorMessage());
            //        }
            //        listIterator.next();
            //    }
            ////list.addEnd(mainJsonResponse);
            //mainJsonResponseList.parmTOHeader(headerlist);
            //mainJsonResponseList.parmTOCustomers(custlist);
            //mainJsonResponseList.parmTOLines(itemlist);
            //list.addEnd(mainJsonResponseList);
            //rootJSonResponse.parmMainJson(list);
            return itemList;
        }
        public OEI1INStagingStockTransferHirarchyJSONResponse createResponse(String20 _transferOrderNo,CustAccount  _custAcc)
        {
            OEI1INStagingStockTransferHirarchyJSONResponse jsonResponse = new OEI1INStagingStockTransferHirarchyJSONResponse();
            OEI1INStagingStockTransferHeaderJSONResponse header = new OEI1INStagingStockTransferHeaderJSONResponse();
            OEI1INStagingStockTransferItemJSONResponse  item = new OEI1INStagingStockTransferItemJSONResponse();
            OEI1INStagingStockTransferCustomerJSONResponse  customer = new OEI1INStagingStockTransferCustomerJSONResponse();
            OEI1INStagingStockTransferHeader    headerStaging;
            OEI1INStagingStockTransferCustomer  customersStaging;
            OEI1INStagingStockTransferItem      itemsStaging;
            List                            headerList = new List(Types::Class);
            List                            itemsList = new List(Types::Class);
            List                            customerList = new List(Types::Class);
            //TO Header
            Query query = new Query();
            QueryBuildDataSource    qbds;
            QueryBuildRange         qbr;
            QueryRun                qRun;
            qbds = query.addDataSource(tableNum(OEI1INStagingStockTransferHeader));
            qbr = qbds.addRange(fieldNum(OEI1INStagingStockTransferHeader,TransferOrderNo));
            qbr.value(SysQuery::value(_transferOrderNo));
            qRun = new QueryRun(query);
            
            while (qRun.next())
            {
                headerStaging = qrun.get(tableNum(OEI1INStagingStockTransferHeader));
                    
                header.parmTransferOrderNo(headerStaging.TransferOrderNo);
                header.parmTransferOrderDate(headerStaging.TransferOrderDate);
                header.parmTripNo(headerStaging.TripNO);
                header.parmVehicleNo(headerStaging.VehicleNo);
                header.parmType(headerStaging.Type);
                headerList.addEnd(header);
            }
            //TO Items
            Query queryItems = new Query();
            QueryBuildDataSource    qbdsItems;
            QueryBuildRange         qbrItems;
            QueryRun                qRunItems;
            qbdsItems = queryItems.addDataSource(tableNum(OEI1INStagingStockTransferItem));
            qbrItems = qbdsItems.addRange(fieldNum(OEI1INStagingStockTransferItem,TransferOrderNo));
            qbrItems.value(SysQuery::value(_transferOrderNo));
            qRunItems = new QueryRun(queryItems);
            
            while (qRunItems.next())
            {
                itemsStaging = qRunItems.get(tableNum(OEI1INStagingStockTransferItem));
                    
                item.parmItemCode(itemsStaging.ItemCode);
                item.parmProductHierarchyCode(itemsStaging.ProductHierarchyCode);
                item.parmLegacyCode(itemsStaging.LegacyCode);
                item.parmMRP(itemsStaging.MRPConfig);
                item.parmBatchNo(itemsStaging.Batch);
                item.parmMFGDate(itemsStaging.MFGdate);
                item.parmExpiryDate(itemsStaging.ExpiryDate);
                item.parmCrateQty(itemsStaging.CrateQty);
                item.parmBottleQty(itemsStaging.BottleQty);
                item.parmQty(itemsStaging.QTY);
                item.parmTenantCode(itemsStaging.TenantCode);
                item.parmTransferOrderNo(itemsStaging.TransferOrderNo);
                item.parmTransferOrderDate(itemsStaging.TransferOrderDate);
                item.parmItemType(itemsStaging.ItemType);
                itemsList.addEnd(item);
            }
            //TO Customer
            Query queryCust = new Query();
            QueryBuildDataSource    qbdsCust;
            QueryBuildRange         qbrCust,qbrCust1;
            QueryRun                qRunCust;
            qbdsCust = queryCust.addDataSource(tableNum(OEI1INStagingStockTransferCustomer));
            qbrCust = qbdsCust.addRange(fieldNum(OEI1INStagingStockTransferCustomer,TransferOrderNo));
            qbrCust1 = qbdsCust.addRange(fieldNum(OEI1INStagingStockTransferCustomer,CustomerCode));
            qbrCust.value(SysQuery::value(_transferOrderNo));
            qbrCust1.value(SysQuery::value(_custAcc));
            qRunItems = new QueryRun(queryCust);
            
            while (qRunItems.next())
            {
                customersStaging = qRunItems.get(tableNum(OEI1INStagingStockTransferCustomer));
                customer.parmTransferOrderNo(customersStaging.TransferOrderNo);
                customer.parmCustomerCode(customersStaging.CustomerCode);
                customer.parmCustomerOrderNo(customersStaging.CustomerOrderNo);
                customer.parmCustomerOrderDate(customersStaging.CustomerOrderDate);
                customer.parmRouteNo(customersStaging.RouteNo);
                customer.parmType(customersStaging.Type);
                customerList.addEnd(customer);
            }
            jsonResponse.parmTOHeader(headerList);
            jsonResponse.parmTOLines(itemsList);
            jsonResponse.parmTOCustomers(customerList);
                
            return jsonResponse;
        }
    }
     
    Please suggest if I am doing some thing wrong here...
  • Community member Profile Picture
    8 on at
    API JSON response is not returning all the lines but just the last inserted row
    HI MArtin,
     
    Thanks a lot for the response.
     
    Kindly see the below code
     
    class OEI1INStagingStockTransferJSONSerializerHeader
    {
        public static List serializeJSON(OEI1INStagingStockTransferHeader _stockTransferHeader,
                                            OEI1INStagingStockTransferCustomer _stockTransferCustomer,
                                            OEI1INStagingStockTransferItem _stockTransferItem)
        {
            OEI1INStagingStockTransferHeaderJSONResponse header = new OEI1INStagingStockTransferHeaderJSONResponse();
            OEI1INStagingStockTransferHeader stagingHeader;
            List                            headerList = new List(Types::Class);
            
            Query query = new Query();
            QueryBuildDataSource    qbds;
            QueryBuildRange         qbr;
            QueryRun                qRun;
            qbds = query.addDataSource(tableNum(OEI1INStagingStockTransferHeader));
            qbr = qbds.addRange(fieldNum(OEI1INStagingStockTransferHeader,TransferOrderNo));
            qbr.value(SysQuery::value(_stockTransferCustomer.TransferOrderNo));
            qRun = new QueryRun(query);
            
            while (qRun.next())
            {
                stagingHeader = qRun.get(tableNum(OEI1INStagingStockTransferHeader));
                //TO Header
                header.parmTransferOrderNo(stagingHeader.TransferOrderNo);
                header.parmTransferOrderDate(stagingHeader.TransferOrderDate);
                header.parmTripNo(stagingHeader.TripNO);
                header.parmVehicleNo(stagingHeader.VehicleNo);
                header.parmType(stagingHeader.Type);
                headerList.addEnd(header);
            }
            
            return headerList;
        }
    }
     
     
    // Root Serializer Code:
     
    class OEI1INStagingStockTransferRootSerializer
    {
        List                                            mainList;
        public boolean orderProcessedSuccess,orderProcessedFailed;
        public str jSonMessage;
        public Notes jSonString;
        List                                            headerList,itemsList,customerList;
        [DataMember("OrderFailed")]
        public boolean parmErrorMessage(boolean _value = orderProcessedFailed)
        {
            if (!prmIsDefault(_value))
            {
                orderProcessedFailed = _value;
            }
            return orderProcessedFailed;
        }
        [DataMember("OrderPassed")]
        public boolean parmSuccessMessage(boolean _value = orderProcessedSuccess)
        {
            if (!prmIsDefault(_value))
            {
                orderProcessedSuccess = _value;
            }
            return orderProcessedSuccess;
        }
        [DataMember("OrderMessage")]
        public str parmOrderMessage(str _value = jSonMessage)
        {
            if (!prmIsDefault(_value))
            {
                jSonMessage = _value;
            }
            return jSonMessage;
        }
        // TO Customers
        [DataMemberAttribute("_requestStockTransfer"),
        DataCollectionAttribute(Types::Class,classStr(OEI1INStagingStockTransferHirarchyJSONResponse))]
        public List parmMainJson(List _mainList = mainList)
        {
            mainList = _mainList;
            return mainList;
        }
    }
     
    And yes when I debugged then I could see all the data inside the list. But when I get the response in postman then I could see just the last insetrted record
  • Martin Dráb Profile Picture
    232,866 Most Valuable Professional on at
    API JSON response is not returning all the lines but just the last inserted row
    I greatly simplified your code for you. For example, most of your variables are actually not used at all.
     
    I've noticed that you create a single entry in three list of each item, which sounds strange to me (I would expect a single header with several lines), but if that's what you want, a better approach is creating a data contract object for each entry and returning a list of such objects. For example:
    [AifCollectionTypeAttribute('return', Types::Class, classStr(OEI1INStagingStockTransferHirarchyJSONResponse))]
    public List processStockTransfer(OEI1INStagingStockTransferJSONRequest _stockTransferRequest)
    {
        Query query = new Query(queryStr(OEI1INStagingStockTransferQuery));
        List itemList = new List(Types::Class);
        ListEnumerator requestEnumerator = _stockTransferRequest.OEI1INStagingStockTransferContract().getEnumerator();
        
        while (requestEnumerator.moveNext())
        {
            OEI1INStagingStockTransferContract requestStockTransfer = requestEnumerator.current();
            
            try
            {
                QueryBuildDataSource transferCustDs = query.dataSourceTable(tableNum(OEI1INStagingStockTransferCustomer));
                if (requestStockTransfer.parmCustomerCode())
                {
                    QueryBuildRange custCodeRange = SusQuery::findOrCreateRange(transferCustDs, fieldNum(OEI1INStagingStockTransferCustomer, CustomerCode));
                    custCodeRange.value(queryValue(requestStockTransfer.parmCustomerCode());
                }
                
                QueryRun queryRun = new QueryRun(query);
                while (queryRun.next())
                {
                    OEI1INStagingStockTransferHeader stagingStockTFHeader = qRun.get(tableNum(OEI1INStagingStockTransferHeader));
                    OEI1INStagingStockTransferCustomer stagingStockTFCustomer = qRun.get(tableNum(OEI1INStagingStockTransferCustomer));
                    OEI1INStagingStockTransferItem stagingStockTFItem = qRun.get(tableNum(OEI1INStagingStockTransferItem));
                    
                    // A method you should create
                    OEI1INStagingStockTransferHirarchyJSONResponse responseItem = this.createResponse(stagingStockTFHeader, stagingStockTFCustomer, stagingStockTFItem);
                    
                    // Here we add the item to a list
                    itemList.addEnd(responseItem);
    
                    // TODO: don't forget to think about database transactions. The current implementation could lead to data inconsistency.
                    OEI1INStagingStockTransferHeader::updateOBProcessed(stagingStockTFHeader.RecId);
                    OEI1INStagingStockTransferCustomer::updateOBProcessed(stagingStockTFCustomer.RecId);
                    OEI1INStagingStockTransferItem::updateOBProcessed(stagingStockTFItem.RecId);
                }
            }
            catch (Exception::Error)
            {
                // TODO: implement
            }
        }
        
        return itemList;
    }
  • Martin Dráb Profile Picture
    232,866 Most Valuable Professional on at
    API JSON response is not returning all the lines but just the last inserted row
    I think I see the key bug - you keep overwriting itemlist variable or rootResponselist inside OEI1INStagingStockTransferHirarchyRootJSONResponse (depending on which case it is). You should add elements to the list.

    What code do you have in OEI1INStagingStockTransferJSONSerializerHeader::serializeJSON()? It sounds logically wrong to me. You should create a data contract object, not to serialize something to JSON.
  • Martin Dráb Profile Picture
    232,866 Most Valuable Professional on at
    API JSON response is not returning all the lines but just the last inserted row
    How is OEI1INStagingStockTransferRootSerializer class defined? By the way, the name looks misleading - it should be a data contract. Serialization, i.e. creating JSON string based on the object graph, is handled by F&O kernel.
     
    When you debugged your code, did you see all the data inside rootJSonResponse object at the end of processStockTransfer method?

    Also, what's the point of the list variable if it always contains a single element?

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,099 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,866 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans