+ Start a Discussion
d.tejdeep@nicomatic.ind.tejdeep@nicomatic.in 

JSON.Serialize changes the order of records in a list

Apex:

List<order_batch__c> bcList = new List<order_batch__c>();
        bcList= [select Or_sub_po_number__c,Or_customer_PO_Number__c,Or_customerid__c,Or_Account_name__c,Or_invoiceadd1__c,Or_invoiceadd2__c,Or_invoiceadd3__c,Or_invoiceadd4__c,Or_invoiceadd5__c,Or_contact_Phone__c,Or_contact_Fax__c,Or_Subcontact_family_name__c,Or_Subcontact_first_name__c,Or_contact_Name__c,Or_contact_Email__c,Or_Carrier__c,Order_Date__c,Or_Comments__c,Order_numbering__c,Or_clientpn__c,Or_Nicomatic_p_n__c,Quantity_Ordered__c,Requested_Date__c,Discount__c,Unit_price__c,Or_Quote_number__c,currencyisocode,id from order_batch__c  where  Quote_Line_Item__r.Quote1__c= : Apexpages.currentPage().getParameters().get('Id')];
       system.debug(bcList);
        String JSONString = JSON.serialize(bcList);


In debug console:

12:39:38:127 USER_DEBUG [44]|DEBUG|(Order_batch__c:{Order_numbering__c=1, Or_contact_Email__c=c@gmail.com, Or_Nicomatic_p_n__c=123, Or_customerid__c=2, CurrencyIsoCode=CAD, Or_Comments__c=12, Or_invoiceadd1__c=4, Discount__c=false, Requested_Date__c=2014-09-01 00:00:00, Or_sub_po_number__c=BHARAT, Or_invoiceadd4__c=7, Or_Quote_number__c=NINAL140718548, Quote_Line_Item__c=a0LN0000000yWu4MAE, Or_invoiceadd3__c=6, Unit_price__c=11.0000, Or_invoiceadd5__c=8, Or_customer_PO_Number__c=BHARAT, Or_Account_name__c=3, Or_contact_Phone__c=97999999999, Or_Carrier__c=9, Id=a1EN00000001iBQMAY, Or_contact_Fax__c=123333333, Or_invoiceadd2__c=5, Quantity_Ordered__c=1.0000}, Order_batch__c:{Order_numbering__c=3, Or_contact_Email__c=c@gmail.com, Or_Nicomatic_p_n__c=456, Or_customerid__c=2, CurrencyIsoCode=CAD, Or_Comments__c=12, Or_invoiceadd1__c=4, Discount__c=true, Requested_Date__c=2014-09-02 00:00:00, Or_sub_po_number__c=BHARAT, Or_invoiceadd4__c=7, Or_Quote_number__c=NINAL140718548, Quote_Line_Item__c=a0LN0000000yWu3MAE, Or_invoiceadd3__c=6, Unit_price__c=2.0000, Or_invoiceadd5__c=8, Or_customer_PO_Number__c=BHARAT, Or_Account_name__c=3, Or_contact_Phone__c=97999999999, Or_Carrier__c=9, Id=a1EN00000001iGtMAI, Or_contact_Fax__c=123333333, Or_invoiceadd2__c=5, Quantity_Ordered__c=12.0000}, Order_batch__c:{Order_numbering__c=2, Or_customerid__c=2, Or_Comments__c=12, Discount__c=false, Order_Date__c=2014-09-01 00:00:00, Or_sub_po_number__c=BHARAT, Or_invoiceadd4__c=7, Or_invoiceadd3__c=6, Or_contact_Phone__c=97999999999, Or_invoiceadd2__c=5, Or_contact_Email__c=c@gmail.com, Or_Nicomatic_p_n__c=123, CurrencyIsoCode=CAD, Or_invoiceadd1__c=4, Requested_Date__c=2014-09-01 00:00:00, Quote_Line_Item__c=a0LN0000000yWu4MAE, Or_Quote_number__c=NINAL140718548, Unit_price__c=1.0000, Or_invoiceadd5__c=8, Or_customer_PO_Number__c=BHARAT, Or_Account_name__c=3, Or_Carrier__c=9, Id=a1EN00000001iBLMAY, Or_contact_Fax__c=123333333, Quantity_Ordered__c=1.0000})


How i can send the same order like in the list i specified
kcpluspluskcplusplus
When you are saying the list you specified, you are referring to the fields you specified in the query correct? 

If that is what you mean, there is no guarantee in apex that the soql you run, will return an sobject with the fields in the same order as your query, and in fact most likely will reorder them, for optimization on the database engine most likely. So that being said, in apex when returning an sobject or in javascript with an object, you are dealing with name value pairs, with no order, because you can retrieve things via a key or dot notation, where as an apex list or js array will have an order. 

So, if you are trying to do some dynamic type of display or processing, you can store field names in a list or array, and use that to have a definite order and then display or process your fields in the order you want.

--KC